0

how can I generate n files which includes random number using batch? I have:

@echo off
set /p howManyFiles=How many files to generate: 
FOR /L %%i IN (1, 1, %howManyFiles%) DO (
    set /a num=%random% %%100 +1
    echo !num! >"C:\xxx\file.txt"
)
pause

but it generates only 1 file.

1 Answer 1

1

Two problems with your code.

1) You are using the correct syntax for variables that require delayed expansion but you do not have delayed expansion enabled.

2) If you want to create multiple files then use the for variable with the file name.

@echo off
setlocal enabledelayedexpansion
set /p howManyFiles=How many files to generate: 
FOR /L %%i IN (1, 1, %howManyFiles%) DO (
    set /a num=!random! %% 100 + 1
    echo !num! >"C:\xxx\file%%i.txt"
)
pause
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah it is almost exactly what i wanted. Such one thing - in path need use %%i thanks :) +1

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.