Computing.Net > Forums > Programming > Simple Batch Loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Simple Batch Loop

Reply to Message Icon

Name: spillon
Date: June 30, 2008 at 21:03:38 Pacific
OS: XP/2000
CPU/Ram: N/A
Product: N/A
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: July 3, 2008 at 06:03:30 Pacific
Reply:

@echo off
for /l %%i in (1 1 10) do (type nul > Hello%%i.txt)


0

Response Number 2
Name: spillon
Date: July 5, 2008 at 06:16:53 Pacific
Reply:

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.


0

Response Number 3
Name: klint
Date: July 5, 2008 at 14:52:03 Pacific
Reply:

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".


0

Response Number 4
Name: GoldenEagle
Date: July 8, 2008 at 23:46:35 Pacific
Reply:


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.


0

Response Number 5
Name: klint
Date: July 9, 2008 at 03:02:10 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Simple Batch Loop

simple batch program www.computing.net/answers/programming/simple-batch-program/12122.html

Simple Batch Script www.computing.net/answers/programming/simple-batch-script/16428.html

Simple Batch file www.computing.net/answers/programming/simple-batch-file/10948.html