如何在批处理脚本的文件名中插入日期和时间? [英] How to insert date and time to a filename in a batch script?
问题描述
我为Jenkins作业编写了一个批处理脚本,该脚本编译了一个".net"代码,其中一个步骤是在提取新的已编译代码之前备份当前目录.
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
问题出在time /t
的输出中,它看起来像这样:
The problem is with the output of time /t
which looks like that:
01:00 AM
这会导致文件名是:
bin-20170412-0100 AM.zip
这是个问题.
我想从时间变量中忽略"AM",怎么办?
I want to omit the " AM" from the time variable, how can it be done?
推荐答案
由于您显然对powershell的使用没有问题,因此请用以下两行代替:
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"
然后,在变量%archive%
中设置完整的存档文件名,以便在以下命令中使用.
You then have your full archive file name set in the variable %archive%
for use in your following commands.
这篇关于如何在批处理脚本的文件名中插入日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!