Solved

Delete Files in folder

Posted on 2006-11-09
6
158 Views
Last Modified: 2010-04-16
Hi,
 I need a function that get as parameter a Folder path name and deletes all the files in this folder.

it's 0 if it deleted every thins else it's returns 1


thanks
0
Comment
Question by:madmare
6 Comments
 
LVL 8

Expert Comment

by:sabeesh
ID: 17905610
System.IO.Directory.Delete(stringpath,bool )
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17905872
That will delete the directory as well, not what was wanted. Try this:

using System.IO;

public int DeleteFromDirectory(string path)
{
    bool deleted = false;
    while (Directory.GetFiles(path).Count > 0)
    {
        File.Delete(Directory.GetFiles(path)[0]);
        deleted = true;
    }
    return deleted ? 1 : 0;
}
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17905874
Swap the 1 and 0 - sorry!
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17905969
Altenative, using exception handling:

using System.IO;
...
public int DeleteFiles(string dirPath)
{
      bool allDeleted = true;
      foreach (string filePath in Directory.GetFiles(dirPath))
      {
            try
            {
                  File.Delete(filePath);
            }
            catch
            {
                  allDeleted = false;
            }
      }
      return allDeleted? 0 : 1;
}
0
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 17906103
This is VB code I converted on the fly, so it may have some syntax errors, but it is close to what you want. Read the comments for more features.

private bool RemoveOldFiles(string DirPath As String)
{
        string[] FileList = Directory.GetFiles(DirPath)
        int FileCount = Directory.GetFiles(DirPath).Length
        int NumDeleted
       
        foreach(string FilePath in FileList)
        {
            string[] TempArr = FilePath.Split("\");
            string FileName = TempArr[TempArr.Length - 1];
           
           DialogResult Selection = MessageBox.Show("Are you sure you want to delete: " & FilePath & "?", "Confirm Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if( Selection == DialogResult.Yes)
                {
                    'If the file is read-only, set to archive to allow deletion
                    if (File.GetAttributes(FilePath) && FileAttributes.ReadOnly)
                        File.SetAttributes(FilePath, FileAttributes.Archive);

                    File.Delete(FilePath);
                    NumDeleted ++;
                }
           }

        }

       if(FileCount == NumDeleted)
       {
            return true; //change to 1 and 0 if you want, but change the function return type
       }
       else
       {
             return false;
        }
//FROM HERE ON IT LOOPS THROUGH SUBDIRS AND DELETES THEIR FILES
//You'll have to modify this some if you want to use it
/*        string[] SubDirList = Directory.GetDirectories(DirPath);

        foreach(string SubDirPath in SubDirList)
        {
                string[] Temp = SubDirPath.Split("\");
               
                RemoveOldFiles(SubDirPath);
         }*/
         
}


    End Sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question