Hi, I want to copy all the video files from one directory (source folder) to another directory (destination folder) which i have already done. Code is given below.

Now i want to put some condition.
For Example, I want to read first the text file (videoID.txt)in which all the video ID's are given line by line. now i want to match each video id against the source video folder, if it matches then copy that video to destination folder otherwise don't copy. Please help me to insert this condition in this code. thanks

static void Main(string[] args)
        {
            string fileName;
            string destFile;
            string sourcePath = @"E:\Source";
            string targetPath = @"E:\Destination";

            // To copy all the files in one directory to another directory.

            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }

I did it using the following:

            string sourcePath = @"E:\Source";
            string targetPath = @"E:\Destination";

            StreamReader strmReader = new StreamReader(@"E:\Source\videoID.txt");

            string videoIDs = strmReader.ReadToEnd();

            videoIDs = Regex.Replace(videoIDs, "\\r\\n", ",");

            string[] individualVidIDs = videoIDs.Split(',');

            foreach (string videoID in individualVidIDs)
            {
                Console.WriteLine("Checking if video {0} exists...", videoID);
                string tempFilePath = sourcePath + "\\" + videoID + ".txt";
                if (File.Exists(tempFilePath))
                {
                    Console.WriteLine("Video {0} has been found...", videoID);

                    string fileName = System.IO.Path.GetFileName(tempFilePath);
                    string destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(tempFilePath, destFile, true);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Video {0} has NOT been found!", videoID);
                    Console.WriteLine();
                }
            }

This assumes that the videoID file is literally seperated by new lines and doesnt have like a comma etc.

Else the RegEx wouldnt be needed to remove the newline escape codes.

I used files like this to test: 10001.txt

Please tell me one more thing.
Video ID's are like 111001, 111002, 111003 in the source directory. Here you can see the first three numbers are same but the last three numbers varying starting from 001 to 003. It can also go to 200. But in videoID.txt, the first three numbers are given, For Example, If 111 is given. Then It should search in the source directory and copy 111001, 111002,,,,,,111200.
Here I mean to say, In the source directory video's will always be given in this format where the last three numbers will be added in ascending order with the actual videoID.

Thanks!

Updated code taking into account new requirement.

        static void Main(string[] args)
        {
            string sourcePath = @"E:\Source\";
            string targetPath = @"E:\Destination\";

            StreamReader strmReader = new StreamReader(sourcePath + "videoID.txt");

            string videoIDs = strmReader.ReadToEnd();

            videoIDs = Regex.Replace(videoIDs, "\\r\\n", ",");

            string[] individualVidIDs = videoIDs.Split(',');
            string[] tempArray = Directory.GetFiles(sourcePath);
            List<string> filesInFolder = new List<string>();
            foreach (string fileName in tempArray)
            {
                filesInFolder.Add(fileName);
            }
            filesInFolder.Remove(sourcePath + "videoID.txt");

            foreach (string videoID in individualVidIDs)
            {
                Console.WriteLine("VideoID = {0}", videoID);
                Console.WriteLine();

                foreach (string fileName in filesInFolder)
                {
                    if (fileName.Substring(fileName.LastIndexOf('\\') + 1, 3) == videoID)
                    {
                        Console.WriteLine("Video {0} has been found.", fileName.Substring(fileName.LastIndexOf('\\') + 1));

                        string destFile = targetPath + "\\" + (fileName.Substring(fileName.LastIndexOf('\\') + 1));
                        System.IO.File.Copy(fileName, destFile, true);
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("Video {0} does not match VideoID!", fileName.Substring(fileName.LastIndexOf('\\') + 1));
                        Console.WriteLine();
                    }
                }
            }

            Console.ReadLine();
        }

Edited 4 Years Ago by Mike Askew

Hi Mike, Sorry your solution is not working. I think you didn't get my point.

From what I understood:

  • Text file storing first 3 digits of VideoID
  • Read from text file
  • Match first three digits to VideoID's of files
  • If match then copy to output directory

Where did I go wrong?

Sorry Mike, I was doing some mistake. Your solution is absolutely right.
Now, it is my last question related to this topic. If we want to make reverse this process.
For example, Reading a directory in which jpg files are present like this (345001.jpg, 345002.jpg,.....345200.jpg and 346001.jpg, 346002.jpg,,,,,346200.jpg). This time we have to copy only the base file names to the text file i.e., only 345 and 346 should be written to the text file.
Thanks

No, Please leave this option on run time. I will set the filename each time.
Thanks

That should do it for you. The output filename is passed in the method call, the folder paths can be changed using the variables.

        static private void ExtractJPEGNames(string OutputFileName) // eg. "PicIDs.txt"
        {
            string sourcePath = @"E:\Source\";
            string targetPath = @"E:\Destination\";
            StreamWriter StrmWtr = new StreamWriter(targetPath + OutputFileName);

            string[] tempArray = Directory.GetFiles(sourcePath);
            List<string> filesInFolder = new List<string>();
            foreach (string fileName in tempArray)
            {
                string tempID = fileName.Substring(fileName.LastIndexOf('\\') + 1, 3);

                if (!filesInFolder.Contains(tempID))
                    filesInFolder.Add(tempID);
            }

            foreach (string fileID in filesInFolder)
            {
                StrmWtr.WriteLine(fileID);
            }

            StrmWtr.Flush();
            StrmWtr.Close();
        }

Edited 4 Years Ago by Mike Askew

This question has already been answered. Start a new discussion instead.