Have you ever come across the need to log the date and time a file was created only to discover dir doesn't do it?
I did. I spent a couple of days frustratingly trying to find a way to capture the information with little luck. Finally I found documentation in the call command that explained that "%~t1" expands %1 to date/time of file. This gave me enough information that I could come up with the following script:
@Echo Off
Call :FileTimeDateSize "MyDate" "MyTime" "MySize" "%WinDir%\notepad.exe"
@Echo:%MyDate%
@Echo:%MyTime%
@Echo:%MySize%
GoTo :EOF
:FileTimeDateSize
Call :AsignDateTimeSize "%~1" "%~2" "%~3" %~t4 "%~z4"
GoTo :EOF
:AssignDateTimeSize
Set %~1=%~4 & Set %~2=%~5 %~6 & Set %~3=%~7 bytes
GoTo :EOF
I was able to extract the date and time using "%~t3" but I wanted the date separated from the time so I wrote the second function to split out the date and the time by feeding "%~t3 to the function :AsignDateTime without quotes so that it will be split out into three tokens which are assigned independently to the desired variables to be used later.
If you run the script you can see that it is working because the date Notepad was built appears on one line and then the time on the next. I have also expanded this so that it reports the file size as well.
Now all I have to do to log the date and time so I can parse it later is @Echo:%MyDate%,%MyTime%,%MySize%>>LogFile.log
No comments:
Post a Comment