Deleting folders and sub folders

This is a discussion on Deleting folders and sub folders within the C++ Programming forums, part of the General Programming Boards category; Here is an update to the code so that it will correctly identify a directory...Instead of using the . as ...

  1. #16
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    Correction

    Here is an update to the code so that it will correctly identify a directory...Instead of using the . as a determinant

    Code:
    #include<iostream>
    #include<string>
    #include <windows.h>
    
    using namespace std;
    
    
    
    void DeleteFolder( const char *szFolderPath)
    {
    	string strFileFilter;
    
    	strFileFilter = szFolderPath;
    	strFileFilter += "\\*.*";
    
    	WIN32_FIND_DATA win32FindData; //struct to hold file information
    
    	HANDLE hFile = FindFirstFile(strFileFilter.c_str(), &win32FindData);
    
    	while( FindNextFile(hFile, &win32FindData) )
    	{
    		string strFilePath; // full file path
    		string strFileName; // file name with extension only
    
    		strFilePath = szFolderPath;
    		strFilePath += "\\";
    		strFilePath += win32FindData.cFileName;
    		strFileName = win32FindData.cFileName;
    
    		//If is dots
    		if( strFileName == "." ||
    			strFileName == "..")
    		{
    			continue;
    		}
    
    		//Check to see if it is a directory
    		if( win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //not extension
    		{
    			//Recursive call
    			DeleteFolder( strFilePath.c_str() );
    		}
    		else
    		{
    			DeleteFile(strFilePath.c_str());
    		}
    	}
    
    	FindClose( hFile ); //release handle otherwise dir cannot be removed
    
    	RemoveDirectory( szFolderPath );
    }
    
    int main(void)
    {
    	string strDir;
    
    	while(1)
    	{
    		cout << "\nEnter dir path or \"quit\" to exit: " << endl;
    		cin >> strDir;
    
    		if( strDir == "quit" )
    			break;
    	
    		DeleteFolder( strDir.c_str() );
    	}
    
    	return 0;
    }
    zMan

  2. #17
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    thanx for the updated code zMan..i just wanna ask a few questions about it

    first of all what is strFileFilter.c_str()?...I dont understand the .c_str() part and its pupose and I dont understand the .cFileName either is that MFC?

    secondly do I have to make a "win32FindData" structure before hand?

    thirdly what is using namespace std?..what does it do?

    thanx, sorry for the idiodic questions but I gotta understand otherwise I'll only continue to be stupid

    thanx in advance
    Boomba,

  3. #18
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    I'll answer what I can...

    Originally posted by Boomba
    first of all what is strFileFilter.c_str()?...I dont understand the .c_str() part and its pupose
    strFileFilter is a std::string, which is a standard class that simplifies manipulation of character strings. But most of the Win32 API functions take char* (C-style strings). c_str() is a member function of class std::string that returns a char* so that it can be used in such cases.

    thirdly what is using namespace std?..what does it do?
    Do you know what namespaces are? If not, it's better to look them up. Basically, it allows you to create more than one object (integer, string, or whatever) with the same name, as long as they're in different namespaces. It's an important part of C++, because lots of times people pick the same names for functions and classes, and without namespaces to keep them separate, they would clash.

    Anyways, all the standard functions and objects (that come with most C++ compilers) are declared in the "std" namespace. To access them normally, you must put "std::" in front of the name (i.e. std::cout). "using namespace std" tells the compiler "let me use anything in the namespace std without prefixing the std:: first." It's basically a shorthand (but note that it also defeats the purpose of namespaces) used in examples for the sake of clarity.

    thanx, sorry for the idiodic questions but I gotta understand otherwise I'll only continue to be stupid
    Exactly, so they're not stupid questions. Better to ask and understand than copy and paste the code and hope it works

  4. #19
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    questions

    secondly do I have to make a "win32FindData" structure before hand?
    the WIN32_FIND_DATA is a type that holds file information. It holds file information such as file attributes, which include information if it is a folder or directory. It also holds the name and other things such as file size. You must create a struct of this type prior to calling the functions FindFirstFile... and FindNextFile because it gets loaded with that information. That is why you pass it by reference.
    zMan

  5. #20
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    is it mfc

    None of the code included in my above postings is part of MFC. MFC has a class that wraps this functionality in a class called CFileFind which is much simpler to use and you don't have to worry about using things such as the WIN32_FIND_DATA type or handles to the findfile functions.
    zMan

  6. #21
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Using zMan's code above, the function is looking for a:

    const char*

    To be passed into it. But, the path of the folder I wish to delete, I'm storing in:

    char Path[260];

    I get an error because it can not convert to zMan's type. Is there a quick way around this without having to rewrite all the code I used my Path variable in previously? (Making it a "string" variable is not a quick fix either. I get 15 errors). Thanks

  7. #22
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    That should not be causing you a problem, show us how you are trying to call the routine.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #23
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I used:

    strcat(Path, RootDirectory);
    strcat(Path, Course);
    DeleteFolder(Path);

    where rootdirectory and course are holding the user entered path

  9. #24
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    it works fine

    I tested it with just a char strDir[500] and it worked fine for me.
    zMan

  10. #25
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I can't see why the call should fail, but, are you sure you are correctly building the string? Should you not perhaps be using strcpy() then strcat(). You seem to be adding to the end of an uninitialised string, or at least, with what you have shown so far, that is as it seems.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #26
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by Epo
    Using zMan's code above, the function is looking for a:

    const char*

    To be passed into it. But, the path of the folder I wish to delete, I'm storing in:

    char Path[260];

    I get an error because it can not convert to zMan's type.
    I think it's unlikely that's the reason. const char* and char[] are nearly equivalent (for 95% of cases, they are). What exactly is the error?

  12. #27
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    OK, I tried the code and the program runs but when i go to delete in my program I get an error from windows xp and my program crashes.

    Heres how I prototyped, called and used the function:

    Code:
    void DeleteFolder(const char*);  //this is the prototype
    
    DeleteFolder(Path);  //this is the way I called it when button is clicked
    
    //this is how the function is being used.(I dont think I changed anything)
    
    void DeleteFolder(const char* szFolderPath)
    {
    	string strFileFilter;
    	
    	strFileFilter = szFolderPath;
    	strFileFilter += "\\*.*";
    
    	WIN32_FIND_DATA win32FindData; //struct to hold file information
    
    	HANDLE hFile = FindFirstFile(strFileFilter.c_str(), &win32FindData);
    
    	while( FindNextFile(hFile, &win32FindData) )
    	{
    		string strFilePath; // full file path
    		string strFileName; // file name with extension only
    
    		strFilePath = szFolderPath;
    		strFilePath += "\\";
    		strFilePath += win32FindData.cFileName;
    		strFileName = win32FindData.cFileName;
    
    		//If is dots
    		if( strFileName == "." || strFileName == "..")
    		{
    			continue;
    		}
    
    		//Check to see if it is a directory
    		if( win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //not extension
    		{
    			//Recursive call
    			DeleteFolder( strFilePath.c_str() );
    		}
    		else
    		{
    			DeleteFile(strFilePath.c_str());
    		}
    	}
    
    	FindClose( hFile ); //release handle otherwise dir cannot be removed
    	RemoveDirectory( szFolderPath );
    }
    So what is it that I'm doing wrong?..am i missing anything?..and why does the value being passed into the function have to be a const char*...cant it just be char?

    thanx in advance,
    Boomba,

  13. #28
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    Nevermind!..haha I figured it out!..it was my fault..I forgot to use strcpy() like epo and I used strcat() incorrectly..now it works like a charm on windows xp I'll try it on NT tommorow..

    thank you all ver much for your help

    I'm still wondering out of curiousity tho...why does the value being passed into the function have to be a const char*...why wont it work just passing char?

    thanx everyone!..great board!
    boomba,

  14. #29
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by Boomba
    I'm still wondering out of curiousity tho...why does the value being passed into the function have to be a const char*...why wont it work just passing char?
    A char is a single character. A char* is a pointer to a character, which usually represents an array of characters (which is a string (but not the std::string object, just another type of string) ). Hope that wasn't more confusing.

  15. #30
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    A char is a single character. A char* is a pointer to a character, which usually represents an array of characters (which is a string (but not the std::string object, just another type of string) ). Hope that wasn't more confusing.
    ok so when i want to pass a char array I'll always have to use a char* like I had to with this code?...makes sense.

    thanx,

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21