Page 1 of 1

getDate function

Posted: 29 Oct 2013 18:52
by carlos
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

Re: getDate function

Posted: 06 Nov 2013 13:10
by npocmaka_
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 )

Re: getDate function

Posted: 06 Nov 2013 15:35
by aGerman
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

Re: getDate function

Posted: 06 Nov 2013 16:28
by npocmaka_
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...

Re: getDate function

Posted: 07 Nov 2013 11:22
by carlos
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.

Re: getDate function

Posted: 08 Nov 2013 02:25
by carlos
@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

Re: getDate function

Posted: 08 Nov 2013 02:45
by npocmaka_
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 . . .

Re: getDate function

Posted: 08 Nov 2013 09:10
by carlos
Thanks I will update my clock codes with this improved.

Re: getDate function

Posted: 08 Nov 2013 09:17
by carlos
I do a minor change that is a fix: I forget surround with quotes tf variable in the penultimate line

Re: getDate function

Posted: 10 Dec 2013 08:01
by npocmaka_

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

Re: getDate function

Posted: 07 Jan 2014 15:21
by siberia-man
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

Re: getDate function

Posted: 08 Jan 2014 07:38
by mcnd
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.

Re: getDate function

Posted: 09 Jan 2014 02:36
by foxidrive
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