I want to delete a Folder that consist of .txt Files. I am trying with this code below but the member :: Delete says that it only can emty an emty folder but this folder contains .txt files.
So I wonder what other technique could be used ?

System::IO::Directory::Delete( "Path of Emty Directory" );

Recommended Answers

All 4 Replies

You need to empty this folder I guess. That's how Windows works if you noticed.

Right now I am working on file listing class, it places all files in given subdirectory in vector of strings. If you are interested.

That sounds wonderful, I would be interested of that, yes. If you will make that work, it will be nice to see what you did..

You need to empty this folder I guess. That's how Windows works if you noticed.

Right now I am working on file listing class, it places all files in given subdirectory in vector of strings. If you are interested.

It lists files, you still need recursively delete directories beginning with deepest path. This calss will be more complex, handling configuration file and multitasking.

DirectoryReader.h:

class DirectoryReader
{
public:
DirectoryReader(void);
~DirectoryReader(void);
vector<string> m_vFilenames;
int MakeList(string p_sPath);
void DisplayVector(vector<string>);
void DisplayVector();
};

DirectoryReader.cpp:

int DirectoryReader::MakeList(string p_sPath)
{
struct _finddatai64_t struct_filedata;
string s_filename = p_sPath + "\\*.*";
string s_current = ".";
string s_parrent = "..";
long nHandle = _findfirsti64(s_filename.c_str(), &struct_filedata);

if(nHandle >= 0)
{
while(_findnexti64(nHandle, &struct_filedata) == 0)
{
if(struct_filedata.attrib & _A_SUBDIR)
{
if( (s_current.compare(struct_filedata.name) && s_parrent.compare(struct_filedata.name)) )
{
s_filename = p_sPath + "\\" + struct_filedata.name;
MakeList(s_filename);
}
}
else
{
//cout << "File found: " << p_sPath << " , " << struct_filedata.name << endl;
m_vFilenames.push_back(p_sPath + "\\" + struct_filedata.name);
}

}
}
else
{
cout << "Path not found." << endl;
return -1;
}
return(m_vFilenames.size());
}

void DirectoryReader::DisplayVector()
{
DisplayVector(m_vFilenames);
}

void DirectoryReader::DisplayVector(vector<string> p_vsVector)
{
vector<string>::iterator iter_vsIterator = p_vsVector.begin();
int n_listCount = 0;

while(iter_vsIterator != p_vsVector.end())
{
cout << *iter_vsIterator << endl;
n_listCount++;
iter_vsIterator++;
}
}

DirectoryReader::~DirectoryReader(void)
{
}

I based some ideas on Ancient Dragon's snippet. It stores full filenames in vector. You can replace .push_back with delete function, ot modify DisplayVector to delete files stored in vector. I can't think how to handle directory tree, it's too late.

I also hope it still works after I removed additional stuff not needed for your problem.

Thank you! While you did the solution I tried out another approach to save all the files that found in a directory like this that later can be used for File::Copy:
(I will check your code out)

array<System:: String ^> ^ files4 = System::IO:: Directory::GetFiles(GetOutMainPath, "*.txt");

std::vector<string> GiveAllFiles2;
std::string GiveAllFiles;
int CountAllFiles = 0;

if( files4->Length > 0 )
{
        for( int i = 0; i < files4->Length; ++i)
       {
         MarshalString(files4[i], GiveAllFiles);
         GiveAllFiles2.push_back(GiveAllFiles.c_str());  
         //This vector contains ALL .txt files that can be copied.
						 
        CountAllFiles = (CountAllFiles + 1);
        }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.