@echo off
setlocal
set "source=%cd%\source"
set "target=%cd%\target"
set "overflow=%cd%\overflow"
for /r "%source%" %%A in (*.*) do call :copyfile "%%~A" "move" "report"
2>nul rd "%source%"
exit /b
:copyfile source [, operation [, report]]
setlocal
set "curpath=%~1"
set "operation=%~2"
set "report=%~3"
if defined report echo "%curpath%"
call set "destpath=%%curpath:%source%=%target%%%"
if exist "%destpath%" call set "destpath=%%curpath:%source%=%overflow%%%"
if exist "%destpath%" (
if defined report echo exist in "%destpath%"
exit /b 1
)
if "%operation%" == "copy" (
if defined report echo copy to "%destpath%"
echo f|>nul xcopy "%curpath%" "%destpath%"
) else (
for %%A in ("%destpath%") do (
if not exist "%%~dpA" md "%%~dpA" || (
if defined report echo md failed with "%%~dpA"
exit /b 1
)
)
if defined report (
echo move to "%destpath%"
move "%curpath%" "%destpath%"
) else >nul move "%curpath%" "%destpath%"
for %%A in ("%curpath%") do 2>nul rd "%%~dpA"
)
exit /b 0
move
on same partition is a move in the
master file table only.
move
to different partition is an actual
copy
and delete
on successful copy
.
copy
always does copy
.
I implemented both operations and can be set by the
2nd optional argument to the called label of :copyfile
by passing "move"
or "copy"
.
"move"
is default if argument is not "copy"
.
The 3rd optional argument to the label :copyfile
is to output a progress report.
This argument if defined, will echo
information
(like paths etc.) about the operation.
The 1st argument to the label :copyfile
is the
path to the file to be copied or moved.
The for /r
loop recurses the source
directory and
calls :copyfile
with the path of each file found.
:copyfile
will set curpath
to the source
file and
will set destpath
to the path to target
, which is
the source
path substituted with target
path.
If destpath
exist, then destpath
is set to the path
to overflow
, which is the source
path substituted
with overflow
path. If still destpath
exist, then
the label is exited.
The move
operation uses rd
to remove empty folders
from source
.
The copy
operation uses xcopy
as it makes the
destination folder structure so that md
is not used.
The echo f
piped to xcopy
is to answer the "file
or folder?" prompt.
At end of script, the source
folder will be removed
if empty.
Set the source
, target
and overflow
variables
at the top of the script to the actual paths.
View set /?
about variable substitution used in
the script. It is used to replace the source
directory path with the another directory path,
to create the destination path.