Solved

Batch File to get largest file name

Posted on 2007-11-29
11
1,782 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 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 96

Expert Comment

by:Lee W, MVP
ID: 20375911
Oh yeah, you can do code now....
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

690 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