locked
Wildcard file deletion RRS feed

  • Question

  • User186656733 posted

    Is there a way to delete multiple files using wildcards?

    I have been using My.Computer.FileSystem.DeleteFile to delete a single file.  Other than getting a list of existing files and deleting one at a time, is there a way to delete using wildcards, equivalent to:

    Delete myfile*.abc

    Help is much appreciated.

    Tuesday, November 22, 2011 8:11 AM

Answers

  • User533502567 posted

    Other than getting a list of existing files and deleting one at a time, is there a way to delete using wildcards, equivalent to:

    I don't think there is an option for wild card deletion in .Net framework. But you can easily do some thing like this..

                    DirectoryInfo Dir = new DirectoryInfo(@"YourDirectotyPath");
                    // FileInfo[] FileList = Dir.GetFiles("*.doc*", SearchOption.AllDirectories);
                    FileInfo[] FileList = Dir.GetFiles("*.FileExtension*", SearchOption.AllDirectories);
    
                    foreach (FileInfo fi in FileList)
                    {
                        //Response.Write(fi.FullName);
                        //delete file
                        File.Delete(fi.FullName);
    
                    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 22, 2011 9:48 AM

All replies

  • User533502567 posted

    Other than getting a list of existing files and deleting one at a time, is there a way to delete using wildcards, equivalent to:

    I don't think there is an option for wild card deletion in .Net framework. But you can easily do some thing like this..

                    DirectoryInfo Dir = new DirectoryInfo(@"YourDirectotyPath");
                    // FileInfo[] FileList = Dir.GetFiles("*.doc*", SearchOption.AllDirectories);
                    FileInfo[] FileList = Dir.GetFiles("*.FileExtension*", SearchOption.AllDirectories);
    
                    foreach (FileInfo fi in FileList)
                    {
                        //Response.Write(fi.FullName);
                        //delete file
                        File.Delete(fi.FullName);
    
                    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 22, 2011 9:48 AM
  • User186656733 posted

    Thank you for your quick reply.

    That certainly works.

     

    Tuesday, November 22, 2011 2:43 PM