小编典典

如何在批处理脚本中将日期和时间插入文件名?

jenkins

我为Jenkins作业编写了一个批处理脚本,该脚本会编译“ .net”代码,其中的步骤之一是在提取新的已编译代码之前备份当前目录。

我正在使用这些行来提取要插入备份文件名的日期和时间:

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看起来像这样:

01:00 AM

这导致文件名是:

bin-20170412-0100 AM.zip

这是一个问题。

我想从时间变量中忽略“ AM”,怎么办?


阅读 387

收藏
2020-07-25

共1个答案

小编典典

因为您显然对Powershell的使用没有问题,所以将前两行替换为:

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

然后,在变量中设置完整的归档文件名,以%archive%在以下命令中使用。

2020-07-25