[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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?
0
sbornstein2
Asked:
sbornstein2
  • 7
  • 3
  • 2
1 Solution
 
WilliamCommented:

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

0
 
sbornstein2Author Commented:
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
 
0
 
sbornstein2Author Commented:
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)
            {
               
            }

        }
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

0
 
käµfm³d 👽Commented:
What about something like:
Dictionary<string, List< System.IO.FileInfo > > list = new Dictionary< string, List< System.IO.FileInfo > >();
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\some\directory");
System.IO.FileInfo[] files = di.GetFiles("*.pdf");
 
foreach (System.IO.FileInfo file in files)
{
    string group = file.Name.Substring(0, file.Name.IndexOf("_"));
 
    if (list.ContainsKey(group))
    {
        list[group].Add(file);
    }
    else
    {
        list.Add(group, new List<System.IO.FileInfo>() { file });
    }
}
 
foreach (string group in list.Keys)
{
    System.IO.FileInfo newest = list[group][0];
 
    for (int x = 1; x < list[group].Count; x++)
    {
        if (list[group][x].CreationTime > newest.CreationTime)
        {
            newest = list[group][x];
        }
    }
 
    foreach (System.IO.FileInfo fi in list[group])
    {
        if (fi.Name != newest.Name)
            fi.Delete();
    }
}                

Open in new window

0
 
sbornstein2Author Commented:
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.
0
 
käµfm³d 👽Commented:
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);
0
 
sbornstein2Author Commented:
this was perfect thanks so much.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now