Solved

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

Posted on 2014-02-06
11
3,577 Views
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)

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

Folder2:
file 3 10:00

Does anyone know if this is possible?

Thanks!
0
Comment
Question by:Saxitalis
11 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
Comment Utility
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
:next
echo Carrying on with rest of batch


Steve
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
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
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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?
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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

~bp
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 24

Expert Comment

by:lionelmm
Comment Utility
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
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
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
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Fair enough!
0
 

Author Closing Comment

by:Saxitalis
Comment Utility
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
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
Saxitalis,
You're welcome. Happy to (try to) help. Regards, Joe
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
No problem, glad it helped
Steve
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

First some basics on Windows 7 Backup.  It has 2 components one is a file based backup which is stored in .zip files each zip is split at around 200 Megabytes and there is the Image Backup which is as the name implies a total image of the partition …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on 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.

763 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

6 Experts available now in Live!

Get 1:1 Help Now