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

Posted on 2014-02-06
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 500 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 54

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 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

LVL 55

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 54

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 54

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 54

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows Live Photo Gallery and show you various editing filters and touches to photos you can apply. This will be demonstrated using Windows Live Photo Gallery on Windows 7 operating system.

691 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