Solved

Batch file to delete and copy latest file

Posted on 2008-10-30
4
865 Views
Last Modified: 2013-12-26
Hi,  I have configured Acronis to create daily backups based on an orginal image file.   The file number increments daily.   What has happened is that I ran a script to delete the older files and copy the latest.   However the script delete the latest file and left a file that is 3 days old and did the copy based on that file; e.g. I had 12 file labeled hbserver_bkup1.tib to hbserver_bkup12.tib with # 12 having a timestamp of 10/29/2008.   However, when I ran the script all the files from 1-12 for delete with the exception of hbserver_bkup9.tib dated 10/26/2008 and said bkup9.tib was copied.

This is the script......where did I go wrong.   I'm also new to this:

@echo off
setlocal enabledelayedexpansion
for /f "skip=1 tokens=* delims= " %%a in ('dir/b /a-d /o-n *.tib') do (
del %%a
)

xcopy z:\*.tib /a /y /d /k "i:\Server Backup"
0
Comment
Question by:stug
  • 3
4 Comments
 
LVL 4

Assisted Solution

by:Kaddict
Kaddict earned 250 total points
ID: 22847190
The dir statement fails to reorder your files correctly.

I'm working on a solution please hang on

-kaddict
0
 
LVL 4

Accepted Solution

by:
Kaddict earned 250 total points
ID: 22847222
OK. The method I'm using sorts the *.tib files by date, from the LATEST to the OLDEST. It keeps (does not delete) the LATEST file, #12, and it deletes 1-11. If I misunderstood what you wanted, just let me know where, and I will correct my solution.

--------cut----------------------------------------
@echo off
setlocal enabledelayedexpansion
Z:
cd\
for /f "skip=1 tokens=* delims= " %%a in ('dir/b /a-d /o-d *.tib') do (
del %%a
)
xcopy *.tib /y "i:\Server Backup"
--------cut----------------------------------------

0
 
LVL 4

Assisted Solution

by:Kaddict
Kaddict earned 250 total points
ID: 22847235
Note:  Sorting DOS filenames by names will not work if they the numbers are for example 1-2-3-4-5-6-7-8-9-10-11 because it'll sort as 1-10-11-2-3-4-5-6-7-8-9... you need numbers like 001-002-003-004-005-006-007-008-009-010-011 in order to sort by name, and I think you can't have Acronis to number the files like that. That's why I recommend sorting by date... yerterday's backup will always be older that today's one ;-)

-kaddict
0
 

Author Closing Comment

by:stug
ID: 31511926
Thank you very much and my apologies for taking so long to respond.   I had to wait a few days for the file list to grow.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

930 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

14 Experts available now in Live!

Get 1:1 Help Now