C#如果文件名的一部分与文件夹名称匹配,则将文件移动到文件夹中

时间:2016-12-01 16:58:54

标签: c# regex file directory move

如果.CID文件名的一部分与目录名的一部分匹配,我有.CID文件要移动到目录中。

以下是一些示例目录:

S002016-P063    // projectnumber-panelnumber
S002016-P066
S002016-P067

以下是一些示例文件名:

P063-1.LEFT.CID    // move to S002016-P063 directory
P063-10.RIGHT.CID    // move to S002016-P063 directory
P066-2_P066-5.LEFT.CID    // move to S002016-P066 because P066 == P066
P067-12.LEFT.CID    // move to S002016-P067
P067-5_P063-2.RIGHT.CID    // leave file where it’s at because P067 != P063

现在我将这些文件物理移动到相应的目录中,但我认为可以使用控制台应用程序完成。我想知道我是否需要使用正则表达式类和可能的匹配方法设置一些if语句?我是C#的新手,希望得到一些帮助。这是我到目前为止所拥有的。基本上,控制台应用程序将列出指定目录中的目录和文件。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello and Welcome to the File Sorting Program"); // Displays Text in Quotes

        Console.WriteLine(); // Adds a Space  

        string[] fileEntries = Directory.GetDirectories(@"\\Users\none\Documents\File Sorter"); // Regex for panel number = -([A-Z0-9a-z\-]+)

        System.Console.WriteLine("Directories in the Folder: ");

        Console.WriteLine(); // Adds a Space

        foreach (string fileName in fileEntries)  
        {
            Console.WriteLine(Path.GetFileName(fileName)); // Lists the directories found in the "File Sorter" directory
        }

        Console.WriteLine(); // Adds a Space
        Console.WriteLine(); // Adds a Space

        string[] files = Directory.GetFiles(@"\\Users\none\Documents\File Sorter", "*.CID"); // Regex for panel number = ([A-Z0-9a-z\-]+)-

        System.Console.WriteLine("Files in this Folder: "); // Displays Text in Quotes

        Console.WriteLine(); // Adds a Space

        foreach (string file in files)
        {
            Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file)); // Lists each file minus .CID found in the "File Sorter" directory
        }

        Console.WriteLine("Press any key to exit."); // Displays Text in Quotes

        System.Console.ReadKey();       
    }
}

2 个答案:

答案 0 :(得分:0)

假设您的文件夹名称和文件名称模式一致,这应该可以为您提供您正在寻找的内容:

string root = @"\\Users\none\Documents\File Sorter";

string[] directories = Directory.GetDirectories(root);
var dirRef = directories.ToDictionary(k => k.Split('-')[1], v => v);

string[] files = Directory.GetFiles(root);
foreach (var file in files)
{
    string fileName = Path.GetFileName(file);
    string projectName = null;
    foreach (Match m in Regex.Matches(fileName, "[A-Za-z0-9]+(?=-)"))
    {
        if (projectName == null)
        {
            projectName = m.Value;
        }
        else if (projectName != m.Value)
        {
            projectName = null;
            break;
        }
    }

    if (projectName == null)
    {
        Console.WriteLine("Project names in file {0} do not match.", fileName);
    }
    else if (dirRef.ContainsKey(projectName))
    {
        File.Move(file, dirRef[projectName] + "\\" + fileName);
        Console.WriteLine("File {0} moved to directory {1}.", fileName, dirRef[projectName]);
    }
    else
    {
        Console.WriteLine("Directory with project name {0} doesn't exist", projectName);
    }
}

答案 1 :(得分:0)

我已经进行了一些测试,而这个代码段使用一些Regex处理上述所有情况。显然它可以改进,但它是你工作的起点:)

var projectFolders = Directory.GetDirectories(@"Projects/");
var fileEligibleToBeMoved = new List<string>();

var files = Directory.GetFiles(Directory.GetCurrentDirectory());

foreach (var file in files)
{
    if (!file.Contains(".CID"))
        continue;
    var fileName = Path.GetFileName(file);
    var matches = Regex.Matches(fileName, @"(P[a-zA-Z0-9]+)");

    if (matches.Count == 0)
        continue;

    var isEligible = true;
    var previous = string.Empty;
    foreach (Match match in matches)
    {
        if(previous == string.Empty)
        {
            previous = match.Value;
            continue;
        }
        if (previous != match.Value)
            isEligible = false;
    }
    if (isEligible)
        fileEligibleToBeMoved.Add(file);                
}

foreach (var dir in projectFolders)
{
    var panelTag = Regex.Match(dir, @"-\w+").Value.Replace("-", "");                
    var filesToMove = fileEligibleToBeMoved
        .Where(file => file.Contains(panelTag))
        .ToArray();
    foreach (var file in filesToMove)
    {

        File.Move(file, string.Format("{0}/{1}", dir, Path.GetFileName(file)));
    }
}