locked
FileInfo not looking in correct file path... RRS feed

  • Question

  • Hi all,

    I am trying to get the information of files ending in .eps from two different locations and write the file names to two seperate .txt files. And the masterLocation is a folder that only has other folders in that need searching and the subLocation only has files in the folder. Here is my code so far:

    namespace folderFileFinder_v1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    //Create variables for file paths
                    string masterLocation = (@"E:\aBULKProjects\_Chartbook\_ChBkArt\");
                    string subLocation = (@"B:\FundRep\FMP10\MiFID\ChBkArt_MiFID\");
                    //Create variables for type of files to look for
                    DriveInfo di = new DriveInfo(masterLocation);
                    DirectoryInfo dirInfo = di.RootDirectory;
                    FileInfo[] fileNames = dirInfo.GetFiles("*.eps");
                    Console.WriteLine("Files found at {0}\n", masterLocation);
                    //Loop through each of the files found and print their names in the console
                    foreach (FileInfo fi in fileNames)
                    {
                        Console.WriteLine("{0}", fi.Name);
                    }
                    //Copy names of the files into a new .txt file
                    File.WriteAllLines(masterLocation, fileNames.Select(fi => fi.Name));
                    File.WriteAllLines(subLocation, fileNames.Select(fi => fi.Name));
                    Console.WriteLine("\n");
                }
                //If fails
                catch (IOException ioex)
                {
                    Console.WriteLine(ioex);
                }
            }
        }
    }

    But it doesnt seem to look in the file path as stated in the variables I have created ie

    masterLocation and subLocation

    It is only looking in the roots ie B:\ and E:\

    Any ideas?


    Lee Warren


    Friday, February 22, 2013 3:25 PM

Answers

  • you are setting the directory back to the root directory. 'DirectoryInfo dirInfo = di.RootDirectory;' is the problem. You can initialize the directory using the constuctor path.

    DirectoryInfo dirInfo = new DirectoryInfo(masterLocation);

      FileInfo[] fileNames =  dirInfo.GetFiles("*.eps", SearchOption.AllDirectories); //To verify in underlying directories

      FileInfo[] fileNames =  dirInfo.GetFiles("*.eps"); //to verify in the current directory

     foreach (FileInfo fi in fileNames)
                    {
                        Console.WriteLine("{0}", fi.Name);
                    }

    Hope this works


    --Krishna

    Friday, February 22, 2013 7:51 PM

All replies

  • Hi Lee Warren,

    Do you want recursive search?

    DirectoryInfo dr = new DirectoryInfo(@"C:\");
    dr.GetFiles("*.eps", SearchOption.AllDirectories);

    Regards

    VV

    Friday, February 22, 2013 3:34 PM
  • Like so?

            static void Main(string[] args)
            {
                try
                {
                    //Create variables for file paths
                    string masterLocation = (@"E:\aBULKProjects\_Chartbook\_ChBkArt\");
                    string subLocation = (@"B:\FundRep\FMP10\MiFID\ChBkArt_MiFID\");
                    //Create variables for type of files to look for
                    DriveInfo di = new DriveInfo(masterLocation);
                    DirectoryInfo dr = new DirectoryInfo(masterLocation);
                    FileInfo[] fileNames = dr.GetFiles("*.eps", SearchOption.AllDirectories);
                    //DirectoryInfo dirInfo = di.RootDirectory;
                    //FileInfo[] fileNames = dirInfo.GetFiles("*.eps");
                    Console.WriteLine("Files found at {0}\n", masterLocation);
                    //Loop through each of the files found and print their names in the console
                    foreach (FileInfo fi in dr)
                    {
                        Console.WriteLine("{0}", fi.Name);
                    }
                    //Copy names of the files into a new .txt file
                    File.WriteAllLines(masterLocation, fileNames.Select(fi => fi.Name));
                    File.WriteAllLines(subLocation, fileNames.Select(fi => fi.Name));
                    Console.WriteLine("\n");
                }
                //If IO fails
                catch (IOException ioex)
                {
                    Console.WriteLine(ioex);
                }
                //If fails
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

    With this the Foreach errors out with:

    Error 1 foreach statement cannot operate on variables of type 'System.IO.DirectoryInfo' because 'System.IO.DirectoryInfo' does not contain a public definition for 'GetEnumerator' C:\Documents and Settings\A510945\my documents\visual studio 2010\Projects\folderFileFinder_v1\folderFileFinder_v1\Program.cs 31 17 folderFileFinder_v1


    Lee Warren

    Friday, February 22, 2013 3:44 PM
  • Change to foreach as follows

    foreach(FileInfo fi in fileNames)

    {

     //Your stuff....

    }

    Friday, February 22, 2013 6:08 PM
  • you are setting the directory back to the root directory. 'DirectoryInfo dirInfo = di.RootDirectory;' is the problem. You can initialize the directory using the constuctor path.

    DirectoryInfo dirInfo = new DirectoryInfo(masterLocation);

      FileInfo[] fileNames =  dirInfo.GetFiles("*.eps", SearchOption.AllDirectories); //To verify in underlying directories

      FileInfo[] fileNames =  dirInfo.GetFiles("*.eps"); //to verify in the current directory

     foreach (FileInfo fi in fileNames)
                    {
                        Console.WriteLine("{0}", fi.Name);
                    }

    Hope this works


    --Krishna

    Friday, February 22, 2013 7:51 PM
  • I have modified the code to remove the Console.WriteLine as it's not really needed now. But I got it working great.

    I have take out the reference to the //DirectoryInfo dirInfo = di.RootDirectory;

                try
                {
                    //Create variables for 2 file paths to look in
                    string masterSourceLocation = (@"E:\aBULKProjects\_Chartbook\_ChBkArt\");
                    string subSourceLocation = (@"B:\FundRep\FMP10\MiFID\ChBkArt_MiFID\");
                    //Create variables for 2 file paths to write to
                    string path1 = @"J:\Documents\pdfCheckingForAll.txt";
                    string path2 = @"J:\Documents\pdfCheckingForEps.txt";
                    FileInfo msl = new FileInfo(masterSourceLocation);
                    FileInfo ssl = new FileInfo(subSourceLocation);
                    DirectoryInfo di1 = msl.Directory;
                    DirectoryInfo di2 = ssl.Directory;
                    FileInfo[] fileNames = di1.GetFiles("*.eps", SearchOption.AllDirectories);
                    FileInfo[] fileNames1 = di2.GetFiles("*.eps", SearchOption.AllDirectories);
                    Console.WriteLine("Printing files found at\n\t{0}\nand\n\t{1}\n", masterSourceLocation, subSourceLocation);
                    File.WriteAllLines(path1, Directory.EnumerateFiles("J:\\", "*.eps"));
                    File.WriteAllLines(path2, Directory.EnumerateFiles("J:\\", "*.eps"));
                    Console.WriteLine("\n");
                    Console.ReadLine();
                }
                //If IO fails
                catch (IOException ioex)
                {
                    Console.WriteLine("This is the ioex Exception: {0}", ioex.Message);
                }
                //If fails
                catch (Exception ex)
                {
                    Console.WriteLine("This is the general Exception: {0}", ex.Message);
                }

    But it is still reading from the J:\ drive only and not the two drives set up as variables.

    Why?


    Lee Warren

    EDIT:

    I have made some changes to the code as below and it all works now. I've added this for reference for anyone else who may find it useful.

                try
                {
                    //Create variables for 2 file paths to look in
                    string masterSourceLocation = (@"E:\aBULKProjects\_Chartbook\_ChBkArt\ABreakdowns_ALL\");
                    string subSourceLocation = (@"B:\FundRep\FMP10\MiFID\ChBkArt_MiFID\");
                    //Create variables for 2 file paths to write to
                    string path1 = @"J:\Documents\pdfsFromE.txt";
                    string path2 = @"J:\Documents\pdfsFromB.txt";
                    FileInfo msl = new FileInfo(masterSourceLocation);
                    FileInfo ssl = new FileInfo(subSourceLocation);
                    DirectoryInfo di1 = msl.Directory;
                    DirectoryInfo di2 = ssl.Directory;
                    FileInfo[] fileNames = di1.GetFiles("*.eps", SearchOption.AllDirectories);
                    FileInfo[] fileNames1 = di2.GetFiles("*.eps");
                    //Write to the console information about the source of information for reference
                    Console.WriteLine("Printing files found at\n\t{0}\nand\n\t{1}\n", masterSourceLocation, subSourceLocation);
                    //Write the file names to txt files
                    File.WriteAllLines(path1, Directory.EnumerateFiles(masterSourceLocation, "*.eps"));
                    File.WriteAllLines(path2, Directory.EnumerateFiles(subSourceLocation, "*.eps"));
                    Console.WriteLine("\n");
                }
                //If IO fails
                catch (IOException ioex)
                {
                    Console.WriteLine("This is the ioex Exception: {0}", ioex.Message);
                }
                //If fails
                catch (Exception ex)
                {
                    Console.WriteLine("This is the general Exception: {0}", ex.Message);
                }
    Many thanks to all for your help.
    Monday, February 25, 2013 12:25 PM