Create unique file name Windows batch

前端 未结 10 1470
闹比i
闹比i 2020-12-16 15:57

I have seen many posts about creating a unique filename from the naive %TIME% to the plausible (but insufficient) %RANDOM%. Using wmic os get localdatetime is m

10条回答
  •  误落风尘
    2020-12-16 16:38

    @echo off
    :: generate a tempfilename in %1 (default TEMPFILE)
    :loop
    set /a y$$=%random%+100000
    set y$$=temp%y$$:~1,2%.%y$$:~-3%
    if exist "%temp%\%y$$%" goto loop
    SET "y$$=%temp%\%y$$%"© nul "%temp%\%y$$%" >nul 2>NUL
    :: y$$ now has full tempfile name
    if "%1"=="" (set "tempfile=%y$$%") else (set "%1=%y$$%")
    

    Here's a cut-down version of my tempfile generator to create a file named tempnn.nnn in the %temp% directory.

    Once tempnn.nnn has been created, then it's simple to create as many further tempfiles as you like for the process by appending a suffix to %y$$%, eg %y$$%.a etc. Of course, that presumes that some other process doesn't randomly create filenames without using this procedure.

提交回复
热议问题