Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Dave Webb's soution while a great one will of course only work on files in the same directory.</p> <p>Here is a solution that will work on any two files.</p> <p>First get the file time (see <a href="https://stackoverflow.com/questions/2111333/how-to-get-files-modifed-date-on-windows-dos-command">How to get file&#39;s last modified date on Windows command line?</a>).</p> <pre><code>for %%a in (MyFile1.txt) do set File1Date=%%~ta for %%a in (MyFile2.txt) do set File2Date=%%~ta </code></pre> <p>However one has then to manually break the date and time into it's components since Cmd.exe will compare them as a sting thus 2 > 10 and 10:00AM > 2:00PM.</p> <p>Compare first the years, then the months, then the day, then AM/PM, then the hour, and then the minute and second, (actually time consuming, but I don't have on the minute a better idea), see the final code at the end.</p> <p>However this solution will not work if the files are in the same minute but different by the second.</p> <p>If you are to this level of precision then get the filetime by using the "forfiles" command (see <a href="https://superuser.com/questions/91287/windows-7-file-properties-date-modified-how-do-you-show-seconds">https://superuser.com/questions/91287/windows-7-file-properties-date-modified-how-do-you-show-seconds</a>).</p> <pre><code>for /F "tokens=*" %%a in ('forfiles /m MyFile1.txt /c "cmd /c echo @fdate @ftime"') do set File1Date=%%a for /F "tokens=*" %%a in ('forfiles /m MyFile2.txt /c "cmd /c echo @fdate @ftime"') do set File2Date=%%a </code></pre> <p>Note that "ForFiles" has a limitation that it can't take a path with spaces, so if you have a path with spaces you will have to change to that directory first, see <a href="https://stackoverflow.com/questions/5164407/forfiles-spaces-in-folder-path">forfiles - spaces in folder path</a></p> <p><strong>Comparison Code</strong></p> <pre><code>:compareFileTime set "originalFileTime=%1" set "secondFileTime=%2" for /F "tokens=1,2,3 delims= " %%a in (%originalFileTime%) do ( set "originalDatePart=%%a" set "originalTimePart=%%b" set "originalAmPmPart=%%c" ) for /F "tokens=1,2,3 delims= " %%a in (%secondFileTime%) do ( set "secondDatePart=%%a" set "secondTimePart=%%b" set "secondAmPmPart=%%c" ) for /F "tokens=1,2,3 delims=/" %%a in ("%originalDatePart%") do ( set "originalMonthPart=%%a" set "originalMonthDayPart=%%b" set "originalYearPart=%%c" rem We need to ensure that the year is in a 4 digit format and if not we add 2000 to it rem Cmd considers "50" &gt; "100" but 50 &lt; 100, so don't surround it with qoutes if %%c LSS 100 set "originalYearPart=20%%c ) for /F "tokens=1,2,3 delims=/" %%a in ("%secondDatePart%") do ( set "secondMonthPart=%%a" set "secondMonthDayPart=%%b" set "secondYearPart=%%c" rem We need to ensure that the year is in a 4 digit format and if not we add 2000 to it rem Cmd considers "50" &gt; "100" but 50 &lt; 100, so don't surround it with quotes if %%c LSS 100 set "secondYearPart=20%%c ) if %originalYearPart% GTR %secondYearPart% goto newer if %originalYearPart% LSS %secondYearPart% goto older rem We reach here only if the year is identical rem Cmd considers "2" &gt; "10" but 2 &lt; 10, so don't surround it with quotes or you will have to set the width explicitly if %originalMonthPart% GTR %secondMonthPart% goto newer if %originalMonthPart% LSS %secondMonthPart% goto older if %originalMonthDayPart% GTR %secondMonthDayPart% goto newer if %originalMonthDayPart% LSS %secondMonthDayPart% goto older rem We reach here only if it is the same date if %originalAmPmPart% GTR %secondAmPmPart% goto newer if %originalAmPmPart% LSS %secondAmPmPart% goto older rem we reach here only if i=t is the same date, and also the same AM/PM for /F "tokens=1 delims=:" %%a in ("%originalTimePart%") do set "originalHourPart=%%a" for /F "tokens=1 delims=:" %%a in ("%secondTimePart%") do set "secondHourPart=%%a" rem Cmd considers "2" &gt; "10" but 2 &lt; 10, so don't surround it with qoutes or you will have to set the width explicitly if %originalHourPart% GTR %secondHourPart% goto newer if %originalHourPart% LSS %secondHourPart% goto older rem The minutes and seconds can be compared directly if %originalTimePart% GTR %secondTimePart% goto newer if %originalTimePart% LSS %secondTimePart% goto older if %originalTimePart% EQU %secondTimePart% goto same goto older exit /b :newer echo "newer" exit /b :older echo "older" exit /b :same echo "same" exit /b </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload