Click to See Complete Forum and Search --> : How to copy only new files from folder A to B(AppendConstrainLine, FileInfo.Exists )


alexandergre
March 8th, 2009, 07:36 AM
So I want to copy only *new/edited* files from source folder to the destination folder. I can do a normal copy from from folder A to B. I also can the overwrite method.. But im stuck at this one:


DirectoryInfo dir = new DirectoryInfo(@"" + source);
FileInfo[] filesource = dir.GetFiles("*.*");

DirectoryInfo dir2 = new DirectoryInfo(@"" + destination);
FileInfo[] filedest = dir.GetFiles("*.*");

if (filesource[x].Exists && filesource[x].CreationTime )




if the file dose not exist in folder B then COPY
if the file does exist in folder B, but its creation time is older than the file in folder A, then COPY.

I found this: http://dotnetperls.com/Content/File-Copy-Backups.aspx but couldnt get it.
please help. thanx a lot guys!

enfekted
March 8th, 2009, 03:07 PM
Your going to have to do some manipulation of the file paths.

Step 1: Get a listing of all your files in your source directory
Step 2: Combine the source filename w/ the destination directory
Step 3: If the combined filename doesn't exist or is older than the source, the copy source.

Some code (just winging it... not sure about the exact methods & signatures)

foreach (string path in Directory.GetFiles(sourceDirectory))
{
string filename = Path.GetFileName(path);
string destinationFile = Path.Combine(destinationDirectory, filename);

if (!File.Exists(destinationFile) || GetFileDate(destinationFile) < GetFileDate(path))
{
File.Copy(path, destinationFile);
}
}


hth

JonnyPoet
March 8th, 2009, 04:08 PM
Here is a workable and tested code

public List<FileInfo> CompareFiles(string destination, string source) {
List<FileInfo> filesToCopy = new List<FileInfo>();
DirectoryInfo dir = new DirectoryInfo(@"" + source);
FileInfo[] filesource = dir.GetFiles("*.*");
DirectoryInfo dir2 = new DirectoryInfo(@"" + destination);
FileInfo[] filedest = dir2.GetFiles("*.*");
FileCollection destFiles = new FileCollection();
destFiles.Add(filedest);
for (int i= 0; i < filesource.Length ; i++){
if ( destFiles.DoesntExistOrIsOlderThen(filesource[i])){
filesToCopy.Add(filesource[i]);
}
}
return filesToCopy;
}

public class FileCollection {
private Dictionary<string, FileInfo> _allfiles;
public FileCollection() {
_allfiles = new Dictionary<string, FileInfo>();
}
public void Add(FileInfo[] files) {
for (int i = 0; i < files.Length; i++) {
_allfiles.Add(files[i].Name, files[i]);
}
}
private FileInfo GetFileByKey(string name) {
if (_allfiles.ContainsKey(name)){
return _allfiles[name];
}
return null;
}
/// <summary>
/// We test if a file with the same name exists and if so we test which one is newer
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public bool DoesntExistOrIsOlderThen(FileInfo source) {
FileInfo file = GetFileByKey(source.Name);
// if the file in the destination is older then the file in the source
if (file== null || (file != null && file.CreationTime < source.CreationTime ) ){
// if a file like tha doesn't exist or the source is newer we return true
return true;
}
return false;
}
}
I tested it it works. The output of the method CompareFiles returns a List of FileInfos which are that one you need to copy to the destination

JonnyPoet
March 8th, 2009, 04:20 PM
Your going to have to do some manipulation of the file paths.

Step 1: Get a listing of all your files in your source directory
Step 2: Combine the source filename w/ the destination directory
Step 3: If the combined filename doesn't exist or is older than the source, the copy source.

Some code (just winging it... not sure about the exact methods & signatures)

foreach (string path in Directory.GetFiles(sourceDirectory))
{
string filename = Path.GetFileName(path);
string destinationFile = Path.Combine(destinationDirectory, filename);

if (!File.Exists(destinationFile) || GetFileDate(destinationFile) < GetFileDate(path))
{
File.Copy(path, destinationFile);
}
}


hthGreat and easy method. Wow. To have the correct signatures it looks like

foreach (string path in Directory.GetFiles(sourceDirectory)) {
string filename = Path.GetFileName(path);
string destinationFile = Path.Combine(destinationDirectory, filename);
if (!File.Exists(destinationFile) || File.GetCreationTime(destinationFile) < File.GetCreationTime(path)) {
File.Copy(path, destinationFile, true );
}
}
This is really much easier and per sure quicker then my code :D tested, works now

alexandergre
March 8th, 2009, 05:29 PM
@Jonnypoet thank you so much for your help but I still dont get hole the code!


this is the program:
http://alexandergre.googlepages.com/namnls.PNG

and where shall I paste the code so that when you click on "copy new/edited Files " button the files begin to copy from source to DEST. or what code shall I add to "copy new/edited Files " button to make it work?


Im a beginner... sorry if I ask again. :(
thank you for your help and time. :)

JonnyPoet
March 8th, 2009, 06:20 PM
and where shall I paste the code so that when you click on "copy new/edited Files " button the files begin to copy from source to DEST. or what code shall I add to "copy new/edited Files " button to make it work? ....
Looks nice :wave: You Button 'copy new/edited' needs to get a click delegate and in this delegate you can use the method.
like
private void btCopyEditNew_Click(object sender, EventArgs e) {
string sourceDirectory = txtSourceBox.Text;
string destinationDirectory = txtDestinationBox.Text;
foreach (string path in Directory.GetFiles(sourceDirectory)) {
string filename = Path.GetFileName(path);
string destinationFile = Path.Combine(destinationDirectory, filename);
if (!File.Exists(destinationFile) || File.GetCreationTime(destinationFile) < File.GetCreationTime(path)) {
File.Copy(path, destinationFile, true);
}
}
}In this example txtSourceBox and txtDestinationBox are the textboxes where you have your pathnames and btCopyEditNew is the name of the button you press if you want to get the copy done.
What you see is the click delegate of that button which you get when you doubleclick to the properties section of the button in the events section. This creates the needed delegates method stub.