Solved

Delete Files in folder

Posted on 2006-11-09
6
154 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now