Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to make a batch file which creates a certain ammount of text files without copying and pasting code.
That is, I want to make it perform the action once, then loop the code x times. X could be 10,000 so I don't want to have to copy and paste code 10,000 times.E.g.
If I want to create 5 text files with "Hello" in the title name, the output would be:
hello.txt
hello.txt
hello.txt
hello.txt
hello.txt

Thanks klint. Works great perfectly fine.
Was just wondering though, and I'm being extremely picky, but was there a simpler way to do it without naming each file hello1.txt, hello2.txt, but instead just calling them all hello.txt and letting the Windows automatic rename system rename the files accordinly as repeats of files are created e.g. hello.txt, hello(1).txt, hello(2).txt etc.something like:
@echo off
:loop
type nul > hello.txt
goto loop if count lss 10
I know that code doesn't work, but I hope you get my logic.If you can't do something like this, or if it is too difficult, don't worry about it. I appreciate your help.

The automatic renaming is done by the Windows Explorer - essentially just an application, and not Windows itself. In a batch file, you will have to emulate Explorer's logic yourself, perhaps using something like "if exist file.txt set filename=Copy(1) of file.txt".

Hi,
I am relatively newbie to Batch programming.My requirement is to read a set of n values from a text file and assign it to variables in the batch file.
i.e.
My text file has
One
Two
Three.We have to read this One from a batch and assign it to variable %%1 & TWO to %%2 and THREE to %%3 and so on...
Any pointers to solution will be greatly appreciated. I hope this to be a valid enough question to post in this forum...
Thank u.

That's a valid enough question, though I think it would have been better if you started a new topic rather than posting it as a reply to a previous question. Anyway, I guess there is a small amount of relevance to the original question so I'll answer it here.
To read a file, use something like the following command:
for /f "delims=" %%a in (file.txt) do (
echo Got this line: "%%a"
)Type for /? for a full description of the for command. Note there's a caveat, you can't read blank lines with batch files: the for command simply skips these.
Now, you say you want to assign each line to the variables %%1, %%2, %%3 etc. These are not legal variable names in batch files. I think you may be confusing two separate features:
1. The command-line parameters to a batch file or a batch subroutine are named %1, %2 etc. (just one % sign.)
2. When you use the for command, the index variable needs a double %% sign in a batch file, and a single % if you type the command directly at the prompt. (No-one ever said batch language was intuitive!)
I think, if you just want to put consecutive lines from a text file into a number of variables, you can do this:
@echo off
setlocal enabledelayedexpansion
set /a i=0
for /f "delims=" %%a in (file.txt) do (
set /a i+=1
set line!i!=%%a
)To understand how this works, get help on the relevant commands by typing this:
setlocal /?
set /?
for /?It's a lot to read, but you'll be almost an expert on most of the advanced features of batch language by the end of it!
To test the above batch file, add the following at the end:
for /l %%i in (1 1 9999) do (
if not defined line%%i goto :eof
echo Line %%i: "!line%%i!"
)Hope that helps.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |