How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?

前端 未结 28 2093
别跟我提以往
别跟我提以往 2020-11-21 05:28

Update: Now that it\'s 2016 I\'d use PowerShell for this unless there\'s a really compelling backwards-compatible reason for it, particularly because of the regional setting

28条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 06:00

    I had a similar problem. I have an automatic daily download from an FTP server of an encrypted file. I wanted to decrypt the file using gpg, rename the file to the current date (YYYYMMDD format) and drop the decrypted file into a folder for the correct department.

    I went through several suggestions for renaming the file according to date and was having no luck until I stumbled upon this simple solution.

    for /f "tokens=1-5 delims=/ " %%d in ("%date%") do rename "decrypted.txt" %%g-%%e-%%f.txt
    

    It worked perfectly (i.e., the filename comes out as "2011-06-14.txt").

    (Source)

提交回复
热议问题