Determine latest file by date in a folder

This is a real tough one for me. Maybe a piece of cake for the experts.

I have a number of files in a Windows Server 2003 folder. The files are backup files of a database and created at various time.
My objective is to have a batch file with commands which will determine the newest copy by date.
Then move/copy/zip that file.
My preference is for .cmd batch file. But if necessary, .vbs, wsh or other supported scripts would do as well.

Any suggestions, tips and assistance would be gratefully received. Thanks
garychuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
The easiest way to get the last file is to use a dir command together with a for command to grab the right entry.  I would do it as follows.  Ask if not sure on any of it.

Steve

@echo off
set sourcedir=c:\backups
set destdir=D:\archive

for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%sourcedir\*.*"') do set filename=%%a & goto :next
:next

move "%filename%" "%destdir%"

the dir /b /a-d /o-d command lists files (not directories) in reverse order of date in bare format where just the filename is shown.  The For loop goes down the output of this file and takes the first entry then stops.
0
forrestoffCommented:
With due respect, this should work if the command:

  for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%sourcedir\*.*"') do set filename=%%a & goto :next

is corrected to be the following:

  for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%sourcedir%\*.*"') do set filename=%%a & goto :next

Nice code, though--I learned something new today! :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
forrestoffCommented:
I'd actually prefer the following:

  xcopy /d /i /y /e "C:\source_dir" "C:\destination_dir"

On the first run, it will copy all files to the destination directory, then, on subsequent runs, it will only copy the files that have changed.  Works well for archiving files.  Of course, you'll have to change the source_dir and destination_dir to meet your file system.

Cheers
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Steve KnightIT ConsultancyCommented:
Yes, sorry typo with the % on variable name. Good catch and glad you liked it.  comes of typing into here from memory without taking tested code.

I use xcopy /d or robocopy to do similar things but he did just want to copy the latest file.  If the destination changes, e.g. multiple different USB drives on different days then it will try and copy all files each time.  A way around that would be to use the archive bit but I like the for loop approach, have it working in live backup and log file situations all over the place.

Steve
0
Steve KnightIT ConsultancyCommented:
garychu, give forrestoff the points for finding my typo if that solves your problem.... will take him over the 10,000 points and an official 'expert' here.

I didn't even know the for command existed or the new options on set, if etc. until I started asking and answering around here.  Good luck.

BTW your name, is it   for res toff, forrest off, forre stoff or what?!

Steve
0
forrestoffCommented:
haha.  I'm not sure what to say!

Thanks, Steve, but you definitely deserve those points.

As for my screen name, it's a high school nickname and all members of the drumline went "Russian". My nickname was "Forrest" (probably because I looked and acted like Forrest Gump), so the "Russian" touch made it Forrestoff (pronounced as german as I could, including the rolling r's, etc...)

My real-name, however is David, and I have a profile here:
http://www.myspace.com/davidfoor

Thanks--this made my day.
0
garychuAuthor Commented:
Thank you gentlemen.
It is exactly what I am after. And what an elegant piece of code.

Xcopy would not have done it for me. I have a specific reason to isolate the newest file. I do use Xcopy to replicate files from one folder to another for archival purposes.

My final version, after a small bit of tidying to get it working is;
@echo off
set sourcedir=c:\backups
set destdir=D:\archive
for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%sourcedir%\*.*"') do set filename=%%a & goto :next
move "%sourcedir%\%filename%" "%destdir%"

I admire your generosity, Steve and have awarded the points accordingly.

Thank you once again.
0
Steve KnightIT ConsultancyCommented:
Careful there if it isn't a copy/paste error. You do need the :next label before the move command to break out of the FOR loop.  Otherwise without the goto you are stuck going around all the files in the directory (except goto will give an error anyway I suppose, so must be a type error).
Steve
0
garychuAuthor Commented:
Thanks for pointing it out, Steve.
It was indeed a copy/paste error, with the :next label left out.
Anyway, I have tested it extensively.
Very much appreciated for followup interest shown.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.