How to create multiple txt files with incrementing numbers and writing in them using batch files (.bat)
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
Related videos on Youtube
Rachid
Updated on September 18, 2022Comments
-
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.
-
DavidPostill about 4 yearsPlease read For - Loop through a range of numbers - Windows CMD - SS64.com - you need parentheses when using miltiple command in a for loop.
-
DavidPostill about 4 yearsPlease read Command Redirection, Pipes - Windows CMD - SS64.com - You are overwriting the file each time you echo to it.
-
DavidPostill about 4 yearsPlease read For - Looping commands - Windows CMD - SS64.com - You need to use
%%
inside a batch file.
-
-
Rachid about 4 yearsit actually creates the txt files but in each file it writes all the text.
-
Rachid about 4 yearsit 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 about 4 yearsWill 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 about 4 yearsactually I have another one that contains 106. I forgot it.
-
Durry42 about 4 yearsHow are the comments/lines stored? Do you have them all on individual lines in the one text file?
-
Rachid about 4 yearsYes.
-
Durry42 about 4 years@Rachid i've edited my answer to include an option based on your extra information
-
Rachid about 4 yearsOoooh!! thank you so so much @Durry42, you saved my life. I've lost hope for doing this thing. Thank you again.
-
Durry42 about 4 years@Rachid you're welcome. Please remember to mark if this answer has helped.