Avatar of samiam41
samiam41
Flag for United States of America

asked on 

Data copy batch script

Hey Experts.  Working on a new script that will copy files from dir1 to dir2 but if the file being copied from dir1 to dir2 already exists in dir2, the file is moved to the Archive dir and then the copy job continues.

I can't seem to figure out the proper scripting for this.  Your help in accomplishing this would be appreciated.  Thanks Experts!

I found this but am having some trouble tweaking it to match what I wanted.  I grabbed it from http://stackoverflow.com/questions/13254256/search-then-copy-and-to-location-rename-if-exit and am still making changes.
@echo off

FOR /F "usebackq delims=;" %%I IN (`dir C:\Test\DIR1\*.* /s /b`) DO (
    IF NOT EXIST "C:\Test\DIR1\%%~nxI" (
        CALL :COPYFILE "%%I"
    ) ELSE (
        CALL :RENAME "%%I"
    )
)

GOTO :EOF

:COPYFILE
COPY "%1" "C:\Test\DIR2"
GOTO :EOF

:RENAME
FOR /L %%N IN (1, 1, 1000) DO (
    IF NOT EXIST "j:\@PCBackup\PST\%~n1%%N%<wbr ></wbr>~x1" (
        COPY "%1" "j:\@PCBackup\PST\%~n1%%N%<wbr ></wbr>~x1"
        GOTO :EOF
    )
)

Open in new window

Microsoft DOSWindows BatchWindows 7Scripting Languages

Avatar of undefined
Last Comment
NVIT

8/22/2022 - Mon