在搜索子目录在C#中的进一步问题 [英] Further questions on Searching subdirectories in C#
问题描述
我问了一个问题,最近在在C#的搜索目录。
I asked a question recently on searching directories in C#.
- 在列表中的字符串或多个字符串的所有目录。
- 尝试使用字符串复制的文件出来。
- 如果失败,则进入下一个字符串,
- List all the directories in a string, or multiple strings.
- Try to copy the file out using the string.
- If it fails, proceed to the next string,
- 复制
- 跳到文本文件中下一个条目
如果它发现它的第四根弦就不会尝试在接下来的15串。
If it finds it on the 4th string it doesn’t try the next 15 strings.
if (checkBox2.Checked) { string[] file_names = File.ReadAllLines(@"c:\dact.txt"); string path1 = @"I:\pa\test\"; string path2 = @"I:\pa\test2\"; string path3 = @"I:\pa\test3\"; string path4 = @"I:\pa\test4\"; string full = (string.Concat(path1, file_names, ".txt")); foreach (string file_name in file_names) if (System.IO.File.Exists(full)) { foreach (string file in file_names) System.IO.File.Copy(file, @"C:\" + textBox1.Text + @"\\OC\" + file_name + ".txt"); } }
在code以上不包含写出来没有文件的能力,我不知道该怎么做。
The code above does not contain the ability to write out failed files, I'm not sure how to do that.
这就是我想要的: 1.阅读的文本文件in所有的行 2.尝试从一个特定的目录
复制文件 3.无论文件失败,它写出到一个文本文件中失败的文件
4.它读取所有新的失败列表,然后改掉我的列表中的第二个路径。
5.重复处理,以第三条道路 - 19路。This is what I want: 1. Read all lines of a text file
2. Try to copy files from a specific directory
3. Whatever files fails, it writes out to a text file the failed files
4. it reads all the new "failed list", then trys the 2nd path in my list.
5. repeats process to third path - 19th path.推荐答案
这是我对这个第一个想法:
This would be my first idea on this:
String[] filesToCopy = File.ReadAllLines("yourFileHere.txt"); String sourceFolder = "yourSourceFolderHere"; String destinationFolder = "yourDestinationFolderHere"; foreach (String subFolder in Directory.GetDirectories(sourceFolder)) { for (int i = 0; i < filesToCopy.Length; i++) { if (!String.IsNullOrEmpty(filesToCopy[i])) { if (File.Exists(Path.Combine(subFolder, filesToCopy[i]))) { File.Copy(Path.Combine(subFolder, filesToCopy[i]), Path.Combine(destinationFolder, FilesToCopy[i])); filesToCopy[i] = string.Empty; } } } } using (StreamWriter strm = new StreamWriter("yourOutputFile.txt")) { foreach (String fileToCopy in filesToCopy) { if (!String.IsNullOrEmpty(fileToCopy)) strm.WriteLine(fileToCopy); } }
编辑:修正了错误的文件复制...
Fixed the wrong file copying...
这篇关于在搜索子目录在C#中的进一步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!