0

I know, there are very similar questions to this, but I couldn't find the correct answer to this one.

My folder structure looks like this:

\Path\To\BaseDir
    \FolderToDelete1
        \File1
        \File2
    \FolderToDelete2
        \File3
        \File4
    \File5

What I need is a one line of command (that's a requirement I must fulfill) what would:

  1. delete every file and folder (recursively) from my BaseDir
  2. delete every folder (recursively) from my BaseDir
  3. delete only every file from my BaseDir (and not folders)

I can't use Power Shell. I can't run batch files. I need a single line of command.

In Linux shell the command I search for option 1 would look like this:

rm -Rf \Path\To\BaseDir\*

What is the Windows CLI equivalent of the above command? My actual path contains spaces, like: "D:/Path to Dir/BaseDir"

elaspog
  • 1,635
  • 3
  • 21
  • 51
  • The linked answer is not an answer to this question. The linked solutions are not one-liners. I do not want to delete my BaseDir directory, just it's contents, but the linked answers do this. I'm updating the question, because I can't run batch files either. – elaspog Oct 15 '19 at 13:53
  • The solution to your problem does effectively lie within one or more of the answers posted to that question, so I believe as did @Gem, that it is a duplicate too! You'll need to change directory to `"D:\Path to Dir\BaseDir"` and then remove that directory whist you're in it, to prevent it from also being removed. Oh and if you really wanted an equivalent of your posted Linux command, would you like it to fail too? _(because `rm -Rf D:\Path to Dir\BaseDir\*` wouldn't be correct)_. – Compo Oct 15 '19 at 14:11
  • In the land of "have you tried" - perhaps try deleting "X:\path\to\dir\" or "X:\path\to\dir\." The extra dot would be enough on linux to stop it deleting the folder, but I don't know the windows commands well enough these days. – Gem Taylor Oct 15 '19 at 14:16
  • @Compo I think you haven't understood the conditions well. An answer with two lines of command might be a working solution, but in my case won't solve the problem. I can output only one command (I have this special case I can't change). Changing the directory and deleting the structure won't help in my case. These contitions were included into my questions and were not included into the linked question. Therefore it can't be duplicate, because the two questions have totally different contitions. – elaspog Oct 15 '19 at 14:30
  • Well, please explain why you've got the requirement of using a single command; regard that calling a single executable is not the same as a single command, particularly when it comes to interla commands of `cmd.exe`, like `cd`, `dir`, `del`, `rd`, `for`,...; so using something like `cd ... && rd ... && del ...` could be done by `cmd /C "cd ... && rd ... && del ..."`, which calls just a single executable but several commands... – aschipfl Oct 15 '19 at 14:55
  • @aschipfl Does this really matter? These are the conditions of the question, what can be hypothetical, imaginary or real, but in none of the mentioned cases makes this question a duplicate, because it's simply different from the linked question. BTW, the reason is that I need to use a very old Alteryx engine, I can't output flat files, the qouting in output format is not allowed in my special case and unfortunately the & should be be my delimiter due to data specific reasons. Are you happier now? – elaspog Oct 15 '19 at 18:03
  • 1
    See [foxidrive's answer](https://stackoverflow.com/a/17097668/3074564) for the solution and [my answer](https://stackoverflow.com/a/50656521/3074564) for the explanation in first referenced topic how to delete really __all__ files and folders recursively in specified directory with no directory used as current directory by any running process which prevents the deletion of the directory and no file opened by any running process which prevents the deletion of the opened file. – Mofi Oct 16 '19 at 05:19

1 Answers1

1

If you study the other answers, something that works can be assembled.

After re-reading the question, it appears that you are looking for three (3) commands.

-- delete every file and folder (recursively) from my BaseDir

CD /D "C:\Path\To\BaseDir" && FOR /F "delims=" %D IN ('DIR /A:D /B') DO (RMDIR /S /Q "%~D") && FOR /F "delims=" %F IN ('DIR /A:-D /B') DO (DEL "%~F")

-- delete every folder (recursively) from my BaseDir

CD /D "C:\Path\To\BaseDir" && FOR /F "delims=" %D IN ('DIR /A:D /B') DO (RMDIR /S /Q "%~D")

-- delete only every file from my BaseDir (and not folders)

CD /D "C:\Path\To\BaseDir" && FOR /F "delims=" %F IN ('DIR /A:-D /B') DO (DEL "%~F")
lit
  • 14,456
  • 10
  • 65
  • 119