Copy the most recent files from sourcefolder and check their existence in destinationfolder before copying

2 views (last 30 days)
The thing is:I'm trying to develop a script to regularly copy files from a sourcefolder to a destinationfolder. Nevertheless, I want to make sure it copies only the most recent and modified data (given the fact the older files don't get their names changed), in order to get a more efficient and quicker programme. Could you help me out, please?? Tks a lot!

Answers (2)

Adam
Adam on 4 Mar 2016
Edited: Adam on 4 Mar 2016
You can use something like
sourceFile = 'D:\SomeLocation\somefile.m;
destinationFile = 'D:\SomeOtherLocation\somefile.m;
import java.io.File
source = File( sourceFile );
destination = File( destinationFile );
if source.lastModified >destination.lastModified
copyfile( sourceFile, destinationFile );
end
Note that I wrote that based on example code I have used with a few changes so it might not be 100% syntactically correct, but I did a quick test of some of the functionality before posting it.
If a file does not exist then the 'lastModified' field of java.io.File returns 0 so you can do the lastModified > test without needing to check that as the source file will always be > 0.

Walter Roberson
Walter Roberson on 4 Mar 2016
sourceFile = 'D:\SomeLocation\somefile.m;
destinationFile = 'D:\SomeOtherLocation\somefile.m;
source_info = dir(sourceFile);
dest_info = dir(destinationFile);
if ~isempty(source_dir) && (isempty(dest_info) || dest_info.datenum < source_info.datenum)
copyfile( sourceFile, destinationFile );
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!