Link to home
Create AccountLog in
Avatar of Zack
ZackFlag for Australia

asked on

Batch file code meaning

Hi EE,


A query for a legacy coder :) what does the following mean in a batch file:



rem ** Set Variables **
set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
set mytime=%time:~0,2%%time:~3,2%%time:~6,2%
set /a myErrorCntr=0

rem ** Set Current Date/Time variables **
FOR /f "tokens=2-7 delims=.:/, " %%q IN ("%date% %time%") DO (
   set cDay=%%q&set cMnth=%%r&set cYear=%%s&set cHour=%%t&set cMin=%%u&set cSec=%%v
)


Can someone break down the functionality of this lingo in detail?


Thank you. 


ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Zack

ASKER

Hi ste5an,

Cheers for the info for anyone else here is detailed post on the subject. 

https://threemillion.net/blog/?p=196

Thank you
Avatar of Bill Prew
Bill Prew

And in case this is any help for the coder, here is a better way to accomplish this.  One of the problems with using either %DATE% and %TIME%, or the DATE and TIME commands, is that the output of those can vary depend on system locale settings, which makes the code break or return the wrong results sometimes.

A more reliable approach can be gotten by using WMI methods which always return the info in the same format.

Here's a sample BAT script you can share with the coder in case it's helpful.  The output from a test on my machine is below the script code.  Notice that the approach varies depending on if the individual date/time elements are desired to have leading zeros or not.  Questions welcome...

@echo off

rem ** Set Variables **
set mydate=%date:~10,4%%date:~7,2%%date:~4,2%
set mytime=%time:~0,2%%time:~3,2%%time:~6,2%
set /a myErrorCntr=0

rem ** Set Current Date/Time variables **
FOR /f "tokens=2-7 delims=.:/, " %%q IN ("%date% %time%") DO (
   set cDay=%%q&set cMnth=%%r&set cYear=%%s&set cHour=%%t&set cMin=%%u&set cSec=%%v
)

echo ***** ORIGINAL APPROACH *****
echo mydate=%mydate%
echo mytime=%mytime%
echo cDay=%cDay%
echo cMnth=%cMnth%
echo cYear=%cYear%
echo cHour=%cHour%
echo cMin=%cMin%
echo cSec=%cSec%

rem ===============================================================================

rem Get current date/time in YYYYMMDDhhmmss format
set Today=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined Today set Today=%%A
)

rem Break apart as needed
set mydate=%Today:~0,8%
set mytime=%Today:~8,6%
set cDay=%Today:~6,2%
set cMnth=%Today:~4,2%
set cYear=%Today:~0,4%
set cHour=%Today:~8,2%
set cMin=%Today:~10,2%
set cSec=%Today:~12,2%

echo ***** NEW APPROACH (WITH LEADING ZEROS) *****
echo mydate=%mydate%
echo mytime=%mytime%
echo cDay=%cDay%
echo cMnth=%cMnth%
echo cYear=%cYear%
echo cHour=%cHour%
echo cMin=%cMin%
echo cSec=%cSec%

rem ===============================================================================

rem Get current date/time in YYYYMMDDhhmmss format
set Today=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined Today set Today=%%A
)

rem Break apart as needed
set mydate=%Today:~0,8%
set mytime=%Today:~8,6%

rem Get current date items without leading zero pad and save to variables
for /f "skip=2 tokens=2-7 delims=," %%A in ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:csv') do (
  set cDay=%%A
  set cMnth=%%D
  set cYear=%%F
  set cHour=%%B
  set cMin=%%C
  set cSec=%%E
)

echo ***** NEW APPROACH (WITHOUT LEADING ZEROS) *****
echo mydate=%mydate%
echo mytime=%mytime%
echo cDay=%cDay%
echo cMnth=%cMnth%
echo cYear=%cYear%
echo cHour=%cHour%
echo cMin=%cMin%
echo cSec=%cSec%

Open in new window

***** ORIGINAL APPROACH *****
mydate=20211309
mytime=111303
cDay=09
cMnth=13
cYear=2021
cHour=11
cMin=13
cSec=03
***** NEW APPROACH (WITH LEADING ZEROS) *****
mydate=20210913
mytime=111303
cDay=13
cMnth=09
cYear=2021
cHour=11
cMin=13
cSec=03
***** NEW APPROACH (WITHOUT LEADING ZEROS) *****
mydate=20210913
mytime=111303
cDay=13
cMnth=9
cYear=2021
cHour=11
cMin=13
cSec=3

Open in new window


»bp