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
Solved

Batch file to delete and copy latest file

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…

840 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