How to Delete a Folder in C++

How to delete a directory and its contents in (POSIX) C?

  1. You need to use nftw() (or possibly ftw()) to traverse the hierarchy.
  2. You need to use unlink() to remove files and other non-directories.
  3. You need to use rmdir() to remove (empty) directories.

You would be better off using nftw() (rather than ftw()) since it gives you controls such as FTW_DEPTH to ensure that all files under a directory are visited before the directory itself is visited.

C# delete a folder and all files and folders within that folder

dir.Delete(true); // true => recursive delete

How to delete all files and folders in a directory?

System.IO.DirectoryInfo di = new DirectoryInfo("YourPath");

foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}

If your directory may have many files, EnumerateFiles() is more efficient than GetFiles(), because when you use EnumerateFiles() you can start enumerating it before the whole collection is returned, as opposed to GetFiles() where you need to load the entire collection in memory before begin to enumerate it. See this quote here:

Therefore, when you are working with many files and directories, EnumerateFiles() can be more efficient.

The same applies to EnumerateDirectories() and GetDirectories(). So the code would be:

foreach (FileInfo file in di.EnumerateFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.EnumerateDirectories())
{
dir.Delete(true);
}

For the purpose of this question, there is really no reason to use GetFiles() and GetDirectories().

Removing a non empty directory programmatically in C or C++

You want to write a function (a recursive function is easiest, but can easily run out of stack space on deep directories) that will enumerate the children of a directory. If you find a child that is a directory, you recurse on that. Otherwise, you delete the files inside. When you are done, the directory is empty and you can remove it via the syscall.

To enumerate directories on Unix, you can use opendir(), readdir(), and closedir(). To remove you use rmdir() on an empty directory (i.e. at the end of your function, after deleting the children) and unlink() on a file. Note that on many systems the d_type member in struct dirent is not supported; on these platforms, you will have to use stat() and S_ISDIR(stat.st_mode) to determine if a given path is a directory.

On Windows, you will use FindFirstFile()/FindNextFile() to enumerate, RemoveDirectory() on empty directories, and DeleteFile() to remove files.

Here's an example that might work on Unix (completely untested):

int remove_directory(const char *path) {
DIR *d = opendir(path);
size_t path_len = strlen(path);
int r = -1;

if (d) {
struct dirent *p;

r = 0;
while (!r && (p=readdir(d))) {
int r2 = -1;
char *buf;
size_t len;

/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;

len = path_len + strlen(p->d_name) + 2;
buf = malloc(len);

if (buf) {
struct stat statbuf;

snprintf(buf, len, "%s/%s", path, p->d_name);
if (!stat(buf, &statbuf)) {
if (S_ISDIR(statbuf.st_mode))
r2 = remove_directory(buf);
else
r2 = unlink(buf);
}
free(buf);
}
r = r2;
}
closedir(d);
}

if (!r)
r = rmdir(path);

return r;
}

How to delete folders created older than last 7 days with C#?

This answer assumes you are measuring the date based on the name of the folder and it's parents, not the metadata of the folder(s).

Gather a list of all the possible folders to delete, and get the full paths.

string directoryPath = @"C:\folder\2022\09\19";

If you're lazy and only intend on this being ran on Windows, split by backslash.

string[] pathSegments = directoryPath.Split('\\');

The last element represents the day. The second to last represents the month, and the third to last represents the year. You can then construct your own DateTime object with that information.

DateTime pathDate = new DateTime(
year: int.Parse(pathSegments[^3]),
month: int.Parse(pathSegments[^2]),
day: int.Parse(pathSegments[^1])
);

You can now easily compare this DateTime against DateTime.Now or any other instance.



Related Topics



Leave a reply



Submit