Solved

Delete Files in folder

Posted on 2006-11-09
6
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to remove duplicate code from my project 5 66
Adding where clause to entity statement c# 1 64
Need help with a query 14 54
How does this modal gets closed? 6 42
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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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