# Batch file to create txt file containing spcified markers.



## Disheer (Dec 18, 2010)

I am currently trying to create a batch file that will take what is in a .ini file and make it into a .txt file that will be able to be transfer to sql.


I am try to convert 1038 lines of 10000 (item name) 10000
to a .sql that goes as follows

update table set name = "Name" where id = 10

ini is as follows 

10000 hot~hair 10000
10001 cold~hair 10001
10002 wet 10002

Any help would be appreciated


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!


Disheer said:


> I am currently trying to create a batch file that will take what is in a .ini file and make it into a *.txt* file that will be able to be transfer to sql.
> 
> I am try to convert 1038 lines of 10000 *(item name)* 10000
> to a *.sql* that goes as follows
> ...


Is the new file going to be *.txt* or *.sql*?

Is *"Name"* the *(item name)* from the .ini file?
Is every line going to be *id = 10*, or is 10 the 5 digit number from each line.

Are there any spaces in the *item name*, or are there only two spaces in each line: one after the first 5 digit number, and one before the last 5 digit number?
Are they all 5 digit numbers?
Is the first and last number always the same?

So your result based on the sample will be


```
update table set name = "hot~hair" where id = 10000
update table set name = "cold~hair" where id = 10001
update table set name = "wet" where id = 10002
```
or this:

```
update table set name = "hot~hair" where id = 10
update table set name = "cold~hair" where id = 10
update table set name = "wet" where id = 10
```


----------



## Ent (Apr 11, 2009)

Is this what you're after? It reads from the file test.ini in the current directory (%cd%). You can of course change either of those values to your own preference for the location of the ini file. Output is directed to test.txt in the same folder as the ini file.
Note that it uses spaces as delimiters to select the middle section. If your file has any items with spaces in (e.g. 1100 one two 1100) they won't work.


```
@echo off
pushd [I]%cd%[/I]
if exist test.txt del test.txt
For /F "tokens=2 delims= " %%I in ([I]test.ini[/I]) do ( Echo update table set name = "%%I" where id = 10 >> test.txt )
Echo Process Complete
Pause
```
Or if theOutCaste is correct in his first sample and you want the number to be used as the ID, this code is the equivalent.


```
@echo off
pushd %cd%
if exist test.txt del test.txt
For /F "tokens=1,2 delims= " %%I in (test.ini) do ( Echo update table set name = "%%J" where id = %%I >> test.txt )
Echo Process Complete
Pause
```


----------



## Disheer (Dec 18, 2010)

I need each line to have a the number that is at the beginning of the line. The number is at the beginning of the line with item name in middle and then number again like the above example. 

update table set name = "Item Name" where id = Number at beginning of code;

Exactly as it shows there. That is the only way it will update corectly. Ending result needs to either be a .txt or .sql either works for this. I can change .txt to .sql to get the same results if it is put in this manner.


----------



## TheOutcaste (Aug 8, 2007)

TheOutcaste said:


> Are there any spaces in the *item name*, or are there only two spaces in each line


----------



## Disheer (Dec 18, 2010)

There are no spaces in the item name but as I looked at the list I found I only want the first 2 parts of the list not the third part as some of the lines have the same third ending.


----------



## Ent (Apr 11, 2009)

What do you mean by the third part, the 10001 or other number that appears at the end of each line? Does that mean that they are not all the same as the starting number?


----------



## TheOutcaste (Aug 8, 2007)

The 2nd program Ent posted will do what you want. Edit the file names as needed, use the PushD statement to change to the folder the .ini file is in, or put the batch file into the same directory.
One change, remove the space between the *%%I* and the *>>* symbols, that space will be added to the end of the line in the new file. That may cause issues depending on how you use the file, and a trailing space can be hard to notice.

I prefer using variables for file names/Paths, that way you only need to change them in one place:

```
@Echo Off
Set _IniFile=Input.ini
Set _OutFile=Output.sql
Set _Path=C:\FolderWithIniFile
PushD %_Path%
If Exist %_OutFile% Del %_OutFile%
For /F "tokens=1,2 delims= " %%I In (%_IniFile%) Do Echo update table set name = "%%J" where id = %%I>>%_OutFile%
Echo Process Complete
Pause
```


----------



## Ent (Apr 11, 2009)

> I prefer using variables for file names/Paths, that way you only need to change them in one place:


Thank you. Lesson duly noted.


----------



## Disheer (Dec 18, 2010)

Thank you so much worked perfectly


----------

