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" );

Dani AI

Generated

Short answer: use a recursive delete (or a library routine that does the same) instead of the single-argument call shown in the thread. As pointed out, the directory must be emptied first — the recursive delete does that for you. already tried the non‑recursive overload, so adding the recursive option will remove files and subfolders automatically.

Example (C++/CLI):

System::IO::Directory::Delete(myPath, true);

If a simple recursive delete still fails, common causes and immediate remedies:

  • Read‑only attributes: clear them first (File::SetAttributes(..., FileAttributes::Normal)) before deletion.
  • Open handles: a file in use by another process causes an IOException; close the handle or stop the process.
  • Permissions: UnauthorizedAccessException means the process lacks rights (try elevated permissions).
  • Path length or invalid path: watch for PathTooLongException or DirectoryNotFoundException.

Modern native C++ option (if building with C++17+):

#include <filesystem>
std::error_code ec;
std::filesystem::remove_all("C:\\path\\to\\folder", ec);
if (ec) { /* inspect ec.message() and handle */ }

Practical workflow that ties back to earlier replies: either use the built‑in recursive delete above, or apply ’s traversal but delete bottom‑up (files first, then directories). When targeting only certain file types (e.g., .txt), enumerate with a recursive search option, clear attributes if needed, delete matching files, then remove empty directories. Always catch and log exceptions, test on a disposable folder, and avoid passing system/root paths — recursive delete is destructive and irreversible.

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.