Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting an entire directory

Status
Not open for further replies.

reneb

Programmer
Aug 8, 2000
43
US
I am trying to write a function that deletes all of the contents of a directory or folder. There are commands for deleting individual files, but I would not know the file names contained in the directory. Also, I can only delete empty directories, so if there is a directory inside the given directory, I won't be able to delete it if it has files in it. Any hints on getting the file names of a directory and determening if these are files or directories. Please help. Thanks in advance. [sig][/sig]
 
Dear reneb,

If you are using MFC look at the class CFileFind

If not using MFC look at the Win32 API's like:

FindClose
FindFirstFile
FindFirstFileEx
FindNextFile

Good luck
-pete [sig][/sig]
 
Hi,


Try using SHFileOperation()
This Api recursively deletes the directory and the files and directories beneath that directory

Regards
SUN [sig][/sig]
 

The following will delete the files and create a log file to store the time, etc. My newer version thows exceptions to check for errors but I don't have that here at the moment. I peaced this together because I usually check the date before I delete so I may have missed a semi-colen or two. Never tried it on NT but no problems with 95 or 98.

You can copy the code below, past it in a new .cpp file, select all (Ctrl+A), then Re-format (Alt+F8).

Be carefull.


#include <afxwin.h>
#include <fstream.h>
#include <iostream.h>
#include <string.h>
#include <time.h>


int main(int argc, char* argv[])
{
BOOL DirectoryNotFile;
CFileFind File; // find file object
ofstream logfile; // file object
CString csSourcePathAndFileName; // path to delete files
CString csLogPathAndFileName; // path to Log file
int NumFilesActedOn = 0; // num files deleted
int NumFilesFound = 0; // num files found


csSourcePathAndFileName == &quot;c:\\temp\\*.jpg&quot;;
csLogPathAndFileName == &quot;C:\\Temp\\MaintenanceLog.txt&quot;;

logfile.open(csLogPathAndFileName,ios::app); // open file for appending
time_t ltime; // get date and time
time( &ltime );
logfile << &quot;\n&quot; << ctime(&ltime); // write time to file


// find first file
BOOL bWorking = File.FindFile(csSourcePathAndFileName);

while (bWorking){ // loop while files are found
NumFilesFound = NumFilesFound+1; // increment file count
bWorking = File.FindNextFile(); // find next file
DirectoryNotFile = File.IsDirectory(); // check if file is actually a directory
if(DirectoryNotFile == FALSE){
if(DeleteFile(File.GetFilePath())){ // delete file
NumFilesActedOn = NumFilesActedOn + 1; // ++ count
}
else{
// log error
logfile << &quot;Error Deleting: &quot; << File.GetFilePath();
logfile << &quot; Error &quot; << GetLastError() << endl;
}
}
}
logfile << &quot;Found &quot; << NumFilesFound << &quot;\t&quot;; // log files found
logfile << &quot;Deleted &quot; << NumFilesActedOn << &quot;\tPath &quot; << (LPCTSTR)csSourcePathAndFileName << endl;
File.Close();
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top