0

How can we create a zip file in UTC time or current time (-) minus 3 hours from the existing time.

The command which I use is below pasted:

echo on

for /f "tokens=3,2,4 delims=/- " %%x in ("%date%") do set d=%%z%%x_%%y
set date=%d%

for /f "tokens=1,2,3 delims=:. " %%x in ("%time%") do set t=%%x%%y%%z


set time=%t%

Echo zipping...

"C:\Program Files\7-Zip\7z.exe" a -tzip "C:\DFD\JZR_%d%%t%.zip" "C:\DFD\*.txt"

del "C:\DFD*.txt" /s /f /q

echo Done!

if anybody have idea, please help.

vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • It's [hard](https://stackoverflow.com/q/355425/503046), as batch is pretty limited. Can you use a modern shell, such as Powershell instead? – vonPryz Jul 27 '20 at 07:33

2 Answers2

0

Try this:

@echo off & for /f "usebackq tokens=* delims= " %%a in (`%windir%\system32\windowspowershell\v1.0\powershell.exe -noprofile -command "& {(Get-Date).ToUniversalTime().AddHours(-3).ToString('yyyyMM_dd')}`) do set "d=%%a"
for /f "usebackq tokens=* delims= " %%a in (`%windir%\system32\windowspowershell\v1.0\powershell.exe -noprofile -command "& {(Get-Date).ToUniversalTime().AddHours(-3).ToString('hhmmss')}"`) do set "t=%%a"
echo Zipping.... & "C:\Program Files\7-Zip\7z.exe" a -tzip "C:\DFD\JZR_%d%%t%.zip" "C:\DFD\*.txt" & del /q /f /s "C:\DFD*.txt" & echo Done!
  • Don't overwrite DATE and TIME they are environment variables.
  • Used inline powershell to get UTC date and time in your format 3 hours back.
Wasif
  • 14,755
  • 3
  • 14
  • 34
0

One possible solution to get UTC time is using WMIC:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1* delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_UTCTime GET /VALUE') do if not "%%J" == "" set "%%I=%%J"
rem Insert a leading 0 if one of the two digit values has just one digit.
if "%Month:~1%" == "" set "Month=0%Month%"
if "%Day:~1%" == "" set "Day=0%Day%"
if "%Hour:~1%" == "" set "Hour=0%Hour%"
if "%Minute:~1%" == "" set "Minute=0%Minute%"
if "%Second:~1%" == "" set "Second=0%Second%"
rem Use the date/time values in format yyyy-MM-dd_hh_mm_ss in ZIP file name.
"%ProgramFiles%\7-Zip\7z.exe" a -tzip "C:\DFD\JZR_%Year%-%Month%-%Day%_%Hour%_%Minute%_%Second%.zip" "C:\DFD\*.txt"
endlocal

Please run in a command prompt window once wmic path win32_utctime get /value to see what WMIC outputs in Unicode with UTF-16 LE encoding converted with a quirks by cmd.exe to ASCII encoding which requires the IF condition. See my answer on How to remove new lines in batch file? for details about the character encoding conversion problem worked around here with the IF condition.

It would be also possible to use as third line:

for /F "tokens=1* delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe PATH Win32_UTCTime GET Day^,Month^,Year^,Hour^,Minute^,Second /VALUE') do if not "%%J" == "" set "%%I=%%J"

Then just the six date/time values of interest would be output by WMIC whereby character ^ left to each comma is necessary to pass the command line with the commas correct to the command process started in background which executes the WMIC command, i.e. to execute in background:

C:\Windows\System32\cmd.exe /c C:\Windows\System32\wbem\wmic.exe PATH Win32_UTCTime GET Day,Month,Year,Hour,Minute,Second /VALUE

The commas would be otherwise interpreted by cmd.exe processing the batch file as argument separators inside set of command FOR and would replace each , by a space character which would make the WMIC command invalid.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic path /?
  • wmic path win32_utctime /?
  • wmic path win32_utctime get /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • thanks a lot for the detailed command script and that saved my time and successfully produced the result which I need. thanks again mofi for the support. – Haunted Eyez Jul 27 '20 at 14:40