Create unique file name Windows batch

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

    I like Aacini's JScript hybrid solution. As long as we're borrowing from other runtime environments, how about using .NET's System.GUID with PowerShell?

    @echo off
    setlocal
    
    for /f "delims=" %%I in ('powershell -command "[string][guid]::NewGuid()"') do (
        set "unique_id=%%I"
    )
    
    echo %unique_id%
    
    0 讨论(0)
  • 2020-12-16 16:13

    you can try with guid.bat

    for /f "tokens=* delims={}" %%# in ('guid.bat') do set "unique=%%#"
    echo %unique%
    

    for usage directly from console:

    for /f "tokens=* delims={}" %# in ('guid.bat') do @set "unique=%#"
    
    0 讨论(0)
  • 2020-12-16 16:14

    I would like to submit another method and find out if there are any holes is it. Please let me know. Thanks.

    C:>title my shell a80en333xyb
    
    C:>type getppid.ps1
    (Get-WmiObject -Class Win32_Process -Filter "processid='$pid'").ParentProcessId
    
    C:>powershell getppid.ps1
    2380
    

    Or, to run it without creating a .ps1 file:

    powershell -NoProfile -Command "(Get-WmiObject -Class Win32_Process -Filter "processid=`'$pid`'").ParentProcessId"
    

    To verify that the correct task is found, the title is set to an unlikely value and tasklist.exe is used to search for the value returned by getppid.ps1.

    C:>tasklist /v | find "2380"
    cmd.exe                       2380 RDP-Tcp#0                  7      8,124 K Running         PHSNT\pwatson                                           0:00:03 my shell a80en333xyb
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-16 16:21

    You could use certutil to base64 encode %date% %time% with a %random% seed like this:

    @echo off
    setlocal
    
    :: generate unique ID string
    >"%temp%\~%~n0.%username%.a" echo %username%%date%%time%%random%
    >NUL certutil -encode "%temp%\~%~n0.%username%.a" "%temp%\~%~n0.%username%.b"
    for /f "usebackq EOL=- delims==" %%I in ("%temp%\~%~n0.%username%.b") do set "unique_id=%%I"
    del "%temp%\~%~n0.%username%.a" "%temp%\~%~n0.%username%.b"
    
    echo %unique_id%
    

    In case the same script is being run from the same directory by multiple users, I added %username% to the temp files to avoid further conflict. I suppose you could replace %random% with %username% for the same effect. Then you'd only get a conflict if a single user executes the same code block twice concurrently.

    (Edit: added %username% as a seed for uniqueness.)

    0 讨论(0)
  • 2020-12-16 16:22

    make the file contents an object, use the pointer memaddress as the first part of your file name and a Rand() as your second part. the memory address will be unique for all objects even with multiple instances running.

    0 讨论(0)
提交回复
热议问题