Question

I'm trying to compare a list of files with a control file on their modified timestamp using

dir /b /tw /od controlfile olderfile | more +1

this should return the controlfile as the modified date on olderfile is earlier than controlfile. However it appears to be comparing on creation date (olderfile's creation date is after controlfile's) and so will always return olderfile. The /tw switch should tell dir to use last written date (from dir /?) so I'm not sure what I'm doing wrong if I want /tw to work as documented?

:UPDATE:

Up to now I had olderfile in a subfolder relative to controlfile. When I copied olderfile to the same directory as controlfile the comparison worked as expected - i.e. the above command returned controlfile. So being in a subfolder obviously breaks the comparison (even when the subfolder modified date is like that of olderfile's itself earlier than that of controlfile).

So my question now is how do I workaround this given that the directory structure is required?

Was it helpful?

Solution

First an explanation of why your code is failing.

Looking at DIR without the /B option, it always displays the file path as a header line, never together with the file name. When using the sort option along with 2 file specifications within the same directory, the command is able to sort the files and display them under the same directory header. Even if the file name is repeated, you know exactly what file is being referred to by the header.

But if the 2 file specs are in different paths, there is a chance that the file name is the same, and only the paths differ. In this case the sort does no good unless each file spec is sorted under its own header; otherwise you couldn't tell which file was which. It doesn't make much sense to sort a single file, but remember that the file spec could include wildcards, so it makes sense. So whenever the sort option is combined with multiple file specs using different paths, DIR will do a separate sort for each path, each with its own header.

Adding the /B option removes the path headers, but it doesn't change the sort behavior! Absolutely a silly and useless design, but that is what you get.

Arriving at a solution is much more problematic, especially if you want to use pure batch with only native Windows commands. Working with dates can be infuriating with batch.

The most expedient batch solution I can come up with is to temporarily copy the 2nd file into the parent directory, run your comparison within the parent directory, and then del the temporary copy. But that could be a problem if the file is large. It also assumes the file name does not already exist within the parent directory. If it does (or might) exist, you could copy the file to different name, known to be unique. Of course you would then have to translate the result.

From what I can tell, any other batch solution will require parsing the date and time. That opens up a whole can of worms. (It is doable)

If the COPY suggestion isn't acceptable (I wouldn't blame you if you decide not to use it) then I recomend using something other than batch, perhaps VBScript, Javascript, or Powershell. Or perhaps a hybrid approach like batch with some VBScript thrown in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top