Batcher Bat
Batcher Bat

Reputation: 9

Random file creater, Batch-file

Im trying to make a batch-file that creates random directories and create files in the folders. But I am unable to make it work.

This is what I have tried:

 @echo off  
 cd "%userprofile%\desktop\Safe_Space"  
 md "%random%"  
 md "%random%"  
 md "%random%"  
 cd "*"  
 copy /Y NUL %random%.txt >NUL

Upvotes: 0

Views: 816

Answers (2)

baymax
baymax

Reputation: 159

You can create directories and files by using FOR, RANDOM and basic instructions. RANDOM create a random number between 0 and 32767. You can control the range of RANDOM using bottomlimit and upperlimit, e.g. 1-1000:

SET /a bottomlimit = 1
SET /a upperlimit = 1000

Don't forget to use SETLOCAL ENABLEDELAYEDEXPANSION (check How do SETLOCAL and ENABLEDELAYEDEXPANSION work?) and inside the loop the !VARIABLE! notation instead of %VARIABLE%. The following example creates 5 directories and 10 empty files. You can change these values as you like, using
FOR /l %%i in (1,1,10) instead of FOR /l %%i in (1,1,2) creates 2 directories. FOR /l %%i in (1,1,10) means a loop starts at 1, steps by 1, and finishes at 10.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

REM creating 5 directories...
FOR /l %%i in (1,1,5) DO (
    SET dirName=!RANDOM!
    REM create a directory with random name
    MKDIR !dirName!     
    CD !dirName!

    REM creating 10 files...
    FOR /l %%i in (1,1,10) DO (     
        REM create an empty file with random name
        COPY NUL !RANDOM!>NUL   
    )

    CD ..
)

The instruction COPY NUL !RANDOM!>NUL doesn't require administation privileges

the >NUL eliminates unwanted output to console

Upvotes: 0

user7818749
user7818749

Reputation:

This will create 10 folders with random names and 1 file within each folder with random names.

This will create a completely empty file in the created directories:

@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,10) do (
    set tempf=!random!
    mkdir !tempf!
    copy /y NUL !tempf!\!random! >NUL
)

Increasing/Decreasing the 10 in for /l %%i in (1,1,10) do ( will increased the number of folders and files. To add more files to folders, repeat echo nul > %random.txt or simply create another loop to create multiple files in the folders.

fsutil is a another option, but requires admin privileges, it will create a nul variable in the file.

@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,10) do (
    set tempf=!random!
    mkdir !tempf!
    fsutil file createnew !tempf!\!random! 1
)

This creates a new file, with some text, in this case the word nul will be written to file, but you can change that:

@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,10) do (
    set tempf=!random!
    mkdir !tempf!
    echo nul > !tempf!\!random!
)

Upvotes: 1

Related Questions