Link to home
Start Free TrialLog in
Avatar of Svlss
Svlss

asked on

Batch file to delete history in destination

I have a batch file that copies bunch of files like Abc.bak ,xyz.bak etc to a different server. I want that batch file to also clean up history in destination folder. It has to delete the files in destination which are older than 3 days. However the catch here is it has to delete only the older files that successfully were copied into destination.  Let me explain it better in examples so Day 1 Abc.bak & xyz.bak files were successfully copied to destination and destination folder doesn’t contain Abc.bak & xyz.bak files that Are 3 days old so script doesn’t delete any files. Day 2 Script copies Abc.bak & xyz.bak files and it doesn’t delete any files , Day3 it does the same . On day 4  because of some issue script was able to copy only abc.bak file & it doesn’t copy xyz.bak file. So script should only delete abc.bak file that is older than 3 days and it should not delete xyz.bak file even though it is older than 3 days because script did not copy current days xyz.bak file to destination. Is this possible?
I know it’s confusing but please let me know if you need  further explanation
Avatar of NVIT
NVIT
Flag of United States of America image

This should get you started.
Note: The ECHO prefixes are for visual confirmation. Remove them to run for real
@echo off
set SrcDir=.
set TgtDir=c:\TargetDir
pushd %SrcDir%
for %%a in (*.*) do (
  ECHO copy %%a %TgtDir%
  ECHO if %errorlevel% equ 0 forfiles /m %%a /d -3 "cmd /c del %TgtDir%\%%a"
)
popd

Open in new window

Avatar of Svlss
Svlss

ASKER

NewCillageIT,

Thanks for the reply I tried your script and it dint work


set TgtDir=D:\Test
pushd D:\Test1
for %%a in (*.*) do (
  ECHO copy %%a %TgtDir%
  ECHO if %errorlevel% equ 0 forfiles /m %%a /d -1 "cmd /c del %%a"
)
popd
Are you running it from a CMD prompt? If so, you should see what files will be affected. However, as I said it is VISUAL ONLY. To run it and actually COPY and DELETE the files, remove the ECHO prefix on both lines.
Also, I revised the first ECHO line so make sure you have the latest.

...copy only abc.bak file & it doesn’t copy xyz.bak file. So script should only delete abc.bak file that is older

Reading your original post doesn't seem to make sense.

If ABC.bak from source to destination, there is nothing to delete at destination because it is overwritten by the COPY command.
Avatar of Svlss

ASKER

to make it more clear when we take a bakcup of a file it adds date and time to backupfile name so it would be like abc01012015.bak ,abc01022015.bak etc..
Are the source filename(s) the same as the destination names?
Avatar of Svlss

ASKER

Yes Source file names are same as destination names
Stilly trying to understand what you want do delete...

Example:
Source: abc01042015.bak
Destination has multiple files, like abc01012015.bak, abc01032015.bak

abc01012015.bak will be erased. Is that correct?
Avatar of Svlss

ASKER

Yes you are correct.
Will the source filenames always begin with ABC or XYZ or some other pattern?
Avatar of Svlss

ASKER

There are 10+ files we copy every day and they all are unique
But, will the filenames always begin with ABC or XYZ or some other pattern?

Are ABC and XYZ real filenames? It would be helpful if you give me real filenames.
Avatar of Svlss

ASKER

The file do not follow a standard pattern.  Abc and Xyz are not real names and the file names are confidential :-(
So script should only delete abc.bak file that is older than 3 days and it should not delete xyz.bak file even though it is older than 3 days because script did not copy current days xyz.bak file
Here, you show 2 prefix patterns: ABC and XYZ. Unless I know the pattern to look for I can't find the matching file to delete.

However, I may be able to find it this way...

Earlier, you showed source names like abc01012015.bak, abc01032015.bak.

Do they always end with date suffixes like those, i.e. 01012015, 01032015?
Avatar of Svlss

ASKER

Yes All the files end with Date month day and time pattern Eg:201501014000.bak,201501024000.bak,201501034000.bak etc
Do the dates in the filenames match or are very close to the actual file date? e.g. does the actual date of file 201501014000.bak match the filename? Specifically, if the 20150101 portion matches, that is close enough.
Avatar of Svlss

ASKER

Yes date and time on the file names
for example

Day1
abc201501014000.bak
xyz201501014000.bak
ijk201501014000.bak


Day2
abc201501024000.bak
xyz201501024000.bak
ijk201501024000.bak

Day3
abc201501034000.bak
xyz201501034000.bak
ijk201501034000.bak
Sorry, that last post was incorrect. I deleted it.
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
Avatar of Svlss

ASKER

Excellent.. Thanks a lot :-)
Avatar of Svlss

ASKER

NewVillageIT will the code be different if target is a shared drive? This codes works fine to copy files in the same server but if i run this code to copy files to shared drive it fails .
Glad to see the solution works for you.

Re the shared drive error, If you give me the exact line and error I may be able to help.
Avatar of Svlss

ASKER

It copies the file but it does not delete the 3 day older files in destination folder. it does not give any error message.
Not sure what to say. It works fine here. Can you give more details, e.g.
- Your exact code.
- Source, target folder names, filenames.
- If you want to hide certain file/folder names like e.g. Secret you can change it to ABC.

Details can make a difference when troubleshooting.