Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What commands can I use (from the command prompt) to delete all files and all subdirectories from a folder, but not delete the folder itself? Basically at the end of the delete, there should be an empty folder.

share|improve this question

migrated from stackoverflow.com Dec 9 '10 at 2:37

This question came from our site for professional and enthusiast programmers.

    
What have you tried? –  Andrew Cooper Dec 8 '10 at 23:14
    
i know rmdir /s but it deleting that folder itself also –  subash Dec 8 '10 at 23:22
    
window command prompt –  subash Dec 8 '10 at 23:25

2 Answers 2

cd <foldername>
del *.*

will delete the files. You'll need to do

rmdir /s <subfolder>

for each subfolder.

Update

Try this in a batch file:

@echo off
cd "%1"
del *.* /y
for /d %%i in (*) do rmdir /s /q "%%i" 

Call it something like EmptyDir.bat. Then you can type:

emptydir <dirname>

and it will delete the files and folder in that folder, but leave the folder there.

share|improve this answer
    
can't we do some generic coding for this –  subash Dec 8 '10 at 23:21
    
See update for example script –  Andrew Cooper Dec 8 '10 at 23:39
1  
It's been a long time since you needed *.* to indicate all files. Just * works fine (Windows has a concession where *.* matches all files, even those without dots in the middle). –  Greg Hewgill Dec 8 '10 at 23:54
    
@Greg Hewgill: True. Force of habit. –  Andrew Cooper Dec 9 '10 at 0:11

$> wipe.bat myExampleFolder

rmdir /s $1
mkdir $1    

With this solution, you may specify relative to current folder or even absolute paths.

Be sure to be aware of dangers. I.e. if the folder you chose to delete does not exists. (That is, why for example cd <targetdir>| del *.* can be quite dangerous... If you look for other solutions, current-directory-restore might be your friend:

@set OLDDIR=%CD%
cd $1
echo do stuff...
@cd /d %OLDDIR%
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.