Indeed, that does the trick on *nix, even though we won't be able to catch rmdir errors in case it fails on empty directories, but I think that's not too bad in this case.
Agreed. It will just write an error message on stderr in that case.
Any idea how to do something equivalent on Windows?
A web search shows that Windows also has an "rmdir" command and it should work the same way as the Unix command for this purpose, but I haven't tested it: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/rmdir