File.Move / File.Copy nothing happening

66 views Asked by At

I am trying to write a code to move (or copy) some files into some folders (Actually to categorize those files by their extensions.)

Everything in the code works fine (Scanning folder and discovering files, extracting their extensions and creating folders and naming them with extensions) except when it is supposed to move/copy files into corresponding folder. It does nothing regarding this task.

Here is the code (In case you need files and folders to test my code, a link to a zipped file containing some sample file & folder is provided at the end of the question.)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ramezanian_VCS_SysProg_Course
{
    class Program
    {
        static void Main()
        {
            /* string variables to hold working directory path and 
                an array of filenames and a list of extensions. */
            String ThisPath = @"E:\HDR1";
            String[] ThisFiles = null;
            List<string> NotDottedExtensionContainer = new List<string>();

            //Getting filevnames with their full path.
            ThisFiles = Directory.GetFiles(ThisPath);

            /* Creating a text log file and also a list of file extensions while trimming and capitalizing them
              like this: file.txt -> .txt -> TXT */
            try
            {
                using (StreamWriter ThisLogFile = new StreamWriter(Path.Combine(ThisPath, "Log_File.txt")))
                //
                foreach (String _F in ThisFiles)
                    {
                        NotDottedExtensionContainer.Add(Path.GetExtension(_F).TrimStart('.').ToUpper());
                        ThisLogFile.WriteLine(_F);
                    }

            }
            catch (Exception)
            {

                throw; //In case something oes wrong.
            }

            UInt16 Temp_CountOfExtensions = 0; //A temporary variable to count how many extensions are there and so
            // how many folders will be created then.

            ThisPath = @"E:\HDR1\";
            //Making a distinct collection of unique extensions...
            IEnumerable<string> UniqueExtensionsCollection = NotDottedExtensionContainer.Distinct();
            foreach (String TempItem1 in UniqueExtensionsCollection)
            {
                Console.WriteLine(TempItem1);
                //Then creating based on that distinct list.
                Directory.CreateDirectory(ThisPath + TempItem1);
                Temp_CountOfExtensions++;
            }
            foreach (String TempItem2 in UniqueExtensionsCollection)
            {
                foreach (String TempItem3 in ThisFiles)
                {
                    //This compares each file extension with subfolder names...
                    if (Path.GetExtension(TempItem3).TrimStart('.').ToUpper() == TempItem2)
                    {
                        //... To move them into their right place...
                        File.Move(TempItem3, ThisPath + TempItem2 + Path.GetFileName(TempItem3));
                    }
                    //...but nothing happens!
                }
            }
            Console.WriteLine("Total of {0} subfolders were created in {1}.",Temp_CountOfExtensions, ThisPath);
            Console.ReadLine();
        }
    }
}

And this is the output (All folders are created successfully with desired names.):

Link to output screenshot

A sample working directory to test the code: CLICK HERE

1

There are 1 answers

2
Evelyn On

You need a slash between the extension folder and the file

File.Move(TempItem3, ThisPath + TempItem2 + "\\" + Path.GetFileName(TempItem3));

As long as the second path doesn't start with a backslash you can just use Path.Combine for this stuff too,

File.Move(TempItem3, Path.Combine(ThisPath, TempItem2, Path.GetFileName(TempItem3)));