-3

My source path is C:\images\ in which I have hundreds of folders called Album-1, Album-2 etc. I create a target path F:\AllPics. And then I want to move all the files inside my albums to the target path, so that I get all the images in one folder with subfolder names like album-1_img1,album2-img2. How can I do this ?

halfer
  • 19,824
  • 17
  • 99
  • 186
lightcoder
  • 43
  • 1
  • 8

2 Answers2

0

Look at the File & Directory classes. You can iterate through the files in a directory similar to this (may need tweeks) There are options to include directories etc.

// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
    ProcessFile(fileName);

// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
    ProcessDirectory(subdirectory);
AntDC
  • 1,807
  • 14
  • 23
  • `Directory.GetFiles()` takes a path at the very least and then a search pattern. You have also missed `"` as well. – uTeisT Aug 22 '16 at 11:52
0
namespace MassFileMoverConsole
{
    class Program
    {
        string _sourcePath;
        string _targetPath;

        static void Main(string[] args)
        {
            Program massMover = new Program();
            massMover.MoveThemAll();
        }

        void MoveThemAll()
        {
            Console.WriteLine("Enter source path : ");
            _sourcePath  = Console.ReadLine();
            Console.WriteLine("Enter target path : ");
            _targetPath = Console.ReadLine();

            var subFolderNamesTargetPath = Directory.GetDirectories(_sourcePath);
            foreach(var subFolderName in subFolderNamesTargetPath)
            {
                var subFolder = new DirectoryInfo(subFolderName);
                var subFolderFiles = subFolder.GetFiles();
                foreach(var subFolderFile in subFolderFiles)
                {
                    var fileNewName = subFolder.Name + "_" + subFolderFile.Name;
                    subFolderFile.CopyTo(Path.Combine(_targetPath, fileNewName));
                }
            }

        }
    }
}
Arctic Vowel
  • 1,492
  • 1
  • 22
  • 34
  • 1
    @lightcoder Happy to hear that, you're welcome! I think you got some downvotes on this question since it doesn't contain any visible code/research effort, which is generally frowned upon in SO. To avoid this in future questions it's better to look through tutorials, try to write code, and post a question if/when you get stuck. Welcome to SO :) – Arctic Vowel Aug 22 '16 at 16:07