Batch which create a txt with random name.

January 14, 2010 at 07:19:34
Specs: Windows XP, E6850, 2 Gb.
Hi there,
can you help me create a batch file, which creates a txt file with the random file name (preliminary checking whether it is not in the folder of file with such name).
P.S.: probability of coincidence of the names with the already created files must be minimum.

See More: Batch which create a txt with random name.

Report •

January 14, 2010 at 14:54:32
this will attempt to provide the names, but you did not specify what length restriction, if any, and what charset restriction if any, and up to you to provide the content of files. you can make changes as needed per length and charset.

@echo off & setlocal enabledelayedexpansion
:max length of name, extension respectively, using 8 dot 3 for this
set /a max1=8
set /a max2=3
:allow zero-length extension (mod 4 gets 0-3)
set /a max2+=1

:: a switch to allow lowercase or not: disable this line for no lowercase
:set lowers=abcdefghijklmnopqrstuvwxyz

:: char.set (varies dep. on application. this one is set up for filenames)
::personally i don't like using any but the most basic characters,
::(the ones allowed by dos), but the more chars in the set, the
:: less likely to get a repeated name. foll. 2 lines are ONE line!
set asc=!#$':;,%%@+-)(0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_{}%lowers%~

:find length of charset
for /L %%a in (1 1 127) do (
set mm=!asc:~%%a,1!
set lc=%%a
if "!mm!"=="%" set uu=1
if "!mm!"=="" goto :xx

set /a lc-=uu
echo.charset: %asc%
echo.length of charset: %lc%

set /p hm=how many to generate:
for /L %%z in (1 1 %hm%) do call :ff
goto :eof

set word=
set /a len="%random%%%max1"+1
for /L %%b in (1 1 !len!) do call :aa
set word=!word!.
set /a len="%random%%%max2"
for /L %%b in (1 1 !len!) do call :aa
if exist !word! goto :ff
echo the final word is:!word!
>>!word! (echo your content here)
goto :eof

set /a xx="%random%%%%lc%"
set word=!word!!asc:~%xx%,1!

Report •

January 14, 2010 at 17:47:19
Change the 10 to the number of files you want to generate.

@echo off & setLocal EnableDELAYedeXpansion

for /L %%i in (1 1 10) do (
set /a F=!random!%%999999999
set /a G=!random!%%999999999
if not exist !F!!G! type nul > !F!!G!

Helping others achieve escape felicity


Report •

January 14, 2010 at 20:22:06
why do i always do things the hard way? I should be working for Microsoft, then i could be making things difficult for you people! LL

Report •

Related Solutions

January 15, 2010 at 00:29:01
Mechanix2Go, nbrane.
Thank you.

Report •

Ask Question