Solved

Batch file to delete and copy latest file

Posted on 2008-10-30
4
866 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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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