Thread: Deleting folders and sub folders

  1. #1
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89

    Question Deleting folders and sub folders

    I am writing a win32app in C++ and I want to be able to delete a folder which will have many sub folders (with one txt file in each folder) at the clcik of a button. How would I go about doing so? I know how to delete a folder with nothing in it...but I do not know how to delete a folder with subfolders and files in it.

    any suggestions?

    thanx in advance,
    Boomba

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    system( "delete /y C:\\AFolder" );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    thanx for your suggestion XSquared but I forgot to mention that the folder names are user entered and I as a programmer cannot state the path because that is also user defined.

    So is it still posible to use somthing similair to the code above? and if so how would i use it?

    any suggestions?

    thanx in advance,
    boomba

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Another solution is to traverse the folder and remove all files and sbufolders. Here are some key win32 API.

    FindFirstFile()
    FindNextFile()
    DeleteFile()

    Kuphryn

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    use something like system(str) with str being "deltree /y c:\\" plus the user entered folder...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    well my "user entered folder" is just where ever the program is
    e.g... if my exe for my program is in C:\\program.....then my "user enterd folder" is in C:\\program\\folder (whatever the user wants to call it)

    so if the name of the "folder" is stored in a variable called course how would I implement that into the System() command? I also have the path that leads to "folder" in a variable called "rootdirectory" if that helps at all.

    or am I thinking the wrong way?

    thanx in advance,
    boomba

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    char sysCmd[ MAX_PATH ];
    
    strcpy( sysCmd, "deltree /y " );
    strcat( sysCmd, pathToProgram );
    strcat( sysCmd, userPath );
    
    system( sysCmd );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Alrighty, well, that code works perfectly, but is there a way to call it without having the Dos-Prompt window come up? Or is that built in to the System call?

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

    File Handling routines

    If you are building a win32 app you should be using the windows.h file and its file handling routines....

    Code:
    HANDLE FindFirstFile(
      LPCTSTR lpFileName,  // pointer to name of file to search for
      LPWIN32_FIND_DATA lpFindFileData 
                           // pointer to returned information
    );
    
    BOOL FindNextFile(
      HANDLE hFindFile,  // handle to search
      LPWIN32_FIND_DATA lpFindFileData 
                         // pointer to structure for data on found file
    );
     
    BOOL RemoveDirectory(
      LPCTSTR lpPathName   // pointer to directory to remove
    );
    
    BOOL DeleteFile(
      LPCTSTR lpFileName   // pointer to name of file to delete
    );
    you can build some recursive function that deletes every file and then destroys the directory...
    zMan

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I have a tutorial on the use of FindFirstFile()/FindNextFile() on my site here.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    deleting a folder

    try the following code....

    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;
    		}
    
    		int iFindDot = strFileName.find(".");
    
    		//Assume it is a directory because no '.' was found
    		if( iFindDot < 0 ) //not extension
    		{
    			//Recursive call
    			DeleteFolder( strFilePath.c_str() );
    		}
    
    		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

  12. #12
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    the system() method worked great for me too but is it supposed to work in windows xp ?...because I tried it on a computer with XP and it does nothing....the black consol windows quickly shows tho

    thanx in advance,
    Boomba,

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    system() is always a bad idea, there are reasons in the FAQ.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    1
    Gues what else is a bad idea?

    Running, zMan's code up there, I have just had my WHOLE C:\ deleted

    I just compiled and run it, nothing happens, now I notice I have 68GB free out of 75GB :MAD: f**k There goes all my work, including my f**king English essay

    Ah. Time to reformat and restart all over again.

  15. #15
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    omg... roflmao

    Its your fault for typing in "C:\" !!
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed