:: UNIQUE.BAT -- CLD rev.9/24/16 :: Demo of batch-file technique for generating unique temp file names :: Usage: :: UNIQUE.BAT ["basename"] [number_of_temp_files_to_generate] :: E.g. UNIQUE.BAT "test" 10 :: ------------------------------------------------------------------ @echo off if not "%1"=="" goto :main cls echo This demo generates a stream of unique temporary filenames. echo Each filename includes a basename and the current date ^& time, echo followed by a pseudo-random number. echo. echo The default basename is "[basename]". echo You can specify a different basename (in quotes) on the command line: echo UNIQUE.BAT "basename" echo. echo The demo stops after generating 100 unique non-existent filenames. echo You can set a different number on the command line; e.g.: echo UNIQUE.BAT 10 echo UNIQUE.BAT 500 echo. echo The two parameters can be combined: echo UNIQUE.BAT "testname" 10 echo. pause :main echo. set _temp=X set basename=[basename] if not "%1"=="" ( if not "%~1"=="%1" ( set basename=%~1 shift /1 ) ) if not "%2"=="" shift /1 set /a _count=0 set /a _upper=100 if not "%1"=="" ( set _tst= for /f "delims=0123456789" %%a in ("%1") do set _tst=%%a if not defined _tst set /a _upper=%1 ) :A set _randno=%RANDOM% :B call :strLen _randno len_out if %len_out% LSS 5 set "_randno=0%_randno%" & goto :B for /f "tokens=2-8 delims=,-./: " %%a in ("%DATE%%TIME%%_randno%") do set tempfn=%basename%-%%a%%b%%c%%d%%e%%f%%g.tmp set /a "_count+=1" if not exist %tempfn% echo %_count%: %tempfn% if %tempfn%==%_temp% ( set /a "_count-=1" echo. echo Duplicate: %tempfn% pause goto :A ) if %_count% LSS %_upper% ( set _temp=%tempfn% goto :A ) else ( for %%a in (_count _randlen _randno _temp _tst basename tempfn) do if defined %%a set %%a= echo. echo Done. echo Press a key to quit... pause >nul goto :eof ) :: ----------------------------------------------- :strLen setlocal EnableDelayedExpansion :sA if not "!%1:~%len%!"=="" set /A len+=1 & goto :sA (endlocal & set %2=%len%) exit /b :: end strLen