0

I have a batch file and it contains the following code:

@echo off
color 0e
:initialize
set filemaker=1
echo How many files?
echo.
set /p uservar=
echo Creating...
for /l %%x in (1, 1, %uservar%) do echo. 2>%CD%\%filemaker%.txt & set /a filemaker=%filemaker%+1 & timeout /t 1 /nobreak >nul
echo %filemaker%
pause

And everytime I execute it, the file that are generated (regardless of the value of %uservar%) is ALWAYS name 2, and there is always ony one file! I would like multiple with different names like, file 1 is name 1, file 2 is name 2, and so on.

Any help is appreciated!

1 Answer 1

1

You need delayed expansion

@echo off
color 0e
:initialize
set filemaker=1
echo How many files?
echo.
set /p uservar=
echo Creating...
SetLocal EnableDelayedExpansion
for /l %%x in (1, 1, %uservar%) do ( 
    echo. 2>%CD%\!filemaker!.txt 
    set /a filemaker=filemaker+1 
    timeout /t 1 /nobreak >nul
)
echo %filemaker%

pause

for more info check these links:

http://www.robvanderwoude.com/variableexpansion.php

http://ss64.com/nt/delayedexpansion.html

http://blogs.msdn.com/b/oldnewthing/archive/2006/08/23/714650.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.