

It's a slight inconvenience to have to use the non-short filename, but it's better safe than sorry, especially since SFNs aren't completely reliable or always predictable (and may even be disabled altogether). C:\Users\Bob Bobson\foobar won't be seen as being the same as C:\Users\BobBob~1\foobar even if they actually are).

If /i not = goto wrongdirĮcho Delete all of the contents of a directoryĮcho %0 is a substitute for deltree, it recursively deletes the contentsĮcho (files and folders) of a directory, but not the directory itselfĮcho DIR is the directory whose contents are to be deletedĮcho Could not change to the target directory. bat 🤷): :: dt is a Windows-compatible version of the deltree command It's also as safe as can be, with error-checking everywhere possible, and also as user-friendly as possible, with built-in docs.ĭt.bat (or dt.cmd for the kids whatever, I'm old, I use. It doesn't set any environment variables and it doesn't use any loops. It's probably about as simple and efficient as is possible with a command-line script instead of outright writing an actual executable.

Here's a solution that works without resorting to third-party tools. The deltree command could do this, but Micrsoft, in its infinite "wisdom" removed the command and didn't port it to Windows. To be clear, rd /s /q c:\foobar deletes the target directory in addition to its contents, but you don't always want to delete the directory itself, sometimes you just want to delete its contents and leave the directory alone. User340956 was painfully close to the solution, but you know what they say about close…
