dżejson
dżejson

Reputation: 11

Create n files with random number value in loop batch

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.

Upvotes: 0

Views: 471

Answers (1)

Squashman
Squashman

Reputation: 14290

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

Upvotes: 1

Related Questions