Solved

I need a batch file that will locate the most recent file in a folder and copy only that file to anther folder.

Posted on 2011-03-05
3
675 Views
Last Modified: 2012-05-11
I need a batch file that will locate the most recent file in a folder and copy only that file to anther folder.
0
Comment
Question by:SteveIBS
3 Comments
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 400 total points
ID: 35044570
I believe this should do the trick

@echo off
for /f "tokens=*" %%a in ('dir /b /od "c:\source\path"') do @set LastFile=%%a
copy "c:\source\path\%lastFile%" "c:\target\path"

Open in new window

0
 
LVL 11

Expert Comment

by:marek1712
ID: 35046401
Or PowerShell oneliner...
 
$var = Get-ChildItem SOURCE | Sort-Object CreationTime -Descending; Copy-Item $var[0] DESTINATION

Open in new window

CreationTime - date/time when the file was created. You can change it to LastWriteTime if you new most recently written file.
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 100 total points
ID: 35047242
Or
@echo off
set LastFile=
for /f "tokens=*" %%F in ('dir /b /o:-d "c:\source\path"') do if not defined LastFile set LastFile=%%F
copy "c:\source\path\%LastFile%" "c:\target\path"

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

932 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

14 Experts available now in Live!

Get 1:1 Help Now