flamebull5
flamebull5

Reputation: 17

Batch File - Creating multiple files with different names

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!

Upvotes: 0

Views: 70

Answers (1)

npocmaka
npocmaka

Reputation: 57252

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

Upvotes: 1

Related Questions