How to create multiple txt files with incrementing numbers and writing in them using batch files (.bat)

5,774

The following will create files 1.txt through to 13.txt in the current working directory. Each file will contain the 4 lines of Text specified.

@echo off
for /l %%a in (1,1,13) do (
   >>%%a.txt echo bla bla bla
   >>%%a.txt echo bla bla blog
   >>%%a.txt echo bla bla bla
   >>%%a.txt echo blom blom blom
)

Notes: When you use @echo off it hides command execution output from that point onwards in the script. This means you do not need to add @ before any commands after this point.

Using echo blah blah > %%A.txt will work, however you will actually end up with blah blah{space} (A space on the end of line) written to the file. Instead, use >>%%a.txt echo blah blah will result in just blah blah


EDIT:

Added this option based on comments by OP re using body text from file.

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('type "C:\Test Folder\MyList.txt"') do (
    set /a count+=1
    >!count!.txt echo %%a
)
endlocal
exit /B

This will read the lines one by one from C:\Test Folder\MyList.txt, and for each line it will add 1 to the variable called count.

Next it will create a file named as the current line number (Eg. 1.txt 2.txt) and it place into that file the current line it has read from the MyList.txt file.

So if you had a the following lines in the MyList.txt file...

Hey i'm first
Hi i'm second
Third line

You would end up with...

1.txt containing the line Hey i'm first

2.txt containing the line Hi i'm second

3.txt containing the line Third line

Share:
5,774

Related videos on Youtube

Rachid
Author by

Rachid

Updated on September 18, 2022

Comments

  • Rachid
    Rachid over 1 year

    I want to create multiple txt files with incrementing numbers with batch; here's my example

    @echo off
    for /l %i in (1,1,13) do
      @echo bla bla bla > "%i.txt"   
      @echo bla bla blo > "%i.txt"   
      @echo bli bli bla > "%i.txt"   
      @echo blom blom blom > "%i.txt"
    

    Help me, please.

  • Rachid
    Rachid about 4 years
    it actually creates the txt files but in each file it writes all the text.
  • Rachid
    Rachid about 4 years
    it actually creates the txt files but in each file it writes all the text. What I want is, in 1.txt there is only "bla bla" and in 2.txt there is only "blo blo" and so on. I hope you understand me.
  • Durry42
    Durry42 about 4 years
    Will there only be 1 line in each file? If it is only 13 files...wouldn't it be easy to just manually create each.
  • Rachid
    Rachid about 4 years
    actually I have another one that contains 106. I forgot it.
  • Durry42
    Durry42 about 4 years
    How are the comments/lines stored? Do you have them all on individual lines in the one text file?
  • Rachid
    Rachid about 4 years
    Yes.­­­­­­­­­­­
  • Durry42
    Durry42 about 4 years
    @Rachid i've edited my answer to include an option based on your extra information
  • Rachid
    Rachid about 4 years
    Ooooh!! thank you so so much @Durry42, you saved my life. I've lost hope for doing this thing. Thank you again.
  • Durry42
    Durry42 about 4 years
    @Rachid you're welcome. Please remember to mark if this answer has helped.