1

I have thousands of image files in a folder and I want a program to automatically create new folders and put fifty files in each folder.

Any idea?

2 Answers 2

1

This sounds like a PowerShell job. Something like this should work:

# Create 100 folders
1..100 | % {New-Item -Name $_ -ItemType directory}

# select 50 files and move them into folder 1, 2, 3 etc until we hit 100
1..100 | % {
$files = ls | select -First 50
Move-Item $files .\$_
}
0

If you can compile C# code, I suggest using this program which takes a path (try .) and a number (50) as argument. It will create numeric subfolders and move files there. I release this code without warranty and co-licensed as CC-0.

using System;
using System.IO;

namespace MoveNFilesToFolder
{
    static class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var path = args[0];
                int n = int.Parse(args[1]);
                var files = Directory.GetFiles(path, "*.*");
                int fileno = 0;
                foreach (var file in files)
                {
                    var subdir = Directory.CreateDirectory(Path.Combine(path, "" + (fileno++/n)));
                    var targetfile = Path.Combine(subdir.FullName, Path.GetFileName(file));
                    try
                    {
                        File.Move(file, targetfile);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Could not move " + file + " to " + targetfile);
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("MoveNFilesToFolder <path> <count>");
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.