Dateien in Ordner verschieben

Ich habe Tausende von Bilddateien in einem Ordner und ich möchte, dass ein Programm automatisch neue Ordner erstellt und fünfzig Dateien in jeden Ordner legt.

Irgendeine Idee?

Antworten (2)

Das klingt nach einem PowerShell-Job. So etwas sollte funktionieren:

# 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 .\$_
}

Wenn Sie C#-Code kompilieren können, schlage ich vor, dieses Programm zu verwenden, das einen Pfad (try .) und eine Zahl ( 50) als Argument akzeptiert. Es werden numerische Unterordner erstellt und Dateien dorthin verschoben. Ich veröffentliche diesen Code ohne Gewährleistung und mitlizenziert als 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>");
            }
        }
    }
}