getDate function

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlos
Expert
Posts: 501
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

getDate function

#1 Post by carlos » 29 Oct 2013 18:52

Hello, using my getDate function writing clocks in this thread: http://www.dostips.com/forum/viewtopic.php?f=3&t=5028 I found that the function needed improves. Then I left here the last version that can be useful for someone:

Here the last improved version: http://www.dostips.com/forum/viewtopic.php?p=29758#p29758

getdate.cmd

Code: Select all

:getdate
set /a "jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12"
set /a "mon=1,tue=2,wed=3,thu=4,fri=5,sat=6,sun=7"
set "tf=%temp%\~%random%"
copy /y nul "%tf%.foo" >nul 2>&1
if errorlevel 1 goto :getdate
makecab /l "%temp%" /d rptfilename="%tf%.rpt" /d inffilename=nul /f "%tf%.foo" >nul 2>&1
if errorlevel 1 goto :getdate
set /p "timestamp=" <"%tf%.rpt" >nul 2>&1
if errorlevel 1 goto :getdate
for /f "tokens=3-9 delims=: " %%a in ("%timestamp%") do (
set /a "year=%%g,month=%%b,day=1%%c-100,weekday=%%a"
set /a "hour=1%%d-100,minute=1%%e-100,second=1%%f-100"
)
for %%a in ("%tf%.rpt","%tf%.foo") do if exist "%%~a" del "%%~a" 2>nul
goto :eof
Last edited by carlos on 08 Nov 2013 10:46, edited 2 times in total.

npocmaka_
Posts: 493
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: getDate function

#2 Post by npocmaka_ » 06 Nov 2013 13:10

you can reduce file operations even more:

Code: Select all

makecab /D RptFileName="%tf%.rpt" /D GenerateInf=off /F nul /V0 >nul


You can exclude inf generation file with /D GenerateInf=off (and will not need to output in nul) and for directive file you can use nul straightforward.

It's not mentioned in the help but seems that 0 is a valid level for verbosity and prints less things in the console (try without >nul at the end)

more info here: http://msdn.microsoft.com/en-us/library ... usersguide

(makecab is more or less a separate script language that stayed hidden from me so far :-D )

aGerman
Expert
Posts: 3869
Joined: 22 Jan 2010 18:01
Location: Germany

Re: getDate function

#3 Post by aGerman » 06 Nov 2013 15:35

GenerateInf=off doesn't suppress the generation of an inf file. It only switches to another output mode. The /F nul is a nice proposal though.

Regards
aGerman

npocmaka_
Posts: 493
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: getDate function

#4 Post by npocmaka_ » 06 Nov 2013 16:28

hmmm. May be you are right
with this I've succeeded to reduce time from 0.08 to 0.06 secs (few attepmpts):

Code: Select all

makecab /D RptFileName="%tf%.rpt" /D GenerateInf=off /d "InfHeader=" /d "InfDiskHeader=" /d "InfFooter=" /d "InfFileHeader=" /d "InfCabinetHeader=" /d "Compress=off" /d "Cabinet=off" /d "InfDiskLineFormat=" /d DoNotCopyFiles=on /d GenerateInf=off /F nul /V0


May be with few more parameters the time could be reduced more...

carlos
Expert
Posts: 501
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: getDate function

#5 Post by carlos » 07 Nov 2013 11:22

Thanks for the research npocmaka. I remember that /f nul works on vista but not in xp. I tried to optimize more the function reading the manual of makecab and your ideas.

carlos
Expert
Posts: 501
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: getDate function

#6 Post by carlos » 08 Nov 2013 02:25

@npocmaka: I get a new improved version of getdate:
code updated

@npocmaka: InfSectionOrder="" desactivates InfCabinetHeader InfDiskHeader InfFileHeader

Now I get the date from the inf file and not the rpt file. using the directives I only write 1 line of text to the inf file.

@npocmaka: thanks for indicates the DoNotCopyFiles variable that speedy the inf creation.

Please someone can test it in windows vista or above ?
I test succesfully in windows xp.

Code: Select all

@Echo Off


Call :GetDate.Init
Rem :GetDate.Init should be called one time in the code before call to :Getdate
Call :GetDate

Echo year:%year%
Echo month:%month%
Echo day:%day%
Echo weekday:%weekday%
Echo hour:%hour%
Echo minute:%minute%
Echo second:%second%

Pause
Goto :EOF

:GetDate.Init
Set /A "jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12"
Set /A "mon=1,tue=2,wed=3,thu=4,fri=5,sat=6,sun=7"
(
Echo .Set InfHeader=""
Echo .Set InfSectionOrder=""
Echo .Set InfFooter="%%2"
For /L %%# In (1,1,4) Do Echo .Set InfFooter%%#=""
Echo .Set Cabinet="OFF"
Echo .Set Compress="OFF"
Echo .Set DoNotCopyFiles="ON"
Echo .Set RptFileName="NUL"
) >"%Temp%\~foo.ddf"
Goto :Eof

:GetDate
Set "tf=%Temp%\~%random%"
Makecab /D InfFileName="%tf%" /F "%Temp%\~foo.ddf" >NUL
For /F "usebackq tokens=1-7 delims=: " %%a In ("%tf%") Do (
Set /A "year=%%g,month=%%b,day=1%%c-100,weekday=%%a"
Set /A "hour=1%%d-100,minute=1%%e-100,second=1%%f-100")
Del "%tf%" >NUL 2>&1
Goto :Eof
Last edited by carlos on 07 Jan 2014 21:52, edited 4 times in total.

npocmaka_
Posts: 493
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: getDate function

#7 Post by npocmaka_ » 08 Nov 2013 02:45

output on Win7 (you can the file itself as a directive but need to comment batch part with ;):

getTime wrote:C:\>datef.bat
year:2013
month:11
day:8
weekday:5
hour:10
minute:44
second:17
Press any key to continue . . .
Last edited by npocmaka_ on 08 Nov 2013 09:32, edited 1 time in total.

carlos
Expert
Posts: 501
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: getDate function

#8 Post by carlos » 08 Nov 2013 09:10

Thanks I will update my clock codes with this improved.

carlos
Expert
Posts: 501
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: getDate function

#9 Post by carlos » 08 Nov 2013 09:17

I do a minor change that is a fix: I forget surround with quotes tf variable in the penultimate line

npocmaka_
Posts: 493
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: getDate function

#10 Post by npocmaka_ » 10 Dec 2013 08:01

Code: Select all

;@Echo Off

;Call :GetDate.Init
;Rem :GetDate.Init should be called one time in the code before call to :Getdate
;Call :GetDate

;Echo year:%year%
;Echo month:%month%
;Echo day:%day%
;Echo weekday:%weekday%
;Echo hour:%hour%
;Echo minute:%minute%
;Echo second:%second%

;Pause
;Goto :EOF

;:GetDate
;Set "tf=%Temp%\~%random%"
; Makecab /D InfFileName="%tf%" /F "%~dpfnxs0"  >NUL
;For /F "usebackq tokens=1-7 delims=: " %%a In ("%tf%") Do (
;Set /A "year=%%g,month=%%b,day=1%%c-100,weekday=%%a"
;Set /A "hour=1%%d-100,minute=1%%e-100,second=1%%f-100")
;Del "%tf%" >NUL 2>&1
;Goto :Eof

;:GetDate.Init
;Set /A "jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12"
;Set /A "mon=1,tue=2,wed=3,thu=4,fri=5,sat=6,sun=7"
;Goto :Eof
.Set InfHeader=""
.Set InfSectionOrder=""
.Set InfFooter="%2"
.Set InfFooter1=""
.Set InfFooter2=""
.Set InfFooter3=""
.Set InfFooter4=""
.Set Cabinet="OFF"
.Set Compress="OFF"
.Set DoNotCopyFiles="ON"
.Set RptFileName="NUL"


One temporary file less.But I'm not sure if this is an improvement

siberia-man
Posts: 153
Joined: 26 Dec 2013 09:28
Contact:

Re: getDate function

#11 Post by siberia-man » 07 Jan 2014 15:21

Glad to meet again the solution well known for long time. I have tested it on Win XP/Vista/7 and it works fine with the following tiny improvement:

Code: Select all

set "rptfile=/specify/the/full/name/to/the/temp/file"

for /f "tokens=3,4,5,6,7,8,9 delims=: " %%1 in ( '
   makecab /d RptFileName^="%rptfile%" /d InfFileName^=nul /f nul ^>nul ^
   ^& type "%rptfile%" ^
   ^& del "%rptfile%"
' ) do (
   rem do a job with this
   rem MakeCAB Report: Wed Jan 08 00:21:35 2014
   rem                 1   2   3  4  5  6  7

   rem do jump to end of job (goto label or exit a function)
   goto endloop
)
:endloop

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: getDate function

#12 Post by mcnd » 08 Jan 2014 07:38

Just for an alternative for the same code, without temporary files (and no, not on a clean XP, sorry) robocopy can be used

Code: Select all

@echo off

   setlocal enableextensions disabledelayedexpansion

   call :getDateTime

   echo year    :%year%
   echo month   :%month%
   echo day     :%day%
   echo weekday :%weekday%
   echo hour    :%hour%
   echo minute  :%minute%
   echo second  :%second%
   
   endlocal
   exit /b

:getDateTime
   set "year="
    for /f "tokens=2-8 delims=: " %%a In ('robocopy ^| findstr /r /c:" [0-9][0-9][0-9][0-9]$"') Do (
      if not defined year (
         if not defined jan (
            set /a "jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12"
            set /a "mon=1,tue=2,wed=3,thu=4,fri=5,sat=6,sun=7"
         )
         set /a "year=%%g, month=%%b, day=1%%c-100, weekday=%%a"
         set /a "hour=1%%d-100, minute=1%%e-100, second=1%%f-100"
      )
   )

   goto :eof


EDITED - Up to windows 7, robocopy date string was not localized. I've had the option to see robocopy running in windows 8.1 and the date has been localized and omits one space between seconds and year (in a spanish copy). So, sorry, i keep the post for reference, but it seems it is not a reliable option.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: getDate function

#13 Post by foxidrive » 09 Jan 2014 02:36

It does fail totally in Windows 8.1

This is the string I see here in Robocopy:

Code: Select all

  Started : Thursday, 9 January 2014 19:35:16

Post Reply