0

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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Itai Ganot
  • 5,873
  • 18
  • 56
  • 99
  • 1
    check this - http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us – npocmaka Apr 12 '17 at 08:10
  • Thanks, that's where I got the lines I'm using from, but in my case the example from that post returns for me the AM and in his answer it seems like it doesn't... – Itai Ganot Apr 12 '17 at 08:14
  • The idea is to get the time independent from time settings. `date /t` and `time /` will work different on different machines (btw. you can use `%date%` and `%time% variables`) – npocmaka Apr 12 '17 at 08:17

1 Answers1

0

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.

Compo
  • 36,585
  • 5
  • 27
  • 39