|
-
May 30th, 2009, 05:38 PM
#1
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?
-
May 30th, 2009, 07:01 PM
#2
Re: Help Deleting Directories
If you use boost:
Code:
#include <boost/filesystem.hpp>
boost::remove_all("C:\\test");
Here for more info.
-
May 30th, 2009, 08:38 PM
#3
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 ?
-
June 1st, 2009, 09:21 PM
#4
Re: Help Deleting Directories
 Originally Posted by NonstopChachacha
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 ?
-
June 1st, 2009, 09:26 PM
#5
Re: Help Deleting Directories
 Originally Posted by NonstopChachacha
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.
-
June 1st, 2009, 09:28 PM
#6
Re: Help Deleting Directories
Could you tell me why please ?
-
June 1st, 2009, 09:48 PM
#7
Re: Help Deleting Directories
 Originally Posted by NonstopChachacha
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|