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?
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.