views:

1857

answers:

3

I have a batch file which does something like this

for %%a in (1 2 3 4 5) do dir > %%a.output

%%a just gives me a .

How can I use the value of the variable %%a to assign the file name (e.g. 1.output, 2.output) ?

A: 

Your command syntax looks correct to me. I would expect that line of a batch file, as is, to produce these commands:

dir > 1.output
dir > 2.output
dir > 3.output
dir > 4.output
dir > 5.output

Which would in turn create 1.output, 2.output, etc.

As a debugging tip, you might try changing that line to something like this:

for %%a in (1 2 3 4 5) do echo dir ^> %%a.output

Note the ^, which is used to escape the > redirection.

Also:

  • Remember to use %%a in batch files but %a at the command line.
  • Remember that for variables are case-sensitive; %%A is different from %%a.

Update

It's been a long time since I had to get a batch file working under such an old OS version, but I wouldn't be surprised if redirection was incompatible with for back in the day.

You could try this:

for %%a in (1 2 3 4 5) do call helper.bat %%a

In helper.bat:

dir > %1.output

Or, if you don't like an extra batch file, combine them into one file:

if .%1==.sub goto do_sub
for %%a in (1 2 3 4 5) do call %0 sub %%a
goto end
:sub
shift
dir > %1.output
:end
system PAUSE
No it does not work. I get the output %a.out .
vivekian2
A: 

I don't have a Win98 system at my disposal, but enter for /? in a DOS box and see if there's something there that can help you.

The way to do this in NT-based Windows OS is:

FOR /L %variable IN (start,step,end) DO command [command-parameters]

So your statement would be something like:

FOR /L %%a IN (1,1,5) DO echo dir ^> %%a.output

Perhaps there's something similar in DOS under Win98.

Patrick Cuff
+1  A: 

for %a in (1 2 3 4 5) do dir > %a%.output

You have to surround your variable.

aintnoprophet