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 get largest file name

Posted on 2007-11-29
11
1,777 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
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.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

790 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