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!