Solved

Batch file to delete and copy latest file

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

11 Experts available now in Live!

Get 1:1 Help Now