Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to loop through folder to delete files and subfolders in ASP.NET

Author: Rachel Nicole

You need to do recursion to delete files and subfolders from a specific folder. Code provided in this article will help you to do this task easily.

 

You cannot delete a directory if it is not empty. So you should have to delete files and subdirectories to delete the directory. A file cannot be deleted if it has ReadOnly permission.

To delete files and subfolders in a folder, you have to do it recursively. Recursion provides extremely powerful solution to a problem in a finite number of steps that have infinite number of computations. As in case of directories, subdirectories and files in directories. In Programming Language, a function calls itself within its code is recursion.
 
Sometimes you need to delete files that are older than a month or a year. You can do it easily in ASP.NET. A code snippet is provided below. Import "System.IO" namespace to use this code.
 
C#
 
using System.IO;
 
private void DeleteFilesAndFolders() {
        DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/NewFolder1"));
        DirectoryInfo[] subDirs = dirInfo.GetDirectories();
        foreach (DirectoryInfo dir in subDirs) {
            FindAndDeleteFolder(dir);
        }
        DeleteFilesInFolder(dirInfo);
       
    }
 
    private void FindAndDeleteFolder(DirectoryInfo drinfo) {
        DirectoryInfo[] subDirs = drinfo.GetDirectories();
        foreach (DirectoryInfo dir in subDirs) {
            FindAndDeleteFolder(dir);
        }
 
        DeleteFilesInFolder(drinfo);
 
        if (drinfo.GetFiles().Length == 0) {
            drinfo.Delete();
        }
    }
    private void DeleteFilesInFolder(DirectoryInfo di)
    {
        FileInfo[] fileInfo = di.GetFiles();
        foreach (FileInfo file in fileInfo) {
            if(file.CreationTime < DateTime.Now.AddMonths(-1)) {
                file.Delete();
            }
        }
    }
 
VB.NET
 
Imports System.IO
 
Private Sub DeleteFilesAndFolders()
        Dim dirInfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/NewFolder1"))
        Dim subDirs As DirectoryInfo() = dirInfo.GetDirectories()
        For Each dir As DirectoryInfo In subDirs
            FindAndDeleteFolder(dir)
        Next
        DeleteFilesInFolder(dirInfo)
    End Sub
 
    Private Sub FindAndDeleteFolder(ByVal drinfo As DirectoryInfo)
        Dim subDirs As DirectoryInfo() = drinfo.GetDirectories()
        For Each dir As DirectoryInfo In subDirs
            FindAndDeleteFolder(dir)
        Next
        DeleteFilesInFolder(drinfo)
        If drinfo.GetFiles().Length = 0 Then
            drinfo.Delete()
        End If
    End Sub
 
    Private Sub DeleteFilesInFolder(ByVal di As DirectoryInfo)
        Dim fileInfo As FileInfo() = di.GetFiles()
        For Each File As FileInfo In fileInfo
            If File.CreationTime < DateTime.Now.AddMonths(-1) Then
                File.Delete()
            End If
        Next
    End Sub
 
 
There are three functions in above code. In first function DeleteFilesAndFolder(), I have created a DirectoryInfo object and provided the path of the directory. Server.MapPath(“~/NewFolder1”) means that I have a folder named “NewFolder1” at the root of my application. Then I have called the GetDirectories() method of the DirectoryInfo object. This method returns the array of type DirectoryInfo. Then I iterated through each Directory and called the method FindAndDeleteFolder(). After that I have called the DeleteFilesInFolder() method.
 
FindAndDeleteFolder() method takes the DirectoryInfo object as parameter. I recursively called the FindAndDeleteFolder() in it then called the DeleteFilesInFolder() method. At the end of this method, I have deleted this directory if it is empty.
 
DeleteFilesInFolder() method takes the DirectoryInfo object as parameter. GetFiles() method of DirectoryInfo object returns the array of type FileInfo. At the end I iterate through all the files and call the Delete() method of FileInfo object if the file is older than 1 month. Providing value in minus in AddMonth() or AddYear() methods mean that it will subtract mentioned months or years. You can also use Subtract() method to subtract DateTime or TimeSpan Value. 
 
You can also delete files that has specific extension by mentioning the Search Option in GetFiles() method.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Data Caching in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net