I need to copy files from one folder to another but only if their file name is also in a text file. The text file is set up like so
我需要将文件从一个文件夹复制到另一个文件夹,但前提是它们的文件名也在文本文件中。文本文件设置如此
file1.jpg
file2.jpg
file3.jpg
etc
There are around one-million files to copy. I'm using C#.
有大约一百万个文件要复制。我正在使用C#。
What would the best way to go about this be? I'm not sure if I should first read all the file names from the text file and put them in to a list then possibly convert the list in to an array then maybe use the array somehow? Or maybe there's a better way to go about it?
最好的方法是什么?我不确定我是否应该首先从文本文件中读取所有文件名并将它们放入列表然后将列表转换为数组然后可能以某种方式使用该数组?或者也许还有更好的方法吗?
I know how to read and write to files and how to copy from one source destination to another. I don't know how to filter out specific files when copying from one source destination to another though.
我知道如何读取和写入文件以及如何从一个源目标复制到另一个源目标。我不知道如何在从一个源目标复制到另一个源目标时过滤掉特定文件。
Any help is greatly appreciated.
任何帮助是极大的赞赏。
Thank you.
谢谢。
2 个解决方案
#1
1
The following code will help you the process you want
以下代码将帮助您完成所需的过程
string source = @"C:\SourcePath\";
string destination = @"C:\DestinationPath\";
string[] strFiles = File.ReadAllText(@"C:\Filename.txt").Split(' ');
for (int i = 0; i < strFiles.Length; i++)
{
File.Copy(source + strFiles[i], destination + strFiles[i]);
}
#2
0
If the text file is one line with million files name. Use this
如果文本文件是一行有百万个文件名。用这个
string from = @"c:\from" , to =@"d:\to"; // source and destination
StreamReader file = new StreamReader(@"c:\list.txt"); // your files list
string total=file.ReadLine();
string[] tobecopied = total.Split(' ');
foreach(string fil in tobecopied)
{
if(File.Exists(from+@"\"+fil))
{
File.Copy(from+@"\"+fil,to+@"\"+fil);
}
else
{
MessageBox.Show(fil+"Not found ");
}
}
But if the text file have 1 line per 1 file , for example
但是,如果文本文件每1个文件有1行,例如
FIle1.exe
FIle1.exe
File2.exe
File2.exe
use this
用这个
string from = @"c:\from" , to =@"d:\to"; // source and destination
StreamReader file = new StreamReader(@"c:\list.txt"); // your files list
string total="";
string temp="";
while((temp=file.ReadLine())!=null)
{
total+=temp+" ";
}
string[] tobecopied = total.Split(' ');
foreach(string fil in tobecopied)
{
if(File.Exists(from+@"\"+fil))
{
File.Copy(from+@"\"+fil,to+@"\"+fil);
}
else
{
MessageBox.Show(fil+"Not found ");
}
}
These ways also check for file existance.
这些方法还检查文件是否存在。
Hope it works. If someone see error please edit it.
希望它有效。如果有人看到错误,请编辑它。
#1
1
The following code will help you the process you want
以下代码将帮助您完成所需的过程
string source = @"C:\SourcePath\";
string destination = @"C:\DestinationPath\";
string[] strFiles = File.ReadAllText(@"C:\Filename.txt").Split(' ');
for (int i = 0; i < strFiles.Length; i++)
{
File.Copy(source + strFiles[i], destination + strFiles[i]);
}
#2
0
If the text file is one line with million files name. Use this
如果文本文件是一行有百万个文件名。用这个
string from = @"c:\from" , to =@"d:\to"; // source and destination
StreamReader file = new StreamReader(@"c:\list.txt"); // your files list
string total=file.ReadLine();
string[] tobecopied = total.Split(' ');
foreach(string fil in tobecopied)
{
if(File.Exists(from+@"\"+fil))
{
File.Copy(from+@"\"+fil,to+@"\"+fil);
}
else
{
MessageBox.Show(fil+"Not found ");
}
}
But if the text file have 1 line per 1 file , for example
但是,如果文本文件每1个文件有1行,例如
FIle1.exe
FIle1.exe
File2.exe
File2.exe
use this
用这个
string from = @"c:\from" , to =@"d:\to"; // source and destination
StreamReader file = new StreamReader(@"c:\list.txt"); // your files list
string total="";
string temp="";
while((temp=file.ReadLine())!=null)
{
total+=temp+" ";
}
string[] tobecopied = total.Split(' ');
foreach(string fil in tobecopied)
{
if(File.Exists(from+@"\"+fil))
{
File.Copy(from+@"\"+fil,to+@"\"+fil);
}
else
{
MessageBox.Show(fil+"Not found ");
}
}
These ways also check for file existance.
这些方法还检查文件是否存在。
Hope it works. If someone see error please edit it.
希望它有效。如果有人看到错误,请编辑它。