tags:

views:

228

answers:

2

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.

A: 

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

Bobby
it doesnt like File.Copy(Path.Combine(subFolder, filesToCopy[i]), destinationFolder); keeps outputting Could not find a part of the path
Mike
can someone help me with the above code, it wont copy files at File.Copy(Path.Combine(subFolder, filesToCopy[i]), destinationFolder);
Mike
i change to this File.Copy(subFolder + "\\" + filesToCopy[1], destinationfolder + filesToCopy[1]); and it keeps writting the same file name
Mike
ugh, i should i just made it simple! Thanks Henk
Mike
now quick question, how do i add ".txt" to the file list file, meaning all the files in list are missing .txt so how do i add it to the search function
Mike
@Mike: Sorry, fixed the copying part...somehow forget about that. @Henk: As I said, this was my first idea on this, I'm not sure if it is fast or not.
Bobby
it's fast, really fast
Mike
A: 

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