Copy only last file in a folder to another folder using robocopy

Posted on 2014-02-06
Medium Priority
Last Modified: 2014-02-08
Hello Experts,

I would like to create a script using Robocopy that will only copy one file in a folder to another folder.

This file should be the file with the most current Date Modified attribute.

E.g. (Copy file from Folder1 to Folder2)

file 1 08:00
file 2 09:00
file 3 10:00

Robocopy should only copy file 3 to Folder2 because it has a time stamp of 10:00

file 3 10:00

Does anyone know if this is possible?

Question by:Saxitalis
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
LVL 43

Accepted Solution

Steve Knight earned 2000 total points
ID: 39840701
The easiest way is to use a dir command output, the top one is the newest file if you do dir /o-d /a-d and the /b just gives you the filename which you can read with a for command and push into copy, robocopy, xcopy or whatever.  The exit /b stops the for loop after the first go.

@echo off
set folder1=C:\folder1
set folder2=c:\folder2
for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%folder1%\*.txt"') do copy "%folder1%\%%~a" "%folder2%" & goto next
echo Carrying on with rest of batch

LVL 55

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
ID: 39840973
It's not possible to do with a straight execution of Robocopy. As Steve showed, you can do it with a script or program. But as long as you're writing a bit of code, it's easy enough to write a program that does exactly what you want, which I just did. It's called CopyNewest. It takes two parameters and an optional switch. The first param is the source folder, second param is destination folder, third param (optional) is /w (which means to overwrite the file if it exists). The definition of newest is based on the Modified time stamp, not the Created one.

Example command line calls:

copynewest.exe c:\folder1 d:\folder2 /w

copynewest.exe "c:\folder_in name with space" "d:\folder_out with backslash\"

The first example shows the /w param, so if the file exists, it will be overwritten (without the /w param it will not be overwritten). The second example makes two points: (1) if there are spaces in the folder name, enclose it in quotes; (2) it doesn't matter if a folder name has an ending backslash — the program works with or without it.

I uploaded the program to EE-Stuff, because the regular EE site does not allow uploading EXE files. You log into EE-Stuff with the same username and password as EE. Then click on the Expert Area tab, then the "Find files for a question" link, and enter this question's ID (28358767) in the box. When you download the file, the file name will be <CopyNewest.renamed-from-exe-for-your-safety>. Rename it to <CopyNewest.exe> (or whatever you want to call it, but make sure the file type is EXE). Regards, Joe
LVL 43

Expert Comment

by:Steve Knight
ID: 39841145
I'm sure you are legit. Joe so notyou criticising, but do people actually still download random exe's without knowing the source,when a one line batch file or few lines of VBscript does the same?
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

LVL 56

Expert Comment

by:Bill Prew
ID: 39841674
And of course, if you want to move on up to Powershell, it might look like:

Get-ChildItem C:\Folder1\*.* | Sort-Object LastWriteTime -Desc | Select-Object -First 1 | Copy-Item -Dest C:\Folder2

Open in new window

LVL 55

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
ID: 39842145
Hi Steve dragon-it,

When you're working with someone who has been a contributing member of EE for more than three years, has posted under his real first and last names since day one (not some alias/moniker), has more than 1,000 accepted/assisted solutions and 1.5 million points to his name, and has published 20 articles (some with source code), I'd say that it qualifies as not exactly a "random" EXE and not exactly as being downloaded "without knowing the source" (and I posted both the MD5 and SHA1 checksums for the file). All of that said, it's entirely up to the EE member.

I do appreciate your comments that you're sure I'm legit and are not criticizing. Regards, Joe
LVL 25

Expert Comment

by:Lionel MM
ID: 39842240
I'm with Steve--why use a exe when a few lines in a batch file will do--mainly because as you use these batch files you also can learn and then once you see how this works you can apply to to other circumstances later whereas with an exe (as good as it may be programatically) you don't see how it was written (works (great) but no learning how to solve it yourself down the road). But as you say Joe it is up to the asker of this question to decide what works best for them
LVL 55

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
ID: 39842315
Hi lionelmm,
I agree on the learning point, which is why I have spent an extraordinary amount of time explaining the code snippets (at a reasonably detailed level) in some of the articles I published here at EE (one example). But the experience of members here varies dramatically. I was on a thread a while ago where the asker said something like, "Oh, no, not that dreaded Windows Explorer thing." So for some members, as strange as this sounds to the Experts, using Windows Explorer to copy, move, or delete a file is beyond their expertise...and even their willingness to learn. I've been on many threads where a member had no interest in a VBScript or a batch file, let alone a PowerShell script. In some circumstances, they want a simple, turnkey solution. That said, of my 1,000+ solutions and 20 articles, this is only the second time that I went the route of posting an EXE on EE-Stuff. Neither has been downloaded yet, so I may be giving up on the technique, but I thought it was worth a shot. Regards, Joe
LVL 43

Expert Comment

by:Steve Knight
ID: 39842410
Fair enough!

Author Closing Comment

ID: 39843601
Thanks for the solution Steve - this is exactly what I was looking for. I actually do quite a bit of coding in C# and have done this copy replace procedure in a program but I wanted a batch file for easy remote deployment. I never work with batch files and did not know the syntax for sure.

I appreciate your comments as well Joe - cheers
LVL 55

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
ID: 39843604
You're welcome. Happy to (try to) help. Regards, Joe
LVL 43

Expert Comment

by:Steve Knight
ID: 39843796
No problem, glad it helped

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

765 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