Delete content including folders

1

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

  • Using the command del it deletes only files and the folders are (you have to delete the folders as well).
  • Using the command rd it deletes beyond the whole content, deletes the root folder as well.

How can I do this?

    
asked by anonymous 14.06.2017 / 15:58

2 answers

5

If you want to keep the root directory for some reason and you 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 one recursively removes all nested directories, and keeps the root level directory as it is (except for its deleted content). Note that targetpath is the directory path you want to empty.

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

del /q caminho_destino\*
for /d %%x in (caminho_destino\*) do @rd /s /q "%%x"
    
14.06.2017 / 16:17
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\*.*
    
14.06.2017 / 16:10