C#使用通配符复制多个文件并保留文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1835578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# copying multiple files with wildcards and keeping file names
提问by
I need to copy multiple files from a directory using a textfile that doesnt contain complete info.
我需要使用不包含完整信息的文本文件从目录中复制多个文件。
NCR.txt:
Red
NCR.txt:
红色
target directory has in it:
red1.txt
red3.txt
red44.txt
目标目录中有:
red1.txt
red3.txt
red44.txt
dest directory needs to have:
red1.txt
red3.txt
red44.txt
dest目录需要有:
red1.txt
red3.txt
red44.txt
My code:
我的代码:
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\");
}
}
采纳答案by SKG
All suggestions were great and thansk for all the advise but this was perfect:
所有的建议都很棒,感谢所有的建议,但这很完美:
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);
}
}
}
回答by Austin Salonen
//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);
}
}
This version is more copy-paste ready:
这个版本更适合复制粘贴:
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);
}
}
回答by SKG
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));
}