1
votes

I am using Windows 10 and I have two folders namely Source and Destination. The Destination folder is inside a Dropbox. I have two methods CopySourceFilesToDestination and SynchronizeSourceAndDestination. Fist method copies all folders and files from source to the destination while the second method checks whether the particular filename is present or not in Source folder and if it didn't find the filename in the source it deletes the particular file from the Destination folder. Now I have few files named as below and I don't need to care about the content in the files.

E:\Source\A0000000001\20162356312-Future of Utopia in History. Hayden White. Historein 7.pdf

E:\Source\T0000000142\20162350775-Étienne Geoffroy Saint-Hilaire, 1772-1844 a visionary naturalist. Hervé Le Guyader.pdf

E:\Source\T0000000403\2016242657-Reveries of the solitary walker; Botanical writings; and Letter to Franquières. Jean Jacques Rousseau.pdf

E:\Source\T0000000428\2016243154-Science of Literature- essays on an incalculable difference.Helmut Müller-Sievers.pdf

When I run my program copies files to the Destination but my SynchronizeSourceAndDestination method deletes all the files expect the first file in the list which doesn't contain any UTF-8 characters.

using System;
using System.IO;

namespace DropboxDemo
{
    class Program
    {
        private static string lookupDirectory = @"E:\Source";
        private static string backupDirectory = @"C:\Users\SIMANT\Dropbox \Destination";
        static void Main(string[] args)
        {
            Console.WriteLine("Please wait while copying files.");
            CopySourceFilesToDestination(lookupDirectory);

            Console.WriteLine("Please wait while synchronizing files.");
            SynchronizeSourceAndDestination(backupDirectory);

            Console.ReadLine();
        }

        public static void SynchronizeSourceAndDestination(string dir)
        {
            foreach (string file in Directory.GetFiles(dir))
            {
                string destFilePath = file.Replace(backupDirectory, lookupDirectory);

                if (!File.Exists(destFilePath))
                {
                    // Delete file from Backup
                    File.Delete(file);
                }
            }

            foreach (string directory in Directory.GetDirectories(dir))
            {
                string destinationDirectory = directory.Replace(backupDirectory, lookupDirectory);

                if (!Directory.Exists(destinationDirectory))
                {
                    Directory.Delete(directory, true);
                    continue;
                }
                SynchronizeSourceAndDestination(directory);
            }
        }
        public static void CopySourceFilesToDestination(string dir)
        {
            foreach (string file in Directory.GetFiles(dir))
            {
                string destFilePath = file.Replace(lookupDirectory, backupDirectory);

                if (!File.Exists(destFilePath))
                {
                    File.Copy(file, destFilePath);
                }
                else
                {
                    // Override the existing file                        
                    File.Copy(file, destFilePath, true);
                }
            }

            foreach (string directory in Directory.GetDirectories(dir))
            {
                //Create directory if not present in the destination
                string destinationDirectory = directory.Replace(lookupDirectory, backupDirectory);
                if (!Directory.Exists(destinationDirectory))
                {
                    Directory.CreateDirectory(destinationDirectory);
                }
                CopySourceFilesToDestination(directory);
            }
        }
    }
}

In the second time, I just copied all files from Destination (which is inside Dropbox) to the Source folder and rerun the program and now it doesn't delete the files. Why am I getting this behaviour? I think when a file is copied to Dropbox, it represents the same file names (what we see from my eyes) in a different way. Could you please help me overcome this issue?

1

1 Answers

0
votes

To make my solution workable I changed extended ASCII character by pressing É (Alt + 144), é (Alt + 130). I think it was because the file creator did some copy and paste of the characters directly.