PDA

View Full Version : Help with DOS batch file


gojira69
06-11-2007, 01:09 PM
I have a text file with about 1,000 lines of text each of which is terminated by returns.

I need each line of the text to output to its own unique file.

Is there a way using FIND (or other) to grab the first line of the file, output it to a file > [name].txt, to then find the next line and output it to a file > [name2.txt] for each line of text?

Thank you in advance for your kind consideration.

sburtchin
06-13-2007, 06:39 AM
Call another batch file within a FOR loop like this:

set /a count=0
FOR /F "delims= tokens=1,*" %a in (bigfile.txt) do @echo %a %b > junk.txt & makfiles.bat

makfiles.bat
set /a count += 1
type junk.txt > name%count%.txt

;) See FOR Windows NT 4/2000/XP Syntax (http://www.robvanderwoude.com/ntfor.html). May I inquire your application for this?

gojira69
06-14-2007, 12:48 PM
Call another batch file within a FOR loop like this:

set /a count=0
FOR /F "delims= tokens=1,*" %a in (bigfile.txt) do @echo %a %b > junk.txt & makfiles.bat

makfiles.bat
set /a count += 1
type junk.txt > name%count%.txt

;) See FOR Windows NT 4/2000/XP Syntax (http://www.robvanderwoude.com/ntfor.html). May I inquire your application for this?

Wow! Thanks sburtchin, this is very much appreciated! :)

gojira69
06-14-2007, 01:37 PM
Now when I run the following:
Find "000" ghsv.txt
set /a count=0
FOR /F "delims= tokens=1,*" %a in (ghsv.txt) do @echo %a %b > junk.txt & increment.bat
After printing all lines of text from "ghsv.txt" to the DOS window, it returns this error:
set /a count=0
a was unexpected at this time.

P.S. Fyi, "000" is at the beginning of each line (record) in ghsv.txt

sburtchin
06-17-2007, 02:14 AM
Read note 1 at FOR loops (http://www.robvanderwoude.com/for.html).

If you are new to batch files, you should closely read the applicable pages of the commands you use at Robert Vanderwoude's site. I am not new to batch files, but I had not used DOS FOR loops until recently. Your task was good incentive for me to finally figure this FOR thing out. Took me a while to get past that the DOS FOR is quite strange compared to FOR in every other language I know. Robert Vanderwoude's site was an invaluable resource. Grabbing complete records from a data file within a batch file is something I wanted to figure out anyway, as this has many potential applications.

I am puzzled that you have "Find "000" ghsv.txt" in you batch file, unless ghsv.txt contains records not beginning with "000" and you only want to look at the ones that do. In that case you need to redirect its output for input to the FOR loop. I think you can even put the FIND inside the FOR --- you'll need to check that. See the link.

I am really curious. What is "ghsv.txt" and why do you want to create a thousand new files?

P.S. I chose to call another batch file from within the FOR loop because that worked for me. It would be much neater if it could all be done within the one batch file, but I kept getting strange results trying to do the incrementing and file creation within the FOR loop, but then I did all the testing of this from the command line (ie. I never actually created the parent batch file). If you figure out how to do it all in only one batch file, please post the solution.