IF EXIST & COPY to archive before XCOPY /s/d/y stomps on backup target location

Started by DaveLembke, November 15, 2007, 05:30:23 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DaveLembke

Help needed for archival of files in batch

Trying to figure out how to use the IF EXIST Command with XCOPY instruction to avoid overwriting existing archive with latest file.

Using:

XCOPY c:\data\*.* Z:\backup /s/d/y

It stomps ontop of older files so I always have the latest altered files backed up which works as I wanted it to until now.

What I would like to do now is to check before copying the files over to see if they exist, and if they do pass them into a folder named BEFORE at Z:\backup\BEFORE ....before copying the latest date/time stamped file of that same name to Z:\backup

Been trying to get the syntax correct but no luck with my batch. I am guessing that I would have to add a condition to be tested before allowing the XCOPY to operate, to test if the file exists of a similar name between Z:\Backup and C:\Data, and if so copy the files that are matching in name to Z:\backup\BEFORE  ... before the XCOPY actually runs the routine to drop the latest data to Z:\Backup

Or Maybe there is a better method of combining the XCOPY string with IF EXIST to make this more clean

Any suggestions???

Thanks,

Dave

Sidewinder

Had a hard time deciphering your request, but if I understood it correctly, maybe this code will be helpful:


for /f "tokens=1* delims=" %%i in ('dir /b c:\data\*.*') do (
if exist "z:\backup\%%i" copy "z:\backup\%%i" z:\backup\before
)
xcopy c:\data\*.* z:\backup /s/d/y


Good luck.

8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein