Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Batch File to get largest file name

Posted on 2007-11-29
11
Medium Priority
?
1,818 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 96

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 400 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 96

Expert Comment

by:Lee W, MVP
ID: 20375911
Oh yeah, you can do code now....
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 96

Accepted Solution

by:
Lee W, MVP earned 1600 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 96

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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

610 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