Computer Hope

Microsoft => Microsoft DOS => Topic started by: DaveLembke on November 15, 2007, 05:30:23 PM

Title: IF EXIST & COPY to archive before XCOPY /s/d/y stomps on backup target location
Post by: DaveLembke on November 15, 2007, 05:30:23 PM
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
Title: Re: IF EXIST & COPY to archive before XCOPY /s/d/y stomps on backup target locat
Post by: Sidewinder on November 16, 2007, 06:23:18 AM
Had a hard time deciphering your request, but if I understood it correctly, maybe this code will be helpful:

Code: [Select]
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)
Title: Re: IF EXIST & COPY to archive before XCOPY /s/d/y stomps on backup target location
Post by: DaveLembke on November 21, 2007, 07:32:20 AM
Thanks once again Sidewinder!!!

That did the trick!

Dave