Quote:
Originally Posted by ghostdog74 this finds the latest Code: 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. Great suggestion ghostdog74
For the Awk script,
test.awk should be
myscript.awk.
Using Dir, the files have to be in the same folder and you can't mix hidden and non-hidden files.
You can put the filenames into a text file and type that to the gawk script and that will work for any folder/hidden file combination, and can easily check
created/
accessed dates instead of modified by changing the %Y in myscript.awk to
%Z and
%X Code:
Echo C:\File1.ext>filelist.txt
Echo D:\File2.ext>>filelist.txt
Type filelist.txt |gawk -f myscript.awk
Downside is you need to download both gawk and the file utilities, and neither one adds itself to the path statement, so you have to do that manually, or specify the complete path to both
gawk and
stat. Once installed though, you can use them in a For loop just like Dir or Xcopy to capture the file name.