Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag 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

Avatar of NVIT
NVIT
Flag of United States of America image

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
Avatar of samiam41

ASKER

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

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

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"
Refresh your web page.
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

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

See my last post, which I revised.
@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

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.
> 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.
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).
> 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
*Confused* between Move and Copy.
Please clarify entire requirements
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.
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)
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.
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hot damn!  You got it and the logging works as well!  Thanks for your patience and working to find the solution.
Always a pleasure to work with someone like NVIT!
Thank you. I'm glad we worked that out, SamIAm41! Have a nice day.