Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: IF EXIST & COPY to archive before XCOPY /s/d/y stomps on backup target location  (Read 8447 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
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



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
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)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Thanks once again Sidewinder!!!

That did the trick!

Dave