Create unique file name Windows batch

前端 未结 10 1469
闹比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:17

    The Batch-JScript hybrid script below uses WSH's fso.GetTempName() method that was designed precisely for this purpose:

    @if (@CodeSection == @Batch) @then
    
    @echo off
    
    for /F "delims=" %%a in ('cscript //nologo //E:JScript "%~F0"') do set "fileName=%%a"
    echo Created file: "%fileName%"
    goto :EOF
    
    @end
    
    var fso = new ActiveXObject("Scripting.FileSystemObject"), fileName;
    do { fileName = fso.GetTempName(); } while ( fso.FileExists(fileName) );
    fso.CreateTextFile(fileName).Close();
    WScript.Echo(fileName);
    

提交回复
热议问题