Delete Folder (C++)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lumpybanana247
    New Member
    • Apr 2007
    • 134

    Delete Folder (C++)

    anybody know how i could delete a FOLDER in c++?
    i know to delete a file is

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    /* remove example: remove myfile.txt */
    #include <stdio.h>
    
    int main ()
    {
      if( remove( "gay/gay.txt" ) != 0 )
      {
        perror( "Error deleting file" );
        cin.get();
    }
      else
      {
        puts( "File successfully deleted" );
        cin.get();
    }
      return 0;
    }
    but anybody know how delete a whole folder?

    thanks
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by lumpybanana247
    anybody know how i could delete a FOLDER in c++?
    i know to delete a file is

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    /* remove example: remove myfile.txt */
    #include <stdio.h>
    
    int main ()
    {
      if( remove( "gay/gay.txt" ) != 0 )
      {
        perror( "Error deleting file" );
        cin.get();
    }
      else
      {
        puts( "File successfully deleted" );
        cin.get();
    }
      return 0;
    }
    but anybody know how delete a whole folder?

    thanks
    Well, I guess you could delete it through your OS command line using the system function. What OS are you using?
    If you don't want to use that, then try searching on Google for it.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      there is a function called rmdir in stdlib.h which can help u to remove a directory.

      Thanks
      Raghuram

      Comment

      • lumpybanana247
        New Member
        • Apr 2007
        • 134

        #4
        thanks to both of you. this is what i used (and it worked :-) )

        Code:
        #include <errno.h>
        #include <dirent.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <iostream>
        #include <fstream>
        using namespace std;
        
        
        /* remove example: remove myfile.txt */
        #include <stdio.h>
        
        int main ()
        {
          if( rmdir("C:/Documents and Settings/Nathan/Desktop/gay") != 0 )
          {
            perror( "Error deleting file" );
            cin.get();
        }
          else
          {
            puts( "File successfully deleted" );
            cin.get();
        }
          return 0;
        }

        Comment

        • lumpybanana247
          New Member
          • Apr 2007
          • 134

          #5
          crap, that code ^^ only works if the directory is empty

          Comment

          • valverde
            New Member
            • Jan 2009
            • 1

            #6
            to delete a folder with all contents

            I use this to delete folders even when they are full of files (in linux):

            if (system("rm -r ./folder_name")== 0)
            cout << "folder succesfully deleted" <<endl;
            else
            cout<<"folder delete failed"<<endl;

            rmdir is for deleting empty folders, and rm -r is to delete non-empty folders.

            This "./folder_name" is of course when the folder is in the same directory of your application. When it is not, you can try using global route //import/home/... etct, or //tmp/... etc , depending on where your folder is. You can also use the relative path for files and folders, as you wish.

            regards,

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by valverde
              rmdir is for deleting empty folders, and rm -r is to delete non-empty folders.
              The -r flag stands for 'recursive', i.e. if the content of a directory (and its optional sub-directories) can be removed, the entire (now empty) directory is removed.

              kind regards,

              Jos

              Comment

              Working...