tags:

views:

73

answers:

5

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 i want to move all the files inside my albums to the folder "Consolidated". So that i get all the music files under one folder.

How can i do this ?

A: 

You loop through them and then simply run Move, the Directory class have functionality for listing contents too iirc.

dutt
+1  A: 

Something like this should get you rolling. You'll have to add error checking and what not (What if there is a subdirectory of source named "Consolidated"? What if Consolidated already exists? Etc.) This is from memory, so pardon any syntax errors, etc.

string source = @"C:\Music";
string[] directories = Directory.GetDirectories(source);
string consolidated = Path.Combine(source, "Consolidated")
Directory.CreateDirectory(consolidated);
foreach(var directory in directories) {
    Directory.Move(directory, consolidated);
}
Jason
A: 

You might be interested in trying Powershell and/or Robocopy to do this task. It'll be a lot more concise than creating a C# application for the task. Powershell is also a great tool for your development tool-belt.

I believe Powershell and Robocopy are both installed by default on Windows Vista and 7.

This might be a good place to start: http://technet.microsoft.com/en-us/library/ee332545.aspx

Brian Hinchey
A: 

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 );
        }
    }
Zachary
+1  A: 

Try like this

        String l_sDirectoryName = "C:\\Consolidated";
        DirectoryInfo l_dDirInfo = new DirectoryInfo(l_sDirectoryName);
        if(l_dDirInfo.Exists ==false )
            Directory.CreateDirectory(l_sDirectoryName);
        List<String> MyMusicFiles = Directory.GetFiles("c:\\Music", "*.*", SearchOption.AllDirectories).ToList();
        foreach (string file in MyMusicFiles)
        {
            FileInfo mFile = new FileInfo(file);
            if(new FileInfo(l_dDirInfo +"\\"+ mFile.Name).Exists==false)//to remove name collusion
                 mFile.MoveTo(l_dDirInfo +"\\"+ mFile.Name);
        }

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

Pramodh