开发者

Further questions on Searching subdirectories in C#

I asked a question recently on searching directories in C#.

Program Logic

  • 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,
  • If it succeeds in finding the file:
    • copy
    • skip to next entry in text file

End Result

If it finds it on the 4th string it doesn’t try the next 15 strings.

Code Snippet

 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");
            }
        }

The code above does not contain the ability to write out failed files, I'm not sure how to do that.

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);
    }
}

Edit: Fixed the wrong file copying...


This is a simple class i´ve made for finding files of a specific type.

It stores the results in a Dictionaty,List> where the first string is the full pathname of the directory and the list are the fullpaths of each file.

After that you just have to copy and paste the files to another location...

public class CheckForFile
    {
        private Dictionary<String, List<String>> FileDirectoryList;
        private String StartLocation;
        private String Format;

        public CheckForFile(String StartLocation, String Format)
        {
            this.FileDirectoryList = new Dictionary<string, List<string>>();
            this.StartLocation = StartLocation;
            this.Format = Format;
        }

        public void Discover()
        {
            DirectoryInfo di = new DirectoryInfo(this.StartLocation);
            DirectoryInfo[] ChildDirectoryList = di.GetDirectories("*", SearchOption.AllDirectories);

            foreach(DirectoryInfo ChildDir in ChildDirectoryList)
            {
                Console.WriteLine(ChildDir.FullName);
                FileInfo[] FileInfoList = ChildDir.GetFiles(this.Format);

                this.FileDirectoryList.Add(ChildDir.FullName, new List<string>());

                foreach (FileInfo ChildFileInfo in FileInfoList)
                {
                    Console.WriteLine("\t\t" + ChildFileInfo.Name);
                    this.FileDirectoryList[ChildDir.FullName].Add(ChildFileInfo.FullName);
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜