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
SvlssAsked:
Who is Participating?
 
NVITConnect With a Mentor Commented:
Copy then delete similar named, 3-day old (relative to current date) target files:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set SrcDir=D:\SourceDir
set TgtDir=D:\TestDir
pushd %SrcDir%
for %%A in (*.*) do (
  set FN=%%~nA
  REM Get date suffix, e.g. 201501024000
  set FNDate=!FN:~-12!
  REM Get date prefix
  set FNPfx=!FN:~0,-12!
  copy "%%A" "%TgtDir%"
  REM Delete 3-day old (relative to current date) target files if source successfully copied
  if %errorlevel% equ 0 (
    forfiles /p "%TgtDir%" /m !FNPfx!*.* /d -3 /c "cmd /c del @file"
  )
)
popd

Open in new window

0
 
NVITCommented:
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

0
 
SvlssAuthor Commented:
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
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
NVITCommented:
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.
0
 
NVITCommented:
Also, I revised the first ECHO line so make sure you have the latest.
0
 
NVITCommented:

...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.
0
 
SvlssAuthor Commented:
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..
0
 
NVITCommented:
Are the source filename(s) the same as the destination names?
0
 
SvlssAuthor Commented:
Yes Source file names are same as destination names
0
 
NVITCommented:
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?
0
 
SvlssAuthor Commented:
Yes you are correct.
0
 
NVITCommented:
Will the source filenames always begin with ABC or XYZ or some other pattern?
0
 
SvlssAuthor Commented:
There are 10+ files we copy every day and they all are unique
0
 
NVITCommented:
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.
0
 
SvlssAuthor Commented:
The file do not follow a standard pattern.  Abc and Xyz are not real names and the file names are confidential :-(
0
 
NVITCommented:
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?
0
 
SvlssAuthor Commented:
Yes All the files end with Date month day and time pattern Eg:201501014000.bak,201501024000.bak,201501034000.bak etc
0
 
NVITCommented:
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.
0
 
SvlssAuthor Commented:
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
0
 
NVITCommented:
Sorry, that last post was incorrect. I deleted it.
0
 
SvlssAuthor Commented:
Excellent.. Thanks a lot :-)
0
 
SvlssAuthor Commented:
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 .
0
 
NVITCommented:
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.
0
 
SvlssAuthor Commented:
It copies the file but it does not delete the 3 day older files in destination folder. it does not give any error message.
0
 
NVITCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.