dir /b /OD file1.txt file2.txt | more +1
This only works if the files are in the same folder, and neither is hidden.
(Limitation of the Dir command)Compares Last Modified Date, but can easily
compare Created Date or Last Accessed Date using /TC or /TA.
To capture the filename in a batch file you can use a For loop:
Code:
For /F "Delims=" %%I In ('dir /b /OD file1.txt file2.txt ^| more +1') Do Set _Newer=%%I
You can use the /AH switch if both files are hidden, but it won't work if only
one is hidden. This will also do the same:
Code:
For /F "Skip=1 Delims=" %%I In ('dir /b /OD file1.txt file2.txt') Do Set _Newer=%%I
You can use Xcopy if the files are in the same or different folders, hidden or not,
but can only compare the Last Modified dates:
Code:
For /F "Delims=" %%I In ('xcopy /DHYL C:\Folder1\File1.txt D:\Backup\File2.txt ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If File1 is newer, _Newer is set to 1. If same or older date, it's set to 0.
If you need the file name instead of a 1/0 answer, takes one more line:
Code:
For /F "Delims=" %%I In ('xcopy /DHYL C:\Folder1\File1.txt D:\Backup\File2.txt ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If %_Newer==1 (Set _Newer=C:\Folder1\File1.txt) Else (Set _Newer=D:\Backup\File2.txt)
This will accept Fully qualified file names on the command line and output the
name of the newest file:
Code:
Set _File1=%1
Set _File2=%2
For /F "Delims=" %%I In ('xcopy /DHYL %_File1% %_File2% ^|Findstr /I "File"') Do set /a _Newer=%%I 2>Nul
If %_Newer%==1 (Set _Newer=%_File1%) Else (Set _Newer=%_File2%)
Echo The newest file is %_Newer%
If you need to compare the Created Date or Last Accessed Date for files in
different folders, you can move one of them, compare using the first option,
then move the file back. Not a good option for larger files, or files on a flash
drive. You'd have to parse the Date and time stamp from a Dir for each file,
convert it to a number format that batch can handle, and then compare
the timestamps. Doable, but much more involved using a batch file.
The GNU utilities would be much easier if you need to compare Created/Accessed
dates in different folders without moving either file. And it can do multiple
files, not just two
2.6.10
Batch File - Comparing dates of two files
Marcadores:
batch file
Subscribe to:
Post Comments (Atom)
0 comentários:
Post a Comment