Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5783
  • Last Modified:

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

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
Saxitalis
Asked:
Saxitalis
1 Solution
 
Steve KnightIT ConsultancyCommented:
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
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Bill PrewCommented:
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
1
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
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
 
Lionel MMSmall Business IT ConsultantCommented:
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
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
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
 
Steve KnightIT ConsultancyCommented:
Fair enough!
0
 
SaxitalisAuthor Commented:
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
 
Joe Winograd, EE MVE 2015&2016DeveloperCommented:
Saxitalis,
You're welcome. Happy to (try to) help. Regards, Joe
0
 
Steve KnightIT ConsultancyCommented:
No problem, glad it helped
Steve
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now