>
> Thanks to everyone for their suggestions. I avoid VBScript out of sheer
> ignorance.
>
> Billious: your method appears to have promise, but I'm afraid I cannot
> understand what your FOR statements do! These three folders may contain
> totally non-matching filenames, and the exercise is to find the newest
> file.
>
> I've run the slightly-revised script on my sample folders (with added
> spaces for realism), but really am none the wiser...
>
> @echo .off
> :: select latest of 3 files for copying
> setlocal
> set f1=F:\Dan 09\Source A
> set f2=F:\Dan 09\Source B
> set f3=F:\Dan 09\Source C
> for /f %%a in ( ' xcopy /L /y /d "%f1%" "%f2%" ' ) do set r1=%%a
> if %r1%==1 (set f2=%f1%)
> for /f %%a in ( ' xcopy /L /y /d "%f2%" "%f3%" ' ) do set r1=%%a
> if %r1%==1 (set f3=%f2%)
> echo copy %f3%
> endlocal
>
Well, you've taken the first step. Note that the spaces surrounding the
single-quotes are superfluous - used for emphasis and avoiding confusion
with the double-quotes you've correctly inserted.
The FOR /F command takes each line of the filename in parentheses in turn
and applies the DO clause to the line. If the "filename" is single-quoted,
then the output of the command is used as the input file.
There are a few documented and undocumented quirks and features. Empty lines
are skipped and so are lines commencing with a semicolon. You may add a
quoted directive string after the /F to control how the line's contents are
interpreted - for instance "tokens=1,3delims=abc" which means 'parse the
line of input using "a", "b" and "c" as delimiters to generate tokens. Apply
the first to the FOR loop metavariable (%%a in your code) and the third to
the next (implicit) metavariable, %%b. By default, delimiters are space,
comma, semicolon [and possibly tab] and tokens=1 so if these directives are
omitted, only the nominated metavariable is set to the value of the first
token. The line is interpreted as
delimiters token1 delimiters token2 delimiters token3 ...
(documentation, from the prompt
FOR /? |more
)
switching to the XCOPY command, the /L (I used upper-case for emphasis and
clarity) produces a Listing of the files that the XCOPY would attempt to
copy, but not actually copy them. The /d ensures that the "copy" takes place
only if destination datestamp is earlier than the source and the /y forces
the 'copy' to proceed without pausing for authority to "overwrite."
The output of the XCOPY command will therefore be
blah, blah, blah
n File(s) copied
where n would be the number of files that the XCOPY would ATTEMPT to copy.
Since the filenames fed to the XCOPY direct it to "copy" ONE file over
another, then n must be 0 or 1, controlled by whether or not the destination
filename is later than the source.
The FOR/F applies each line of XCOPY's output to the DO clause, so sets R1
to the first token from each line in turn. The value that will "stick" is
that on the last line, which will be the "0" or "1"
Hence all that remains is to establish the full-filenames of the three files
in question into F1..F3.
The skeleton for this is to use the same method on three directories: [air
code]
set d1=c:\wherever\
for /f "delims=" %%i in ( ' dir /b /a-d /od "%d1%*.*" ' ) do set f1=%d1%%%i
which performs a DIR listing on the target directory D1 in BASIC format
(name only) in date order, omitting directorynames. The ENTIRE line is
applied to the metavariable %%i controlled by the empty delimiter-set (the
close-double-quote appears immediately after the '=') hence there is only
one token. This permits %%i to contain spaces.
Again, each line of the DIR output is SET into F1 in turn, so the final
result in F1 is the concatenation of the directory name D1 and the final
value of %%i from the final line of the DIR output - the newest filename
since the DIR listing is in date order.
And if you don't need a coffee after all that - I do!