giathienphu
giathienphu

Reputation: 137

how to delete files and subfolder using bat file

Please help me to delete all files and sub folder in MYFILES folder. Below is my code (it is delete MYFILES too but i want to keep it):

rmdir e:\MYFILES\ /s /q

Thanks a lot.

Upvotes: 1

Views: 195

Answers (2)

MC ND
MC ND

Reputation: 70941

pushd "e:\MYFILES" && ( rmdir . /s /q 2>nul & popd )

Change to the desired folder (pushd). If the command was successful (the && is an equivalent to if there are no errors), the folder is now the current active directory, so, remove anything inside it (rmdir . /s /q) As it is the current directory, it can not be deleted (the 2>nul hides the error output when the current folder can not be removed). Once the process ends, return to the previous active directory (popd)

Upvotes: 1

prem
prem

Reputation: 3548

Try this

set targetdir=c:\example
del /q %targetdir%\*
for /d %%x in (%targetdir%\*) do @rd /s /q ^"%%x^"

Upvotes: 1

Related Questions