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

조회 수: 1(최근 30일)
Nelson dos Santos Netto
Nelson dos Santos Netto 2016년 3월 4일
답변: Walter Roberson 2016년 3월 4일
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

Community Treasure Hunt

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

Start Hunting!

Translated by