How to insert date and time to a filename in a batch script? [duplicate]

我与影子孤独终老i 提交于 2019-12-02 15:23:49

问题


I wrote a batch script for a Jenkins jobs which compiles a ".net" code and one of the steps there is to back up the current directory before extracting the new compiled code.

I'm using the these lines to extract the date and time which I want to insert into the backup file name:

for /F "tokens=2-4 delims=/ " %%i in ('date /t') do set date=%%k%%i%%j
for /f "tokens=1-2 delims=/:" %%a in ('time /t') do set time=%%a%%b
powershell.exe -nologo -noprofile -command "& { Add-Type -A System.IO.Compression.FileSystem; [IO.Compression.ZipFile]::CreateFromDirectory('bin', 'bin-%date%_%time%.zip'); }"
xcopy bin-%date%_%time%.zip c:\temp

The problem is with the output of time /t which looks like that:

01:00 AM

And that causes the filename to be:

bin-20170412-0100 AM.zip

which is a problem.

I want to omit the " AM" from the time variable, how can it be done?


回答1:


Because you obviously have no issue with powershell usage, replace those first two lines with:

For /F %%A In ('Powershell -C "Get-Date -Format yyyyMMdd_HHmm"'
) Do Set "archive=bin-%%A.zip"

You then have your full archive file name set in the variable %archive% for use in your following commands.



来源:https://stackoverflow.com/questions/43363804/how-to-insert-date-and-time-to-a-filename-in-a-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!