Empty Directory Utility Under NT

Environment: Tested on Windows NT (SP5) with Visual C++ 6 (SP4)

Description

What is it ?

A "Del [Path\]*.*" /sxyz" NT utility.

Explanations:

I had to write a "Empty Directory" utility on WIN NT, without using Shell. So I wrote this function. I think it can be usefull for programmers who don't have time to learn Files under NT.
This is a recursive function which delete all the directories and files inside the directory sent as a parameter (sPath). The Parameter 'sPath' has to be a full path name without "\\" at the end.
So is it !

Attention !

With this call, you can empty every directory also important one. I am not responsible for deleting important data with my Code !

Return Value:

TRUE if the directory exists,
FALSE if it not.

Code

// Include this line before your code where you call 
// this function !
BOOL EmptyDirectory(CString &sPath);

// This is a recursive function which empty a directory path 
// of its files and call itself if necessary to delete
// subdirectories.
BOOL EmptyDirectory(CString &sPath)
{
 CFileFind finder;

 CString  sWildCard = sPath + "\\*.*";

 BOOL bFound;
 BOOL bWorking = finder.FindFile(sWildCard);

 bFound = bWorking;

 while (bWorking) 
 {
  bWorking = finder.FindNextFile();

  if (finder.IsDots()) continue;

  if (finder.IsDirectory()) 
  {
   CString s = finder.GetFilePath();
   EmptyDirectory(s);
   RemoveDirectory(finder.GetFilePath());
   continue; 
  }
  _unlink( finder.GetFilePath() );

 }

 return bFound;
}


Comments

  • Thanks

    Posted by Legacy on 08/06/2003 12:00am

    Originally posted by: vijay

    Thanks

    Reply
  • Just a small helpful addition...

    Posted by Legacy on 04/22/2003 12:00am

    Originally posted by: Eyes`Only

    FYI-

    If you add the line...

    sPath.TrimRight('\\');

    ... right before your line ...

    CString sWildCard = sPath + "\\*.*";

    ... you effectively remove the requirement "The Parameter 'sPath' has to be a full path name without "\\" at the end."

    Just in case anyone wants to errorproof their code the easy way :)

    Eyes`Only

    Reply
  • you just won the first place! congrats.

    Posted by Legacy on 04/13/2003 12:00am

    Originally posted by: Stan

    Could it be any more trivial? Your article just won the first place in our yearly "10 most trivial pieces of code" competition.

    Reply
  • Great

    Posted by Legacy on 03/12/2003 12:00am

    Originally posted by: Anuo

    Oh yeah! I just like this.

    Reply
  • Big thanks from a total beginner

    Posted by Legacy on 11/16/2002 12:00am

    Originally posted by: bodozer

    Thanks for the code.
    I found it very useable for my project.
    I'm totally new in VC++.
    I used BC++ (for DOS) 10-15 years ago.

    regards bodozer,

    Reply
  • read only directory

    Posted by Legacy on 09/09/2002 12:00am

    Originally posted by: brownbro

    What if the directory is read only ? I've had challenges in the past with RemoveDirectory() returning an error when the directory is read only.

    Reply
  • NON-MFC Version

    Posted by Legacy on 07/18/2002 12:00am

    Originally posted by: J.T. Gilkeson

    Here's the NON-MFC version:
    
    

    // This is a recursive function which empty a directory path
    // of its files and call itself if necessary to delete
    // subdirectories.
    BOOL EmptyDirectory(LPCSTR lpFolder)
    {
    WIN32_FIND_DATA ff;
    char absPath[MAX_PATH];
    char lpDeleteFolder[MAX_PATH];

    strcpy(lpDeleteFolder, lpFolder);
    strcat(lpDeleteFolder, "\\*.*");

    HANDLE hFile = FindFirstFile(lpDeleteFolder, &ff);
    BOOL bWorking = (INVALID_HANDLE_VALUE != hFile);
    BOOL bFound = bWorking;

    while (bWorking)
    {
    strcpy(absPath, lpFolder);
    strcat(absPath, "\\");
    strcat(absPath, ff.cFileName);

    if ((0 == strcmp(ff.cFileName, ".")) || (0 == strcmp(ff.cFileName, "..")))
    NULL;
    else if (ff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    // RECURSIVE CASE: Call with subdirectory
    EmptyDirectory(absPath);
    RemoveDirectory(absPath);
    }
    else
    DeleteFile(absPath);

    bWorking = FindNextFile(hFile, &ff);
    }

    //BASE CASE: No more subdirs
    FindClose(hFile);

    return bFound;
    }


    Reply
  • GREAT CODE!

    Posted by Legacy on 03/10/2002 12:00am

    Originally posted by: Dian Suharto

    Simple, nice and working code.

    Thanks!!!

    Dian Suharto

    Reply
  • great code

    Posted by Legacy on 02/19/2002 12:00am

    Originally posted by: anand

    great code.It simplified my work

    Reply
  • Great for beginners

    Posted by Legacy on 02/11/2002 12:00am

    Originally posted by: Chris Hunter

    I am just beginning Visual C++ programming. I found this article and it looks to be extremely helpful. I just wanted to say thanks.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds