Custom Search
Join the PC homebuilding revolution! Read the all-new, FREE 200-page online guide: How to Build Your Own PC!
NOTE: Using robot software to mass-download the site degrades the server and is prohibited. See here for more.
Find The PC Guide helpful? Please consider a donation to The PC Guide Tip Jar. Visa/MC/Paypal accepted.
Results 1 to 5 of 5

Thread: Help with DOS batch file

  1. #1

    Help with DOS batch file

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Lost In Middle America
    Posts
    337
    Call another batch file within a FOR loop like this:

    Code:
    set /a count=0
    FOR /F "delims= tokens=1,*" %a in (bigfile.txt) do @echo %a %b > junk.txt & makfiles.bat
    makfiles.bat
    Code:
    set /a count += 1
    type junk.txt > name%count%.txt
    See FOR Windows NT 4/2000/XP Syntax. May I inquire your application for this?

  3. #3
    Quote Originally Posted by sburtchin View Post
    Call another batch file within a FOR loop like this:

    Code:
    set /a count=0
    FOR /F "delims= tokens=1,*" %a in (bigfile.txt) do @echo %a %b > junk.txt & makfiles.bat
    makfiles.bat
    Code:
    set /a count += 1
    type junk.txt > name%count%.txt
    See FOR Windows NT 4/2000/XP Syntax. May I inquire your application for this?
    Wow! Thanks sburtchin, this is very much appreciated!

  4. #4
    Now when I run the following:
    Code:
    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:
    Code:
    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

  5. #5
    Join Date
    Feb 2006
    Location
    Lost In Middle America
    Posts
    337
    Read note 1 at FOR loops.

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •