0

I'm trying to build a batch script that will iterate over a set of folders and give me the most recently modified file (and later check if the date of that file is 180 days old). Right now, what I'm working with is this:

setlocal EnableDelayedExpansion

for /f %%f in ('dir /b /s /od /tw "<some UNC path>\*.*"') do (

REM Make sure the variable is defined the first time we try to compare it
if NOT DEFINED fileDateTime set fileDateTime=%%~tf

if %%~tf LEQ !fileDateTime! (
 set fileDateTime=%%~tf
 set fileName=%%nxf
 set filePath=\\%%~pf
))

This does not work.Specifically, everything except the comparison at the start works. It seems completely arbitrary how the computer parses the LEQ; I can't find a consistent pattern. It definitely isn't comparing two dates with each other.

The variable !fileDateTime! always has the same format as %%~tf, basically by definition. But Batch doesn't know what to do with it, or rather, I'm not sure how to tell Batch what to do with it.

I have tried using ForFile, but the path in question is a network share, so it fails (and for some reason the workaround with Net Use doesn't like to work either.)

Is there an easier way to get the most recent file in a folder and check how old it is?

(Also, the server holding the UNC path is a linux server, so if this is substantially easier in Bash, I could do that too.)

EDIT: if anyone is wondering how I fixed this, my solution was "realize that there's absolutely no reason I should be doing this is batch to begin with, install Python on the server, and script it in that instead." This will now be my go-to solution for batch problems in the future. But thank you all very much for the advice, which definitely would have helped if I didn't change tack.

J.Swersey
  • 151
  • 3
  • 3
  • 16
  • 2
    The value of `%%~tf` depends on your local settings. `If` can't compare dates, only integers or strings. You have two `set` commands after `if defined` (hopefully a copy/paste error?) – Stephan Oct 05 '20 at 10:31
  • @Stephan yes, that was a copy error, I'll go fix it. Is there any good way to transmute a date into an integer in Batch? What I've seen so far is very complex. – J.Swersey Oct 05 '20 at 10:45
  • 3
    `Dir` with `/OD` and `/TW` will list your files according to their last modified date and times from oldest to newest, but will output the order separately for each subdirectory, not the entire list. Can you explain what exactly you're trying to do, because my assumption is that you're hoping to propagate a variable for the first subdirectory, and either keep it or throw it, should the date or time from the next subdirectory be more recent that that, then continue through each subdirectory, until the end where the remaining variable value will be the required newest fully qualified filename. – Compo Oct 05 '20 at 10:56
  • See my answer [here](https://stackoverflow.com/questions/37617852/batch-script-for-loop-with-two-commands). (the accepted answer will probably not work for you). Let us know if that helps you. – Stephan Oct 05 '20 at 11:07
  • 1
    Two more possible solutions [here](https://stackoverflow.com/questions/53841679/windows-dir-command-sort-files-by-date-when-s-is-specified) – Stephan Oct 05 '20 at 11:23

1 Answers1

1

The date you get is a string, not a date object like you'd get in an object-oriented language.
And the LEQ operator can only compare integers, not arbitrary strings.
Worse still, the date string you get is in a format that depends on your OS localization, AND on user preferences.
To do a meaningful comparison, you have to first convert your date strings to a julian date (An integer counting the number of days since an initial reference date.) Then compare those integers together.
For that I recommend that you use the :jdate function there:
https://www.dostips.com/DtCodeCmdLib.php#Function.jdate