:: this deletes 10 files named "file1.txt" to "file10.txt"
for /L %%a in (1,1,10) do del R:\Test\file%%a.txt
:: this takes ALL of the files matching the mask and copies them to
:: "file*.txt" at the destination
for /f "tokens=1*delims=: " %%a in (
'dir /b /a-d "C:\Users\Username\Desktop\Test*" ^|findstr /n /r "." ') do (
echo xcopy "C:\Users\Username\Desktop\%%b" "R:\test\file%%a.txt"
)
The first command should be obvious.
The second simply lists the source directory, names only, no directorynames of files matching "test*" and directs that output to findstr
which /n
numbers each line matching the /r
regular expression "." (which will be all.)
The findstr
output will be of the form
1: test one.xyz
for
will tokenise the result, %%a
receiving the part before the delimiter-sequence and %%b
the part after. The delimiters are set to :
and Space; findstr
counts each as a delimiter, and a string of delimiters as one.
The result is echo
ed to ensure that the proposed action is reported, not executed so that any error may be detected before any damage occurs.
The activate the copy
, simply remove the echo
keyword.
Note that the files remain in the source as you've not given any clues about how they'll be further processed. If the files are left in place, they'll be copied on the next run as well - the for
loop will just keep counting...
for /f "tokens=1*delims=: " %%a in (
'dir /b /a-d /o-d "C:\Users\Username\Desktop\Test*" ^|findstr /n /r "." '
) do if %%s leq 10 (
echo copy "C:\Users\Username\Desktop\%%b" "R:\test\"
)
will [echo the commands to] copy
the latest 10 files from the source directory to the destination.
It should be obvious how to change 10
to some other value.
Manipulating the actual directory names and maintaining the source and destination directories - your affair.