-3

I have a shared drive with about ~10,000 files located in about ~7,000 different folders that may be more than 7 folders deep from the parent. Most of these files (if not all) are .pdf files and I want to create an index file of each .pdf file with the same name. The index file would contain the folder name delimited by a pipe.

Example of structure:

C:\Files\1000\2000\01\99\AB\01\00\file1.pdf
C:\Files\1000\2000\01\99\CD\02\10\file2.pdf
C:\Files\1200\2010\20\99\WE\30\12\file3.pdf
C:\Files\1300\2000\31\99\BA\56\23\file4.pdf
C:\Files\1400\2014\59\99\RT\34\34\file5.pdf

Example of index file contents:

1000|2000|01|99|AB|01|00|<some static info>|<some static info>
1000|2000|01|99|CD|02|10|<some static info>|<some static info>
1200|2010|20|99|WE|30|12|<some static info>|<some static info>
1300|2000|31|99|BA|56|23|<some static info>|<some static info>
1400|2014|59|99|RT|34|34|<some static info>|<some static info>

Final Output:

C:\Files\1000\2000\01\99\AB\01\00\file1.pdf
C:\Files\1000\2000\01\99\AB\01\00\file1.txt
C:\Files\1000\2000\01\99\CD\02\10\file2.pdf
C:\Files\1000\2000\01\99\CD\02\10\file2.txt
C:\Files\1200\2010\20\99\WE\30\12\file3.pdf
C:\Files\1200\2010\20\99\WE\30\12\file3.txt
C:\Files\1300\2000\31\99\BA\56\23\file4.pdf
C:\Files\1300\2000\31\99\BA\56\23\file4.txt
C:\Files\1400\2014\59\99\RT\34\34\file5.pdf
C:\Files\1400\2014\59\99\RT\34\34\file5.txt

The index file would be saved as the same file name of the .pdf and saved in the same directory as the file. How should I approach this? Thanks for your suggestions!

Edit: Thanks, Sam and Furkle. When I started to write it, I was finally able to get the path of the files similar to what you did.

string[] filenames = Directory.GetFiles(loca, "*.*", SearchOption.AllDirectories);
string fname = DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; //wrote it to a file just to see if I could get the path

File.CreateText(Path.Combine(Log, fname)).Dispose();

foreach(string filename in filenames)
{
using (FileStream f = new FileStream(Path.Combine(Log, fname), FileMode.Append, FileAccess.Write, FileShare.Write))
Using (StreamWriter l = new StreamWriter(f))
{
l.WriteLine(filename);
}
}
MessageBox.Show("Done");

The variable loca is just a test path that I am using for now. The output gave me the absolute path to where the files are located in my test so far. I will use your tips to see how I can further develop it. Thanks for your guidance again!

Jayarikahs
  • 75
  • 11
  • 4
    `How should I approach this?` You can start by writing some code and ask here if you get stuck somewhere... – L.B Nov 14 '14 at 21:02
  • You are so critical with new comers just asking for some assistance. I am still learning how to do things and just need guidance with an approach. I am not looking for someone to write the entire thing for me, but just a push in a direction on how I should just start things off... – Jayarikahs Nov 14 '14 at 21:32
  • Jayarikahs, `How should I approach this?` is too broad. we don't know what you know & what you don't. What you have researched & tried. So in this form, your question simply implies *"write it for me"*. BTW: SO is about coding problems, if you want more info about some topic, you can use google. – L.B Nov 14 '14 at 21:38
  • Please don't forget to select one of the answers (currently either Sam's or mine), so that future users can know which helped you most. – furkle Nov 14 '14 at 21:42
  • I cannot assume how you want to interpret my context, as English is not my native language. If that by means assumes that what I wrote means what you assumed along with the general population, then I need to work on my English too because I did not mean for my context to be understood in that manner. – Jayarikahs Nov 14 '14 at 21:46
  • Thanks, Furkle. I will try to use both the suggestions and select the one. – Jayarikahs Nov 14 '14 at 21:47

2 Answers2

0

As far as creating the .txt files, here's something you could do:

// define your DirectoryInfo dir here

// iterates through all files at all levels in your directory
foreach (FileInfo file in dir.EnumerateFiles("*.pdf", SearchOption.AllDirectories))
{
    // gets the file name without the extension
    string filenameWithoutExtension = file.Name.Substring(
                                       0, file.Name.Length - file.Extension.Length);

    // creates a FileInfo object for the text file you aim to create
    FileInfo textFile = new FileInfo(
                         file.Directory.ToString() + @"\" + file.Name + ".txt");

    // checks that the file does not already exist
    if (!textFile.Exists)
    {
        // creates an empty file
        textFile.Create();
    }
}

This will create a text file with the same name as your file (e.g. for file1.pdf it will create a file called file1.txt) in the same directory. Note that IO operations are quite unpredictable, so you'll want to add exception handling fitting your needs/environment. Note also that this only works if the extension is exactly .pdf - if you need to worry about PDF, you'd want to use .EnumerateFiles().Where(a => a.Extension.ToLower() == ".pdf").

As far as actually writing to those files, you could either do that within your loop, immediately after textFile.Create(), or you could store each FileInfo in an IEnumerable and write to them later. I suspect the former would work better for you, but you haven't really given many implementation details.

furkle
  • 5,019
  • 1
  • 15
  • 24
  • You can create a recursive method that iterates through every directory and that directory's subsequent sub directory. While doing this, for every file with the pdf extension, create an index file with the same name with the extension txt. Alternatively, you can use the above method or something similar to: http://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c – James Shaw Nov 14 '14 at 21:36
  • sorry @furkle... I mistakenly hit the enter key when I was typing that. I wasn't finished. – James Shaw Nov 14 '14 at 21:39
0
  • Get file names. The Directory.GetFileSystemEntries method might help

  • iterate over file names with a foreach loop, and add the file name to your output at each iteration.