Thread: Deleting an entire folder and its contents including other folders

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Question Deleting an entire folder and its contents including other folders

    Does anybody know how to delete an entire folder and its contents including other folders? I'm using MSVC++ 6.0 and the program is in win32.

    Thanks
    -Chris

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    i think there's a rmdir command in dir.h
    but i'm not sure wether it can delete the dir including its contents.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    use the FindFirstFile/FindNextFile combination to delete every file int the directory, one by one, then delete the directory

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    use the FindFirstFile/FindNextFile combination to delete every file int the directory, one by one, then delete the directory
    I know how to use the FindFirstFile/FindNextFile combination but how do I "...then delete the directory"?

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    There's a Win32 function - RemoveDirectory().

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    Here is the sample out of the MSDN files.... this is in the help files....

    Code:
    /* MAKEDIR.C */
    
    #include <direct.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    void main( void )
    {
       if( _mkdir( "\\testtmp" ) == 0 )
       {
          printf( "Directory '\\testtmp' was successfully created\n" );
          system( "dir \\testtmp" );
          if( _rmdir( "\\testtmp" ) == 0 )
            printf( "Directory '\\testtmp' was successfully removed\n"  );
          else
             printf( "Problem removing directory '\\testtmp'\n" );
       }
       else
          printf( "Problem creating directory '\\testtmp'\n" );
    }
    
    
    Output
    
    Directory '\testtmp' was successfully created   
     Volume in drive C is CDRIVE
     Volume Serial Number is 0E17-1702
    
     Directory of C:\testtmp
    
    05/03/94  12:30p        <DIR>           .
    05/03/94  12:30p        <DIR>           ..
                   2 File(s)          0 bytes
                                 17,358,848 bytes free
    Directory '\testtmp' was successfully removed
    zMan

  7. #7
    Unregistered
    Guest
    For the person who suggested RemoveDirectory():

    RemoveDirectory() only removes a directory if it is completely empty. Is there any similar function that can erase the data and other folders within a folder?

    For last person who posted here:
    Your example is a console app and uses system commands etc, and what i am doing is in windows. Is there a way to do this with windows?

    Thanks
    -Chris

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I forgot to login when I posted the above message...

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    RemoveDirectory() only removes a directory if it is [snippity snip]
    Hence the FindFirstFile/FindFirstFile to remove the contents.

  10. #10
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Oh thanks. But what if there are other folders within this directory? Is there any way to get rid of these?

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    use recursion

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    wouldn't the simple remove() function do the job if you simply deleted the directory, or would it leave the other files there?

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Don't know if it exist in the newer versions of windows. But DELTREE was a dos command to delete a directory and everything inside (including other directories).

Popular pages Recent additions subscribe to a feed