Reputation: 31
I have to list all PDF Files in a Directory and create a csv File with the Names of all the PDFs Files in it.
Also there should stand other entries like Creation Date and a Barcode in the CSV File.
The entries should be separated by "#_#".
The CSV File should look like this:
Description Date Barcode PDFName
SAP_Eingang#_#2014.01.14 12:34:44#_#01001019000516572013#_#01001019000516572013_2014_01_14_12_34_44_662.pdf
SAP_Eingang#_#2014.01.14 12:35:44#_#01001019000516572014#_#01001019000516572014_2014_01_14_12_35_44_662.pdf
SAP_Eingang#_#2014.01.14 12:36:44#_#01001019000516572015#_#01001019000516572015_2014_01_14_12_36_44_662.pdf
I have already this Code to list all PDFs in a CSV File:
string[] fileArray = Directory.GetFiles(@"c:\test\", "*.pdf").Select(path =>Path.GetFileName(path)).ToArray();
System.IO.File.WriteAllLines(@"c:\test2\test.csv",fileArray);
But I don't know how to write the other entries in the CSV File.
Upvotes: 0
Views: 108
Reputation: 31
My code is now:
using (StreamWriter rdr = new StreamWriter(@"C:\test2\test.csv"))
{
const string description = "SAP_Eingang";
const string separator = "#_#";
DirectoryInfo di = new DirectoryInfo(@"c:\test\");
FileInfo[] files = di.GetFiles("*.pdf");
foreach (FileInfo fi in files)
{
string[] columns = new string[4];
columns[0] = description;
columns[1] = fi.CreationTime.ToString("yyyy.MM.dd hh:mm:ss");
columns[2] = fi.Name.Substring(0, 20);
columns[3] = fi.Name;
string line = String.Join(separator,columns);
rdr.WriteLine(line);
}
}
Upvotes: 0
Reputation: 3874
Use DirectoryInfo instead:
const string description = "SAP_Eingang";
const string separator = "#_#";
//open csv file in a using statement
DirectoryInfo di = new DirectoryInfo(@"c:\test\");
FileInfo[] files = di.GetFiles("*.pdf");
foreach(FileInfo fi in files) {
string[] columns = new string[4];
columns[0] = description;
columns[1] = fi.CreationTime.ToString("yyyy.MM.dd hh:mm:ss");
columns[2] = fi.Name.Substring(0, 20);
columns[3] = fi.Name;
string line = String.Join(separator, columns);
//write line into the csv file
}
//close the using statement
I haven't compiled the above code, but you get the idea
Upvotes: 1