Tuesday, April 14, 2009

Create unique temp filename with Batch script

For Windows batch scripts, you could use %RANDOM% to generate a random number and use that as part of the temp file name but that doesn't guarantee uniqueness since random number can be repeated. By adding another variable to the mix, seconds and fraction of seconds, the file name would be unique:



call :GETTEMPNAME
echo "Temp file name is %TMPFILE%"
goto :EOF

:GETTEMPNAME
set TMPFILE=%TMP%\mytempfile-%RANDOM%-%TIME:~6,5%.tmp
if exist "%TMPFILE%" GOTO :GETTEMPNAME

:EOF

Post a Comment