From 658cadceeaa540be2a517902ce0233d21546fb25 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 9 Aug 2024 18:29:38 -0400 Subject: [PATCH] add helper to purge directories recursively --- tools/lammps-gui/helpers.cpp | 18 ++++++++++++++++++ tools/lammps-gui/helpers.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/tools/lammps-gui/helpers.cpp b/tools/lammps-gui/helpers.cpp index bc158f3bb5..47d09f4515 100644 --- a/tools/lammps-gui/helpers.cpp +++ b/tools/lammps-gui/helpers.cpp @@ -13,6 +13,7 @@ #include "helpers.h" +#include #include #include #include @@ -66,6 +67,23 @@ bool has_exe(const QString &exe) return false; // Not found! } +// recursively remove all contents from a directory + +void purge_directory(const QString &dir) +{ + QDir directory(dir); + + directory.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); + const auto &entries = directory.entryList(); + for (auto &entry : entries) { + if (!directory.remove(entry)) { + directory.cd(entry); + directory.removeRecursively(); + directory.cdUp(); + } + } +} + // Local Variables: // c-basic-offset: 4 // End: diff --git a/tools/lammps-gui/helpers.h b/tools/lammps-gui/helpers.h index a88233b0f3..b22b6e72c4 100644 --- a/tools/lammps-gui/helpers.h +++ b/tools/lammps-gui/helpers.h @@ -25,6 +25,9 @@ extern char *mystrdup(const QString &text); // find if executable is in path extern bool has_exe(const QString &exe); +// recursively purge a directory +extern void purge_directory(const QString &dir); + #endif // Local Variables: // c-basic-offset: 4