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

1 次查看(过去 30 天)
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!

回答(2 个)

Adam
Adam 2016-3-4
编辑:Adam 2016-3-4
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 2016-3-4
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

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by