0

My goal is simple: I'd like a to clear a directory (delete all of the sub-directories and files it contains, but not delete the directory itself) given a path, in a cross-platform way.

Most of the solutions I've found online either involve using dirent.h which, from my understanding, is non-standard and may not work on non-POSIX systems (notably Windows) or using Boost.Filesystem. However, building Boost and including it in my project is a lot to ask for if all I'd like to do is clear a directory.

Is there a way to achieve my goal in standard C++? Or, has the standard not advanced to this point yet?

Parker Hoyes
  • 2,118
  • 1
  • 23
  • 38
  • _"building Boost and including it in my project is a lot to ask"_ - No it's not. – Captain Obvlious Mar 14 '16 at 20:02
  • As of now, you do not have it in standard C++. However, since there are practically two options - either Posix with readdir, either Windows with FindFirstFile/FindNextFile, I'd say, your situation is not that dire. Just use #ifdef to choose one or another. – SergeyA Mar 14 '16 at 20:07
  • @SergeyA: That only covers POSIX and Windows. There are plenty of non-POSIX, non-Windows computers in the world — not every computer is a mainstream, desktop commodity PC! – Lightness Races in Orbit Mar 14 '16 at 20:21
  • @BarryTheHatchet, practically or theoretically? – SergeyA Mar 14 '16 at 20:21
  • @SergeyA: Both​. The theory models reality (otherwise it wouldn't be much use) – Lightness Races in Orbit Mar 14 '16 at 20:22
  • @BarryTheHatchet, I worked with different hardware. Have seen a lot of embedded stuff as well. Yet I am to see something which is non-Posix (embedded is Linux nowadays mostly) and non-windows, and have modern C++ compiler at the same time. Care to name one? – SergeyA Mar 14 '16 at 20:23
  • @SergeyA: The ones I work with every day don't have public-facing names. – Lightness Races in Orbit Mar 14 '16 at 20:24
  • @BarryTheHatchet, onthology teaches us that something which doesn't have a name doesn't exist. My point is proven. – SergeyA Mar 14 '16 at 20:24
  • @SergeyA: The point is: a no-name system does jobs which rather not need a filesystem library. Needless discussion. – Youka Mar 14 '16 at 20:26
  • @Youka, I guess, the question of cleaning a directory on such a system is really not a very acute one? I still maintain, that for **all** practical intents and purposes a Posix-windows switch is enough. And as I said in other comments, Boost concurs. – SergeyA Mar 14 '16 at 20:31
  • I was really looking for a solution that's as "clean" and simple as using Boost.Filesystem, without having to actually include the entire library in my build process. Interfacing directly with the Windows API opens up a lot of potential for corner-case bugs, as described [in this answer](http://stackoverflow.com/a/29556279/1772838). However, it looks like my search was to no avail - I'm currently compiling Boost. Perhaps I'll come back to this thread when the C++ standard advances. – Parker Hoyes Mar 14 '16 at 20:36
  • @SergeyA: _"onthology teaches us that something which doesn't have a name doesn't exist"_ lol what – Lightness Races in Orbit Mar 14 '16 at 20:38
  • @BarryTheHatchet, that's kinda basic and well-know glitch of human cognitive process. Since everything exists only as long as it can be reflected in our minds, and in order to have something in your mind you have to have a name for it, something which doesn't have a name doesn't really exist. Every totalitarian regime knows that and bans a certain forms of discourse altogether. Direct analogy with C++ is here - if you do not have a pointer (a tag, a name) to your allocated memory, the memory all but doesn't exist for your program. – SergeyA Mar 14 '16 at 20:42
  • @SergeyA: What a load of nonsense. By that logic, no answer on Stack Overflow exists. Or, to go along with your C++ analogy, what about literals? Every single value in the expression `1 + 2 == 3` most certainly does "exist"! – Lightness Races in Orbit Mar 14 '16 at 20:43
  • @ParkerHoyes, the answer you are referring to is bullocks. There are plenty of apps written with WinAPI without boost, and they are doing just fine. It is really not a rocket science to use FindFirstFile/FindNextFile. After all, there are C applications out there :) – SergeyA Mar 14 '16 at 20:44
  • @BarryTheHatchet, C++ analogy was just that, analogy. It was meant to limit itself to dynamically allocated memory, but I already ran to the limit of max symbols. I thought, it was clear. As for ontology, answers to exist, because they do have names (which are, in strict sense, their titles together with their urls). You may start with https://en.wikipedia.org/wiki/Meta-ontology and go from there, if you are really interested in ontological thinking. – SergeyA Mar 14 '16 at 20:48
  • @SergeyA: While it's nice that you've found a theory to invest in, it's not a fact, and it doesn't "prove" anything. – Lightness Races in Orbit Mar 14 '16 at 22:16

1 Answers1

0

A filesystems library was "added" to C++ in the "Filesystems TS", so you may be able to find an experimental implementation in your compiler's standard library implementation.

However, it's not yet part of any formal standard. My understanding is that it'll be part of C++17.

Until then, Boost it is — or your own hand-crafted code on systems that are neither Windows nor POSIX-compliant.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Even some C++11 features aren't fully implemented yet and C++17 will take his time to grow mature too. I bet boost filesystem easily stays relevant until 2020. – Youka Mar 14 '16 at 20:18
  • @Youka: Oh, certainly. – Lightness Races in Orbit Mar 14 '16 at 20:20
  • 1
    Well, your answer doesn't really go along with your comments. Claiming that you work with non-Posix, non-Windows systems and suggesting using `boost` is a really strange choice - boost itself only caters for Posix or Windows. – SergeyA Mar 14 '16 at 20:32