sbornstein2
asked on
C# - File IO question
I need to loop through all folders under a directory I have in the code and for each folder delete all files that have the extension "PDF" that are not the latest file date file. So I need to somehow delete all but one based on the file date keeping the latest file. Any idea how I do that?
ASKER
actually sorry there is one more thing I just realized. I also need to look at the start of the file name and group it by invoice date. Sorry so for example let's say I have four files in the directory for march and april end but I need the latest of each invoice date file which is the start of the filename before the first underscore:
3312009_01400071_179505084 .pdf Date 4/7/09 11:00AM
3312009_01400071_179505085 .pdf Date 4/7/09 11:42AM
4302009_01400071_179505084 .pdf Date 4/7/09 11:15AM
4302009_01400071_179505085 .pdf Date 4/7/09 11:20AM
I would want to delete the 1st file for each set of these and only be left with:
3312009_01400071_179505085 .pdf Date 4/7/09 11:42AM
4302009_01400071_179505085 .pdf Date 4/7/09 11:20AM
3312009_01400071_179505084
3312009_01400071_179505085
4302009_01400071_179505084
4302009_01400071_179505085
I would want to delete the 1st file for each set of these and only be left with:
3312009_01400071_179505085
4302009_01400071_179505085
ASKER
something on the lines of I can loop through the folders such as:
public void CleanupMacOldPDFs()
{
string PdfFolder = "\\\\fileserver";
string[] PDFDir = Directory.GetDirectories(@ PdfFolder) ;
foreach (string FolderName in PDFDir)
{
}
}
public void CleanupMacOldPDFs()
{
string PdfFolder = "\\\\fileserver";
string[] PDFDir = Directory.GetDirectories(@
foreach (string FolderName in PDFDir)
{
}
}
You are only interested in the files right? Does it matter what dir they are in?
ASKER
also i dont want to pass it a datetime I just want to look at the dates and only delete everything but the new one.
ASKER
i need to cleanup each directory where we only have one invoice date PDF file and only the latest file dated file.
ASKER
but the directories are all under one root directory so I would provide the root directory and then loop through each folder and cleanup the old files I don't need.
This oughta to get what you are after...
List<string> PDFDirs = new List<string>( Directory.GetDirectories( "Your Root Directory" , "*" , SearchOption.AllDirectories ) );
foreach ( string DIR in PDFDirs )
{
List<string> AllPdfs = new List<string>( Directory.GetFiles( DIR , "*.PDF" , SearchOption.AllDirectories ) );
foreach ( string PDF in AllPdfs )
{
FileInfo FI = new FileInfo( PDF );
if ( FI.LastWriteTime > "YOUR DATE TIME HERE!" )
File.Delete( PDF );
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
going to test it here. Doesn't like this line though,
list.Add(group, new List<System.IO.FileInfo>() { file });
trying to figure that out the { file } missing paren or something.
list.Add(group, new List<System.IO.FileInfo>()
trying to figure that out the { file } missing paren or something.
Just as well, you can change the line from
list.Add(group, new List<System.IO.FileInfo>() { file });
to
list.Add(group, new List<System.IO.FileInfo>() );
list[group].Add(file);
list.Add(group, new List<System.IO.FileInfo>()
to
list.Add(group, new List<System.IO.FileInfo>()
list[group].Add(file);
ASKER
this was perfect thanks so much.
Open in new window