Solved

Batch File to get largest file name

Posted on 2007-11-29
11
1,760 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
  • 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
 

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
The curse of the end user strikes again      

You’ve updated all your end user’s email signatures. Hooray! But guess what? They’re playing around with the HTML, adding stupid taglines and ruining the imagery. Find out how you can save your signatures from end users today.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now