Move all files in subfolders to another folder

Viewed 89833

My source path is C:\Music\ in which I have hundreds of folders called Album-1, Album-2 etc.

What I want to do is create a folder called Consolidated in my source path.

And then I want to move all the files inside my albums to the folder Consolidated, so that I get all the music files in one folder.

How can I do this?

14 Answers
    private static void MoveFiles(string sourceDir, string targetDir)
    {
        IEnumerable<FileInfo> files = Directory.GetFiles(sourceDir).Select(f => new FileInfo(f));
        foreach (var file in files)
        {
            File.Move(file.FullName, Path.Combine(targetDir, file.Name));
        }
    }
        String directoryName = @"D:\NewAll\";
        DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
        if (dirInfo.Exists == false)
            Directory.CreateDirectory(directoryName);

        List<String> AllFiles= Directory
                           .GetFiles(@"D:\SourceDirectory\", "*.*", SearchOption.AllDirectories).ToList();

        foreach (string file in AllFiles)
        {
            FileInfo mFile = new FileInfo(file);

            // to remove name collisions
            if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
            {
                mFile.MoveTo(dirInfo + "\\" + mFile.Name);
            }
            else
            {
                string s = mFile.Name.Substring(0, mFile.Name.LastIndexOf('.'));

                int a = 0;
                while (new FileInfo(dirInfo + "\\" + s + a.ToString() + mFile.Extension).Exists)
                {
                    a++;
                }
                mFile.MoveTo(dirInfo + "\\" + s + a.ToString() + mFile.Extension);
            }
        }

ToCopyIt is important to mention that you can't use the ".move()" method across volumes.
https://docs.microsoft.com/en-us/dotnet/api/system.io.file.move?view=net-6.0
https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.move?view=net-6.0

Move files:

string DirFrom = @"C:\MyWork";
string DirTo = @"E:\Archive";

DirectoryInfo DirInfoFrom = new DirectoryInfo(DirFrom);
DirectoryInfo DirInfoTo = new DirectoryInfo(DirTo);

if (!DirInfoTo.Exists)
{
    Directory.CreateDirectory(DirTo);
}

foreach (FileInfo FileToCopy in DirInfoFrom.GetFiles())
{
    FileToCopy.CopyTo(DirTo + FileToCopy.Name);
    File.Delete(FileToCopy.FullName);
}

Tested 1/31/22 .NET4.8 VS2019

We already had variant for copying directory structure, so this is just modified version of it for moving:

public static void MoveInner(string sourceDirName, string destDirName, bool moveSubDirs)
{
    var dir = new DirectoryInfo(sourceDirName);
    var dirs = dir.GetDirectories();

    // If the source directory does not exist, throw an exception
    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + sourceDirName);
    }

    // If the destination directory does not exist, create it
    if (!Directory.Exists(destDirName))
        Directory.CreateDirectory(destDirName);


    // Get the file contents of the directory to copy
    var files = dir.GetFiles();

    foreach (var file in files)
    {
        // Create the path to the new copy of the file
        var temppath = Path.Combine(destDirName, file.Name);

        // Move the file.
        file.MoveTo(temppath);
    }

    // If copySubDirs is true, copy the subdirectories
    if (!moveSubDirs)
        return;

    foreach (var subdir in dirs)
    {
        // Create the subdirectory
        var temppath = Path.Combine(destDirName, subdir.Name);

        // Move the subdirectories
        MoveInner(subdir.FullName, temppath, moveSubDirs: true);
    }
}
class Program
{
    static void Main(string[] args)
    {
        movedirfiles(@"E:\f1", @"E:\f2");
    }
    static void movedirfiles(string sourdir,string destdir)
    {
        string[] dirlist = Directory.GetDirectories(sourdir);

        moveallfiles(sourdir, destdir);
        if (dirlist!=null && dirlist.Count()>0)
        {
            foreach(string dir in dirlist)
            {
                string dirName = destdir+"\\"+ new DirectoryInfo(dir).Name;
                Directory.CreateDirectory(dirName);
                moveallfiles(dir,dirName);
            }
        }

    }
    static void moveallfiles(string sourdir,string destdir)
    {
        string[] filelist = Directory.GetFiles(sourdir);
        if (filelist != null && filelist.Count() > 0)
        {
            foreach (string file in filelist)
            {
                File.Copy(file, string.Concat(destdir, "\\"+Path.GetFileName(file)));
            }
        }
    }

}
Related