0

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?

1
  • 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, 2013 at 18:43

1 Answer 1

3

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 %.

1
  • 2
    Since you're new here, remember to accept the answers that helped you solve your problem.
    – Karan
    Jan 3, 2013 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .