使用通配符复制多个文件并保留文件名的C#代码

8

我需要使用一个不包含完整信息的文本文件从目录中复制多个文件。

NCR.txt:
红色

目标目录中有:
red1.txt
red3.txt
red44.txt

目标目录需要有:
red1.txt
red3.txt
red44.txt

我的代码:

System.IO.Directory.CreateDirectory(@"C:\nPrep\" + textBox1.Text + "\\red");
        if (checkBox3.Checked)
        {
            String[] file_names = File.ReadAllLines(@"C:\NCR.txt");

            foreach (string file_name in file_names)
            {
                string[] files = Directory.GetFiles(textBox2.Text, file_name + "*.txt");
                foreach (string file in files)
                    System.IO.File.Copy(file, @"C:\nPrep\" + textBox1.Text + "\\red\\");
            }

        }

6
你的问题是什么? - SKG
请提供简化的代码。不确定Checkbox3和Textbox2的作用。 - SKG
它不保留我所拥有的文件名,我需要知道如何使System.IO.File.Copy(file, @"C:\IProProductionPrep" + textBox1.Text + "\redactions\"); 保留原始文件名。我只能让目标目录最终得到red.txt。 - user222427
checkBox3 和 textBox2 工作得非常完美,问题在于当我复制并保留原始文件名时。 - user222427
1
System.IO.File.Copy(file, @"C:\nPrep" + textBox1.Text + "\red\"); 需要保留原始文件名... - user222427
3个回答

24
//FileInfo & DirectoryInfo are in System.IO
//This is something you should be able to tweak to your specific needs.

static void CopyFiles(DirectoryInfo source, 
                      DirectoryInfo destination, 
                      bool overwrite, 
                      string searchPattern)
{
    FileInfo[] files = source.GetFiles(searchPattern);

    //this section is what's really important for your application.
    foreach (FileInfo file in files)
    {
        file.CopyTo(destination.FullName + "\\" + file.Name, overwrite);
    }
}

这个版本更容易复制粘贴:

static void Main(string[] args)
{
    DirectoryInfo src = new DirectoryInfo(@"C:\temp");
    DirectoryInfo dst = new DirectoryInfo(@"C:\temp3");

    /*
     * My example NCR.txt
     *     *.txt
     *     a.lbl
     */
    CopyFiles(src, dst, true);
}

static void CopyFiles(DirectoryInfo source, DirectoryInfo destination, bool overwrite)
{
    List<FileInfo> files = new List<FileInfo>();

    string[] fileNames = File.ReadAllLines("C:\\NCR.txt");

    foreach (string f in fileNames)
    {
        files.AddRange(source.GetFiles(f));
    }

    if (!destination.Exists)
        destination.Create();

    foreach (FileInfo file in files)
    {
        file.CopyTo(destination.FullName + @"\" + file.Name, overwrite);
    }
}

不是复制所有文件,目录中将包含不仅是red*.txtm,还有yellow*.txt green*.txt。我列出的格式完美地读取文件并解析目录,问题在于找到通配符后复制完整文件名。 - user222427
它还只复制ncr.txt中的一个文件,代码大多数情况下会复制所有与ncr.txt匹配的文件,并使用现有文件名进行复制。 - user222427
2
Mike,请注意。Austin为您提供了一个完美的复制文件的方法,您只需要使用red*.txtyellow*.txt等来调用它即可。 - H H

3
所有建议都非常好,感谢所有的建议,但这个很完美:
if (checkBox3.Checked)
{
    string[] lines = File.ReadAllLines(@"C:\NCR.txt");

    foreach (string line in lines)
    {
        string[] files = Directory.GetFiles(textBox2.Text, line + "*.txt");
        foreach (string file in files)
        {
             FileInfo file_info = new FileInfo(file);
             File.Copy(file, @"C:\InPrep\" + textBox1.Text + "\\text\\" + file_info.Name);
        }
    }
}

1
为了让后续的人更容易理解,考虑给checkBox3textBox2起更好的名字。 - Austin Salonen
这不就是 Austin 提供的方法的硬编码版本吗? - Ben Mosher

2
    string sourceDir = @"c:\";
    string destDir = @"c:\TestDir";
    var r = Directory.GetFiles(sourceDir, "red*.txt"); //Replace this part with your read from notepad file

    foreach (var s in r)
    {
        var sourceFile = new FileInfo(s);
        sourceFile.CopyTo(destDir + "\\" + s.Replace(sourceDir, string.Empty));
    }

正如Mike所提到的那样,System.IO.File.Copy(file, @"C:\nPrep" + textBox1.Text + "\red\")看起来不正确。 - SKG
从技术上讲,在执行foreach之前应该检查r.Length > 0,否则如果没有文件,就会出现异常。这是为那些可能会在文件存在与否的情况下尝试使用此代码的人准备的。 - vapcguy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接