CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2009
    Posts
    7

    Help Deleting Directories

    Hey everyone I have been doing a bit of C++ programming lately and found my self stuck when I tried to port a program written in Java to C++ that uses a recursive function to delete the files in a directory.

    Code:
    #include <unistd.h>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    char* initialPath = "C:\\test\\*";
    WIN32_FIND_DATA fileData;
    HANDLE handle;
    
    int deleteDirectories (char* initialPath, WIN32_FIND_DATA fileData, HANDLE handle) 
    {    
         string newFileName;
         string fileName;
         
         handle = FindFirstFile(initialPath, &fileData);
         newFileName = initialPath;
         newFileName = newFileName.substr(0, newFileName.size() - 1) + fileData.cFileName;
         remove(newFileName.c_str());
         
         while (true)
         {
               if (fileName == fileData.cFileName)
               {
                  break;             
               }
               fileName = fileData.cFileName;
               
               FindNextFile(handle, &fileData);
               
               if (fileData.dwFileAttributes == 16)
               {           
                  newFileName = initialPath;
                  newFileName = newFileName.substr(0, newFileName.size() - 1) + fileData.cFileName + "\\*";
                  initialPath = const_cast<char*>(newFileName.c_str());   
                  cout << initialPath << endl;
                  
                  deleteDirectories (initialPath, fileData, handle);  
                  
               }
               
               newFileName = initialPath;
               newFileName = newFileName.substr(0, newFileName.size() - 1) + fileData.cFileName;
               remove(newFileName.c_str());         
         }
         
    FindClose(handle);     
    return 0;
    }
    
    int main()
    {
        deleteDirectories (initialPath, fileData, handle);    
        return 0;
    }
    Now for some reason it deletes the files on the first layer correctly but when I implemented the if statement to check to see if the file was a directory or not it throws my program into an endless loop. Can anyone provide me with some incite into fixing this problem?

  2. #2
    Join Date
    May 2007
    Posts
    811

    Re: Help Deleting Directories

    If you use boost:
    Code:
    #include <boost/filesystem.hpp>
    
    boost::remove_all("C:\\test");
    Here for more info.

  3. #3
    Join Date
    May 2009
    Posts
    7

    Re: Help Deleting Directories

    Hello, how can I delete a directory or a file if I still get pop-up that it is being used by some program ?

  4. #4
    Join Date
    May 2009
    Posts
    7

    Re: Help Deleting Directories

    Quote Originally Posted by NonstopChachacha View Post
    Hello, how can I delete a directory or a file if I still get pop-up that it is being used by some program ?
    So how ?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help Deleting Directories

    Quote Originally Posted by NonstopChachacha View Post
    Hello, how can I delete a directory or a file if I still get pop-up that it is being used by some program ?
    You can't.

  6. #6
    Join Date
    May 2009
    Posts
    7

    Re: Help Deleting Directories

    Could you tell me why please ?

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help Deleting Directories

    Quote Originally Posted by NonstopChachacha View Post
    Could you tell me why please ?
    Because the operating system won't let you. What would you expect the program to do if the directory it was working in was deleted out from under it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured