Data copy batch script

samiam41
samiam41 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NVITEnd-user support

Commented:
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

Author

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

Author

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

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

NVITEnd-user support

Commented:
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 support

Commented:
Refresh your web page.
NVITEnd-user support

Commented:
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

Author

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 support

Commented:
See my last post, which I revised.
NVITEnd-user support

Commented:
@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

Author

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 support

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.

Author

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).

Author

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 support

Commented:
*Confused* between Move and Copy.
Please clarify entire requirements

Author

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.

Author

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)

Author

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.
End-user support
Commented:
@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

Author

Commented:
Hot damn!  You got it and the logging works as well!  Thanks for your patience and working to find the solution.

Author

Commented:
Always a pleasure to work with someone like NVIT!
NVITEnd-user support

Commented:
Thank you. I'm glad we worked that out, SamIAm41! Have a nice day.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial