Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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. – Joey Aug 10 '10 at 9:06
    
See also stackoverflow.com/questions/1965787/… – Vadzim Jan 31 '14 at 19:27

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
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
3  
and using /Q keeps empty sub folders which is undesirable. – Tony_Henrich Aug 9 '10 at 20:27

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
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 '14 at 17:06

Navigate to the parent directory

#Line1 pushd "Parent Directory"

Delete the sub folders

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

you can use rmdir to delete the files and subfolder, like this:

rmdir /s/q MyFolderPath

However, it is significantly faster, especially when you have a lot of subfolders in your structure to use del before the rmdir, like this:

del /f/s/q MyFolderPath > nul
rmdir /s/q MyFolderPath
share|improve this answer

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

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 '14 at 18:48

You must log in to answer this question.

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