Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move all files in subfolders to another folder

Tags:

c#

directory

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?

like image 910
Anuya Avatar asked Oct 12 '10 03:10

Anuya


People also ask

How do you remove all files from subfolders and relocate them to one folder?

To quicken up this process, Click Ctrl + A, Highlight all the files (that you want to move) and right click, Cut and Paste the files to the desired directory.

Is there a way to move files from multiple folders at once?

Once the files are visible, press Ctrl-A to select all of them, then drag and drop them to the right location. (If you want to copy the files to another folder on the same drive, remember to hold down Ctrl while you drag and drop; see The many ways to copy, move, or delete multiple files for details.)


2 Answers

Try like this

String directoryName = "C:\\Consolidated";
DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
if (dirInfo.Exists == false)
    Directory.CreateDirectory(directoryName);

List<String> MyMusicFiles = Directory
                   .GetFiles("C:\\Music", "*.*", SearchOption.AllDirectories).ToList();

foreach (string file in MyMusicFiles)
{
    FileInfo mFile = new FileInfo(file);
    // to remove name collisions
    if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false) 
    {
         mFile.MoveTo(dirInfo + "\\" + mFile.Name);
    }
}

It will get all the files in the "C:\Music" folder (including files in the subfolder) and move them to the destination folder. The SearchOption.AllDirectories will recursively search all the subfolders.

like image 62
Thorin Oakenshield Avatar answered Oct 09 '22 19:10

Thorin Oakenshield


You can use the Directory object to do this, but you might run into problems if you have the same file name in multiple sub directories (e.g. album1\1.mp3, album2\1.mp3) so you might need a little extra logic to tack something unique onto the names (e.g. album1-1.mp4).

    public void CopyDir( string sourceFolder, string destFolder )
    {
        if (!Directory.Exists( destFolder ))
            Directory.CreateDirectory( destFolder );

        // Get Files & Copy
        string[] files = Directory.GetFiles( sourceFolder );
        foreach (string file in files)
        {
            string name = Path.GetFileName( file );

            // ADD Unique File Name Check to Below!!!!
            string dest = Path.Combine( destFolder, name );
            File.Copy( file, dest );
        }

        // Get dirs recursively and copy files
        string[] folders = Directory.GetDirectories( sourceFolder );
        foreach (string folder in folders)
        {
            string name = Path.GetFileName( folder );
            string dest = Path.Combine( destFolder, name );
            CopyDir( folder, dest );
        }
    }
like image 38
Zachary Avatar answered Oct 09 '22 20:10

Zachary