Link to home
Start Free TrialLog in
Avatar of sbornstein2
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?
Avatar of William Domenz
William Domenz
Flag of United States of America image


List<string> AllPdfs = new List<string>( Directory.GetFiles( "Your Directory" , "*.PDF" , SearchOption.AllDirectories ) );
            foreach ( string PDF in AllPdfs )
            {
                FileInfo FI = new FileInfo( PDF );
                if ( FI.LastWriteTime > "YOUR DATE TIME HERE!" )
                    File.Delete( PDF );
            }

Open in new window

Avatar of sbornstein2
sbornstein2

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
 
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)
            {
               
            }

        }
You are only interested in the files right? Does it matter what dir they are in?
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.
i need to cleanup each directory where we only have one invoice date PDF file and only the latest file dated file.
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 );
                }
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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);
this was perfect thanks so much.