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

LVL 9
samiam41Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
Change  line. 7 to move "%%I" "c:\archive"

Delete lines 17 too 23. The RENAME  section.

If just one operation is done, you can replace the CALL COPY to just  COPY "%%I "C:\Test\DIR2
samiam41Author Commented:
Thanks for the reply!  Line  7 is "if not exist..."  I'm not sure where you are referencing.

@echo off
setlocal enabledelayedexpansion
set Log=c:\test\logs\data_move.log

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

GOTO :EOF

:COPYFILE
COPY "%1" "C:\Test\Gendoc" > %log%
GOTO :EOF

:MOVE
FOR /L %%N IN (1, 1, 1000) DO (
    IF NOT EXIST "C:\Test\GENDOC\%%~nxI" (
        MOVE "C:\Test\GENDOC\%%~nxI" "C:\Test\GENDOC\Archive\*.*" >> %log%
        GOTO :EOF
    )
)

Open in new window

samiam41Author Commented:
I want to copy files from Test\JCAO to Test\Gendoc.  If the file being copied exists, I want the conflicting file to be move to Test\Gendoc\Archive and then the copy job runs.
@echo off
setlocal enabledelayedexpansion
set Log=c:\test\logs\data_move.log

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

GOTO :EOF

:COPYFILE
COPY "%1" "C:\Test\Gendoc" > %log%
GOTO :EOF

:MOVE
FOR /L %%N IN (1, 1, 1000) DO (
    IF NOT EXIST "C:\Test\GENDOC\%%~nxI" (
        MOVE "C:\Test\GENDOC\%%~nxI" "C:\Test\GENDOC\Archive\*.*" >> %log%
        GOTO :EOF
    )
)

Open in new window

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

NVITEnd-user supportCommented:
Maybe its my phone. Line 7 is CALL :RENAME "%%I"

Error my last post. Should be

If just one operation is done, you can replace the CALL COPY to just  COPY "%%I" "C:\Test\DIR2"
NVITEnd-user supportCommented:
Refresh your web page.
NVITEnd-user supportCommented:
AFAIK, the MOVE command doesn't echo results. Please verify. If not, just enable the line above it, which i REM'd

Also, I put everything in line, w/o the CALLs.
@echo off
setlocal enabledelayedexpansion
set Log=c:\test\logs\data_move.log

echo %date% %time% BEGIN "%0"
FOR /F "usebackq delims=;" %%I IN (`dir C:\Test\JCAO\*.* /s /b`) DO (
    IF NOT EXIST "C:\Test\Gendoc\%%~nxI" (
       COPY "%%I" "C:\Test\Gendoc" >> %log%
    ) ELSE (
        REM echo Move "%%I" "C:\Test\GENDOC\Archive" >> %log%
        MOVE "%%I" "C:\Test\GENDOC\Archive" >> %log%
    )
)
echo %date% %time% END "%0"
GOTO :EOF

Open in new window

samiam41Author Commented:
I ran the script you just provided and the files (file1.doc, file2.doc) in Test\JCAO are moved right into Test\Gendoc\Archive even though file1.doc and file2.doc don't exist in Gendoc.  The files should be copied from JCAO, not moved.  

The only ones that should be moved are when there is a conflict between the files copied into Gendoc from JCAO.  If they already exist in Gendoc, those in Gendoc should be moved and then the file should be copied from JCAO to Gendoc so there is no conflict.

@echo off
setlocal enabledelayedexpansion
set Log=c:\test\logs\data_move.log

FOR /F "usebackq delims=;" %%I IN (`dir C:\Test\JCAO\*.* /s /b`) DO (
    IF NOT EXIST "C:\Test\JCAO\%%~nxI" (
       COPY "%%I" "C:\Test\Gendoc" >> %log%
    ) ELSE (
        MOVE "%%I" "C:\Test\GENDOC\Archive" >> %log%
    )
)

GOTO :EOF

Open in new window

NVITEnd-user supportCommented:
See my last post, which I revised.
NVITEnd-user supportCommented:
@echo off
setlocal enabledelayedexpansion
set Log=c:\test\logs\data_move.log

echo %date% %time% BEGIN "%0" >> %log%
FOR /F "usebackq delims=;" %%I IN (`dir C:\Test\JCAO\*.* /s /b`) DO (
    IF NOT EXIST "C:\Test\Gendoc\%%~nxI" (
       COPY "%%I" "C:\Test\Gendoc" >> %log%
    ) ELSE (
        REM echo Move "%%I" "C:\Test\GENDOC\Archive" >> %log%
        REM Enable above line if MOVE command doesn't echo results
        MOVE "%%I" "C:\Test\GENDOC\Archive" >> %log%
    )
)
echo %date% %time% END "%0" >> %log%
GOTO :EOF

Open in new window

samiam41Author Commented:
Just ran the revised script.  A bit more information.  The files being copied from JCAO to Gendoc are updated versions of the files in Gendoc.  So when I copy the new files from JCAO, their older versions exist in Gendoc.  Because of this, I want to archive the old version before copying over the new ones.

**just saw you posted a new reply**  testing now.
NVITEnd-user supportCommented:
> I want to archive the old version before copying over the new ones.
Define "archive". I presume you want to move them. Please confirm. Do you want to move them or rename them? Moving would be easier.
samiam41Author Commented:
Improvement!!

The files are moved to archive if they exist in gendoc when copied from jcao.  However, the files are moved out of jcao instead of copied.  I need them to remain in jcao (the folder is actually named day_it_was_updated [example 031116] so it needs to remain).
samiam41Author Commented:
> I want to archive the old version before copying over the new ones.
Define "archive". I presume you want to move them. Please confirm. Do you want to move them or rename them? Moving would be easier.

My apologies.  Archive=move them
NVITEnd-user supportCommented:
*Confused* between Move and Copy.
Please clarify entire requirements
samiam41Author Commented:
the latest script you provided moves the file from jcao to gendoc.  i need it to copy the file so the files remain in jcao.
samiam41Author Commented:
directory structure BEFORE script runs
c:\test\jcao\file1.doc (last updated 0311)
c:\test\jcao\file2.doc (last updated 0311)

c:\test\gendoc\file1.doc (last updated 0218)
c:\test\gendoc\file2.doc (last updated 0213)


after script runs
c:\test\jcao\
c:\test\jcao\

c:\test\gendoc\file1.doc (last updated 0311)
c:\test\gendoc\file2.doc (last updated 0311)



c:\test\gendoc\archive\file1.doc (last updated 0218)
c:\test\gendoc\archive\file2.doc (last updated 0213)
samiam41Author Commented:
See how after the script runs, the JCAO directory is empty?  I need the files to be there after the copy job.  Right now they are removed after the script runs.
NVITEnd-user supportCommented:
@echo off
setlocal enabledelayedexpansion
set Log=c:\test\logs\data_move.log

echo %date% %time% BEGIN "%0" >> %log%
FOR /F "usebackq delims=;" %%I IN (`dir C:\Test\JCAO\*.* /s /b`) DO (
    IF NOT EXIST "C:\Test\Gendoc\%%~nxI" (
       COPY "%%I" "C:\Test\Gendoc" >> %log%
    ) ELSE (
        COPY /y "%%I" "C:\Test\GENDOC\Archive" >> %log%
    )
)
echo %date% %time% END "%0" >> %log%
GOTO :EOF

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samiam41Author Commented:
Hot damn!  You got it and the logging works as well!  Thanks for your patience and working to find the solution.
samiam41Author Commented:
Always a pleasure to work with someone like NVIT!
NVITEnd-user supportCommented:
Thank you. I'm glad we worked that out, SamIAm41! Have a nice day.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.