michaelekins
michaelekins

Reputation: 579

Batch File - move duplicate files

I'm having a headache with this for loop in a batch file.

Basically, I am searchign through an archive folder to see if a file has already been processed. If so, then move the file to a duplicates folder. I've set the date variables above in the script and they work fine:

 for %%f in (%toLoadLocation%\*.*) DO (
 if exist %archiveLocation%\%%~nxf (
 IF NOT EXIST "%archiveLocation%\Duplicates" mkdir "%archiveLocation%\Duplicates"
 IF NOT EXIST "%archiveLocation%\Duplicates\%localYYYY%" mkdir "%archiveLocation%\Duplicates\%localYYYY%"
 IF NOT EXIST "%archiveLocation%\Duplicates\%localYYYY%\%localMM%" mkdir "%archiveLocation%\Duplicates\%localYYYY%\%localMM%"
 copy %%f "%archiveLocation%\Duplicates\%localYYYY%\%localMM%"
 echo %localDate% %localTime%       Duplicate claims file: %%f File moved to archive     >> "%logLocation%\Error.log"
 )

Am I doing something dumb here? I don't get an error, the script just bombs out when runnign this (I've a pause command before and after this hoping to catch a message, but nothing)

Any help would be much appreciated.

Thanks guys!

Upvotes: 0

Views: 543

Answers (2)

Endoro
Endoro

Reputation: 37569

try this:

for %%f in ("%toLoadLocation%\*.*") DO if exist "%archiveLocation%\%%~nxf" (
    IF NOT EXIST "%archiveLocation%\Duplicates" mkdir "%archiveLocation%\Duplicates"
    IF NOT EXIST "%archiveLocation%\Duplicates\%localYYYY%" mkdir "%archiveLocation%\Duplicates\%localYYYY%"
    IF NOT EXIST "%archiveLocation%\Duplicates\%localYYYY%\%localMM%" mkdir "%archiveLocation%\Duplicates\%localYYYY%\%localMM%"
    copy "%%~f" "%archiveLocation%\Duplicates\%localYYYY%\%localMM%"
    echo %localDate% %localTime%        Duplicate claims file: %%f File moved to archive     >> "%logLocation%\Error.log"
)

Upvotes: 1

RGuggisberg
RGuggisberg

Reputation: 4750

You should use quotes on these 2 lines:

if exist "%archiveLocation%\%%~nxf" (

and

copy "%%f" "%archiveLocation%\Duplicates\%localYYYY%\%localMM%"

Upvotes: 0

Related Questions