Solved

Batch File to get largest file name

Posted on 2007-11-29
11
1,779 Views
Last Modified: 2008-02-01
I have a backup directory that has files which look like this

mybackup200711291000.bak
mybackup200711290900.bak
mybackup200711290800.bak
etc.

I want to copy the last backup file to another location.  Is there someway to do the following in a batch file:
cd c:\backups
copy max(file) \\anotherserver\backups

how do you get the max file name?
0
Comment
Question by:Fraser_Admin
[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
  • 6
  • 4
11 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 20375883
This should do the trick:

@echo off
cd /d c:\backups
for /f "tokens=*" %%a in ('dir  /b /os') do set bigbackup=%%a
echo copy "%bigbackup%" \\anotherserver\backups


But - are you sure the backup will never DECREASE in size?  You might be better off basing things on the date - which would be:

@echo off
cd /d c:\backups
for /f "tokens=*" %%a in ('dir /b /od') do set bigbackup=%%a
echo copy "%bigbackup%" \\anotherserver\backups


(The only change between these two is the dir command in the parens - /os vs. /od)
0
 
LVL 38

Assisted Solution

by:Shift-3
Shift-3 earned 100 total points
ID: 20375884
An easy way to do this is to use the DIR command with the /O:D switch.  Combined with FOR /F it loops through a list of files in the directory and ends on the one with the most recent date modified.
setlocal
 
for /F "tokens=*" %%G in ('dir *.bak /B /O:D') do set maxfile=%%G
 
copy "%maxfile%" \\anotherserver\backups
 
endlocal

Open in new window

0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 20375911
Oh yeah, you can do code now....
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:Fraser_Admin
ID: 20376039
oh i and want to just call the file backup or something so it will overwrite when i copy.  i am going to set it up as a job so it runs everynight, as yes it is largest by date,  so datetime stamp would work for me too.  it is just that on the main server we keep backups for 1 week and i always want to copy the most recent to my test server in the night.
0
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 400 total points
ID: 20376093
If you want to overwrite things, then just specify the file name you want to use:

I like Shift-3's use of set/endlocal - should've done it myself.

Also, you do realize I have an echo in there so that it SHOWS you what it will do - when you want it to actually do things, just remove the "echo"
@echo off
setlocal
cd /d c:\backups
for /f "tokens=*" %%a in ('dir  /b /os') do set bigbackup=%%a
echo copy "%bigbackup%" "\\anotherserver\backups\MostRecentBackup.Bak"
endlocal

Open in new window

0
 

Author Comment

by:Fraser_Admin
ID: 20376115
setlocal
 
for /F "tokens=*" %%G in ('dir *.bak /B /O:D') do set maxfile=%%G
 
echo %maxfile%
 
endlocal

pause


when i do this i see imsins.BAK but that file is not in the folder i'm executing the batch from??
0
 

Author Comment

by:Fraser_Admin
ID: 20376143
oh it gave me that file cause it was defaulting somewhere else because it didn't like the unc path
0
 

Author Comment

by:Fraser_Admin
ID: 20376351
is there anyway within the batch file that you could compress it first before copying?  my file is > 900mb takes forever to copy.
0
 

Author Comment

by:Fraser_Admin
ID: 20376376
it is ~130 mb when it is zipped.  it looks like it would take about 3 hours to copy with the full size.  so i would like to zip via command line if possible?
0
 

Author Comment

by:Fraser_Admin
ID: 20377011
i got the zip working by using fbzip.exe utility.  it is free and works from command line.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 20377967
Actually, I use info-zip - which offers a command line zip tool called, simply enough - zip.exe.  
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

730 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