Delete contents including folders

Asked

Viewed 1,283 times

1

I am trying to delete the contents of a folder but I have the following problem:

  • Using the command del it erases only files and folders get (you have to delete the folders too).
  • Using the command rd it erases beyond the whole contents, erases the root folder as well.

How can I do that ?

  • Try rd /s /q

  • @acklay tried but it erases the root too.

  • If you put it like that: rd /s /q C:\path\to\directory\* will not?!

  • @acklay does not give, it happens until an error message saying that is incorrect the label if you put the * .

  • See my answer, I tested it here and it worked. You can also use the Erase.

  • Take a look here for help: https://ss64.com/nt/del.html

  • Very Good! Delete sub-folders and files, but keep ROOT.

Show 2 more comments

2 answers

6


If you want to keep the root directory for some reason and really want to empty it, then you can do the following:

del /q caminho_destino\*
for /d %x in (caminho_destino\*) do @rd /s /q "%x"

The first command removes all files from the directory, and then the second recursively removes all nested directories, and keeps the root-level directory as is (except for its content that has been deleted). It is noteworthy that way_destination is the path of the directory you want to empty.

Note that inside a file . bat you need to fold the % in the loop for:

del /q caminho_destino\*
for /d %%x in (caminho_destino\*) do @rd /s /q "%%x"
  • Show, thank you very much.

  • Dispose, I’m glad you solved.

  • Very good, I ended up having the same idea only that I only saw your post after edited my answer. It happens. :-)

0

Edited, Script:

The first version does not delete the subfolders, so try the script below:

del /q "C:\Teste\*.*"
FOR /D %%p IN ("C:\Teste\*.*") DO rmdir "%%p" /s /q

mkdir teste
cd \teste
mkdir files
cd files
echo $null >> file.txt
cd \
del /S teste\*.*
  • I tried, it just erases the files, the folder continues.

  • The root folder? you wanted to delete it too?

  • Your script doesn’t work, note... But I don’t want to delete the root, only its contents.

  • Okay, check that didn’t work at all, I’ll edit for a new version

  • I have not tested why I am on linux, but as soon as I release a mq windows here I will test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.