Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch file to delete history in destination

Posted on 2015-01-20
25
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 11
25 Comments
 
LVL 25

Expert Comment

by:NVIT
ID: 40559979
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
ID: 40560032
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 25

Expert Comment

by:NVIT
ID: 40560046
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 25

Expert Comment

by:NVIT
ID: 40560052
Also, I revised the first ECHO line so make sure you have the latest.
0
 
LVL 25

Expert Comment

by:NVIT
ID: 40560059

...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
ID: 40560067
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 25

Expert Comment

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

Author Comment

by:Svlss
ID: 40560319
Yes Source file names are same as destination names
0
 
LVL 25

Expert Comment

by:NVIT
ID: 40560343
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
ID: 40560372
Yes you are correct.
0
 
LVL 25

Expert Comment

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

Author Comment

by:Svlss
ID: 40560440
There are 10+ files we copy every day and they all are unique
0
 
LVL 25

Expert Comment

by:NVIT
ID: 40560456
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
ID: 40560516
The file do not follow a standard pattern.  Abc and Xyz are not real names and the file names are confidential :-(
0
 
LVL 25

Expert Comment

by:NVIT
ID: 40560540
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
ID: 40560567
Yes All the files end with Date month day and time pattern Eg:201501014000.bak,201501024000.bak,201501034000.bak etc
0
 
LVL 25

Expert Comment

by:NVIT
ID: 40560727
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
ID: 40560742
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 25

Expert Comment

by:NVIT
ID: 40560873
Sorry, that last post was incorrect. I deleted it.
0
 
LVL 25

Accepted Solution

by:
NVIT earned 2000 total points
ID: 40561168
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
ID: 40561870
Excellent.. Thanks a lot :-)
0
 

Author Comment

by:Svlss
ID: 40561898
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 25

Expert Comment

by:NVIT
ID: 40562058
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
ID: 40573118
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 25

Expert Comment

by:NVIT
ID: 40573419
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Make the most of your online learning experience.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

660 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