|
Answer» Okay, let's see if someone has a way for this:
To begin with, in this example I can take the time variable and extract just the hour digits by doing the following:
Echo %time:~0,2%
I want to be able to do exactly that, but for the timestamp on a particular file. I've written a program (below) that compares the timestamp of two different files in different folders, but I'm not sure how I can extract just a few characters from those VARIABLES.
For /f "delims=" %%a in ('dir /b /a-d "%FirstDir%\*.TXT"') do ( For /f "delims=" %%b in ("%FirstDIR%\%%a") do ( For /f "delims=" %%c in ("%SecondDir%\%%a") do ( Echo Visual Comparison: %%a \ %%~tb \ %%~tc If {"%%~tb"} EQU {"%%~tc"} Echo It's the same! If {"%%~tb"} NEQ {"%%~tc"} Echo It's not the same! Pause ) ) ) Goto :EOF
The reason I'm doing this is although I can TELL if they have the same timestamp or not, I can't tell if one files' timestamp is a later time/date than the other. The variables are, of course, the %%~tb and %%~tc. The above program goes through several files in two different directories.
Can anyone help me with this DILEMA? Thanks.Simple batch file to illustrate. For you to modify and play with.
Note: in my locale (Europe) file date stamp format is DD/MM/YYYY HH:MM thus: 11/02/2008 21:09
Your system date / time format may well be different eg. US date format, AM/PM in time etc
but if you study below you will see how to slice up timestamp & create date number which can be compared arithmetically. Shows use of set /a to create arithmetic (numerical) variable.
and also see how to use delayed expansion (! instead of %) in a loop, which you need because without it, regular non-loop variables are expanded only at runtime.
Of course if testdate variable was the product of another loop outside this one you would be using !testdate!...
echo off setlocal enabledelayedexpansion set /a testdate=19991231 for /f "delims==" %%F in ('dir /b /a-d') do ( echo Filename : %%F Time stamp: %%~tF set timestamp=%%~tF set mm=!timestamp:~0,2! set dy=!timestamp:~3,2! set yr=!timestamp:~6,4! set /a datenumber=!yr!!mm!!dy! echo month= !mm! day= !dy! year= !yr! number= !datenumber! if !datenumber! GTR %testdate% echo file date is later than %testdate% if !datenumber! EQU %testdate% echo file date is same as %testdate% if !datenumber! LSS %testdate% echo file date is earlier than %testdate% echo. )
output...
Filename : back.cmd Time stamp: 13/09/2007 20:21 month= 13 day= 09 year= 2007 number= 20071309 file date is later than 19991231
Filename : inf8.bat Time stamp: 11/02/2008 07:59 month= 11 day= 02 year= 2008 number= 20081102 file date is later than 19991231
Filename : tsta.bat Time stamp: 11/02/2008 21:09 month= 11 day= 02 year= 2008 number= 20081102 file date is later than 19991231
Filename : Arce.doc Time stamp: 09/07/1991 04:08 month= 09 day= 07 year= 1991 number= 19910907 file date is earlier than 19991231
|