Solved

Batch file to delete history in destination

Posted on 2015-01-20
25
160 Views
Last Modified: 2015-01-27
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
0
Comment
Question by:Svlss
  • 14
  • 11
25 Comments
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
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
 

Author Comment

by:Svlss
Comment Utility
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
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
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
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Also, I revised the first ECHO line so make sure you have the latest.
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility

...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
 

Author Comment

by:Svlss
Comment Utility
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
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Are the source filename(s) the same as the destination names?
0
 

Author Comment

by:Svlss
Comment Utility
Yes Source file names are same as destination names
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
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
 

Author Comment

by:Svlss
Comment Utility
Yes you are correct.
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Will the source filenames always begin with ABC or XYZ or some other pattern?
0
 

Author Comment

by:Svlss
Comment Utility
There are 10+ files we copy every day and they all are unique
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Expert Comment

by:NVIT
Comment Utility
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
 

Author Comment

by:Svlss
Comment Utility
The file do not follow a standard pattern.  Abc and Xyz are not real names and the file names are confidential :-(
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
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
 

Author Comment

by:Svlss
Comment Utility
Yes All the files end with Date month day and time pattern Eg:201501014000.bak,201501024000.bak,201501034000.bak etc
0
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
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
 

Author Comment

by:Svlss
Comment Utility
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
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
Sorry, that last post was incorrect. I deleted it.
0
 
LVL 23

Accepted Solution

by:
NVIT earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:Svlss
Comment Utility
Excellent.. Thanks a lot :-)
0
 

Author Comment

by:Svlss
Comment Utility
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
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
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
 

Author Comment

by:Svlss
Comment Utility
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
 
LVL 23

Expert Comment

by:NVIT
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now