Solved

Batch file to delete and copy latest file

Posted on 2008-10-30
4
870 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
[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
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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