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 would like to delete all files and subfolders in a batch file in Windows 7 and keep the top folder. Basically emptying the folder. What's the command line instruction for that?

share|improve this question
3  
Not to nitpick, but you're looking for how do do this "from the command prompt" not from "DOS." There hasn't been a DOS subsystem since Win ME. –  MDMarra Aug 9 '10 at 18:53
    
@MarkM: Well, there is/was ntvdm but as 64-bit systems gain market share it's getting increasingly irrelevant. –  Јοеу Aug 10 '10 at 9:06
    
See also stackoverflow.com/questions/1965787/… –  Vadzim Jan 31 at 19:27
add comment

6 Answers

You can do this using del and the /S flag (to tell it to remove all files from all subdirectories):

del /S C:\Path\to\directory\*
share|improve this answer
1  
yours is better :D +1 - Deleted my answer –  BloodPhilia Aug 9 '10 at 16:48
1  
My answer was wrong as the top folder had to remain. –  Tom Wijsman Aug 9 '10 at 16:49
    
+1 more. Also good catch Tom! You seem like one of the more level-headed people here :) –  JNK Aug 9 '10 at 16:49
2  
Explanation: del stands for Delete and S stands for Recursive. –  Tom Wijsman Aug 9 '10 at 16:50
1  
@Tony_Henrich, Learn more about msdos commands here. computerhope.com/msdos.htm –  Moab Aug 9 '10 at 18:21
show 3 more comments

The best Solution: e.g. i want to delete all files and sub-directories of parent directory lets say "C:\Users\Desktop\New folder\". The easy way is create batch file of below three commands.

cd C:\Users\Desktop\New folder\

del * /S /Q

rmdir /S /Q "C:\Users\Desktop\New folder\"

Here first it will clean all files in all sub-directories and then cleans all empty sub-directories. Since current working directory is parent directory i.e."\New folder", rmdir command can't delete this directory itself.

share|improve this answer
add comment
rmdir "c:\pathofyourdirectory" /q /s

Don't forget to use the quotes and for the /q /s it will delete all the repositories and without prompting.

share|improve this answer
    
The question was "I would like to delete all files and subfolders in a batch file in Windows 7 and keep the top folder." –  Werner Henze Feb 5 at 17:06
add comment

This worked better for me when I had spaces in the folder names.

@echo off
REM ---- Batch file to clean out a folder
REM Checking for command line parameter
if "%~1"=="" (

echo Parameter required.
exit /b 1

) else (
echo ***********************************************************************************
    echo *** Deleting all files, including the ones in the subdirs, without confirmation *** 
    del "%~1\*" /S /Q
echo ***********************************************************************************
    REM Deleting all the empty subdirs that were left behind
FOR /R "%~1" %%D IN (.) DO (
    if "%%D"=="%~1\."  (
    echo *** Cleaning out folder: %~1 *** 
    ) else (
    echo Removed folder "%%D"
    rmdir /S /Q "%%D"
    )
) 

    REM All good.
    exit /b 0

)
share|improve this answer
    
People always forget about having spaces in the names. –  Kevin Panko Feb 13 at 18:48
add comment

If you want to delete all files in a folder, including all subfolders and not rely on some error conditions to keep the root folder intact (like I saw in another answer) you could have a batch file like this:

@echo off

REM Checking for command line parameter
if "%~1"=="" (

    echo Parameter required.
    exit /b 1

) else (

    REM Change directory and keep track of the previous one
    pushd "%~1"

    if errorlevel 1 (

        REM The directory passed from command line is not valid, stop here.
        exit /b %errorlevel%

    ) else (

        REM First we delete all files, including the ones in the subdirs, without confirmation
        del * /S /Q

        REM Then we delete all the empty subdirs that were left behind
        for /f %%D IN ('dir /b /s /a:d "%~1"') DO rmdir /S /Q "%%D"

        REM Change directory back to the previous one
        popd

        REM All good.
        exit /b 0
    )

)

And then you would simply call it with:

empty_my_folder.bat "C:\whatever\is\my folder"
share|improve this answer
add comment

Navigate to the parent directory

#Line1 pushd "Parent Directory"

Delete the sub folders

#Line2 rd /s /q . 2>nul
share|improve this answer
    
Wow, that's hackish. :) –  Tarnay Kálmán Jul 3 at 13:08
    
This is essentially equivalent to two previous answers. –  Scott Jul 3 at 13:27
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.