This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
56} How to find and move more recent files from one folder to another?
Actually, this task is very near what
XCOPY /D /V /F /W C:\SOURCE\*.* C:\TARGET\
does. With one important difference. XCOPY does not move files (the
discontinued
XXCOPY could).
Even if this task could be attempted with the CMD.EXE tools
presented thorughtout this FAQ, the more complicated and specific a
task gets, the batter to resort to use a programming language or a
third party program. A Visual Basic Script
XPMOVE.VBS is included for the task.
Moving files always involves some danger. Use the material
carefully, and as always, at your own risk.
The
information provided by XCOPY together with
SED
and
GAWK can be used to generate a file
of commands to make the moves.
@echo off & setlocal enableextensions
::
:: Source and target folders
set SourceFolder=C:\_D\BAS
set TargetFolder=C:\_M
::
:: Target file where to put the generated script
set cmd_=C:\_M\MoveThem.cmd
:: An auxiliary file
set tmpfile_=%temp%\tempfile.lis
::
:: Use xcopy to identify the files to be moved
::/D:m-d-y Copies files changed on or after the specified date.
:: If no date is given, copies only those files whose
:: source time is newer than the destination time.
:: /V Verifies each new file.
:: /F Displays full source and destination file names while copying.
:: /L Displays files that would be copied.
xcopy /D /V /F /L "%SourceFolder%\*.*" "%TargetFolder%\" > "%cmd_%"
:: Quote the file names and drop the last line (number of files)
sed -e "s/ -\x3e /\x22 \x22/" "%cmd_%"|sed "$d"> "%tmpfile_%"
:: Put an @echo off at the beginning of the generated command file
echo @echo off>"%cmd_%"
:: Build up the move command, include a rem for safety
gawk '{printf "rem move /-y \"%%s\"\n",$0}' "%tmpfile_%" >> "%cmd_%"
::
:: Clean up
for %%f in ("%tmpfile_%") do if exist %%f del %%f
::
:: Tell that the generated commands file is ready
dir "%cmd_%"
endlocal & goto :EOF
In the above the " -\x3e " means " -> " and a \x22 stands in hex
for a quote character (").
The contents of the generated MoveThem.cmd would be someting like
@echo off
rem move /-y "C:\_D\BAS\BATFAQ.BAS" "C:\_M\BATFAQ.BAS"
rem move /-y "C:\_D\BAS\BATFAQ.BAT" "C:\_M\BATFAQ.BAT"
rem move /-y "C:\_D\BAS\BATFAQ2.BAT" "C:\_M\BATFAQ2.BAT"
:
There is, however, a clearly simpler solution utilizing the archive
file attribute
@echo off & setlocal enableextensions
::
:: Source and target folders
set SourceFolder=C:\_D\BAS
set TargetFolder=C:\_M
::
:: Set the archive file attribute for all the source folder's files
attrib +a "%SourceFolder%\*.*"
::
:: /D Copy the files whose source time is newer than the destination time
:: /M Copies only files with the archive attribute set,
:: turns off the archive attribute.
xcopy /D /V /F /M "%SourceFolder%\*.*" "%TargetFolder%\"
::
:: Delete the source files which have the archive file attribute turned off
del /a:-a-h /p "%SourceFolder%\*.*"
endlocal & goto :EOF