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.

I have a folder on my Windows system with the following contents:

c:\folder\folder1
c:\folder\folder2
c:\folder\folder3
c:\folder\folder4
c:\folder\folder5
c:\folder\folder6
c:\folder\A.bat
c:\folder\b.bat

I want to run a batch file that leaves only

c:\folder\A.bat
c:\folder\b.bat

and deletes the rest. I.e., delete all sub-folders, irrespective of whether they contain files themselves, and retain only the "top level" files.

How can I do this?

share|improve this question
    
So you want to delete all sub-folders, irrespective of whether they contain files themselves, and retain only the "top level" files? If this is a one-time thing it can be done far more easily via Explorer itself. –  Karan Jan 3 '13 at 18:43
add comment

1 Answer 1

Navigate to C:\folder and run the following command:

for /f "delims=" %d in ('dir /ad /b') do @rd /q /s "%d"

This will get rid of all sub-directories under C:\folder and leave only the files. If you want to use it in a batch file, use %% instead of %.

share|improve this answer
2  
Since you're new here, remember to accept the answers that helped you solve your problem. –  Karan Jan 3 '13 at 18:55
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.