在搜索子目录在C#中的进一步问题子目录、问题

由网友(久爱不变)分享简介:我问了一个问题,最近在在C#的搜索目录。I asked a question recently on searching directories in C#.在列表中的字符串或多个字符串的所有目录。 尝试使用字符串复制的文件出来。如果失败,则进入下一个字符串,List all the directories in...

我问了一个问题,最近在在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:patest";
            string path2 = @"I:patest2";
            string path3 = @"I:patest3";
            string path4 = @"I:patest4";

            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...

阅读全文

相关推荐

最新文章