0

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
  • 1
    To create a random file in a random folder you have to temporarily store the folder name, otherwise you don't know where to store the current random file. –  Apr 12 '18 at 11:13

2 Answers2

1

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!
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

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

baymax
  • 151
  • 3
  • 1
    What is with the random case? Why do `DIR`? why not pipe output of `copy nul !random! >nul` to eliminate unwanted output in console? and why generate 10 files per dir, as it is not what OP required? That being said, if you change all that, you basically have what I answered. – Gerhard Apr 12 '18 at 14:19