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?
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.
user340956 was painfully close to the solution, but you know what they say about close…
To be clear,
rd /s /q c:\foobar deletes the target directory in addition to its contents, but you don't always want to delete the directory itself, sometimes you just want to delete its contents and leave the directory alone. The
deltree command could do this, but Micrsoft, in its infinite "wisdom" removed the command and didn't port it to Windows.
Here's a solution that works without resorting to third-party tools. It's probably about as simple and efficient as is possible with a command-line script instead of outright writing an actual executable. It doesn't set any environment variables and it doesn't use any loops. It's also as safe as can be, with error-checking everywhere possible, and also as user-friendly as possible, with built-in docs.
dt.cmd for the kids; whatever, I'm old, I use
:: dt is a Windows-compatible version of the deltree command :: Posted to SuperUser by Synetech: https://superuser.com/a/1526232/3279 @echo off goto start :start if ["%~1"]==[""] goto usage pushd "%~1" 2>nul if /i not ["%cd%"]==["%~1"] goto wrongdir rd /s /q "%~1" 2>nul popd goto :eof :usage echo Delete all of the contents of a directory echo. echo ^> %0 DIR echo. echo %0 is a substitute for deltree, it recursively deletes the contents echo (files and folders) of a directory, but not the directory itself echo. echo DIR is the directory whose contents are to be deleted goto :eof :wrongdir echo Could not change to the target directory. Invalid directory? Access denied? goto :eof
Here's how it works:
- It checks if a command-line argument has been passed, and prints usage information and quits if not.
- It uses
pushdto save the current directory, then switch to the target directory, redirecting any errors to
nulfor a cleaner command-line experience (and cleaner logs).
- It checks to see if the current directory is now the same as the target directory, and prints an error message and quits if it is not. This avoids accidentally deleting the contents of the previous directory if the
pushdcommand failed (e.g., passing an invalid directory, access-error, etc.)
- This check is case-insensitive, so it's usually safe on Windows, but isn't for any case-sensitive file-systems like those used by *nix systems, even under Windows.
- It doesn't work with short-filenames (e.g.
C:\Users\Bob Bobson\foobarwon't be seen as being the same as
C:\Users\BobBob~1\foobareven if they actually are). It's a slight inconvenience to have to use the non-short filename, but it's better safe than sorry, especially since SFNs aren't completely reliable or always predictable (and may even be disabled altogether).
- It then uses
rdto delete the target directory and all of its contents, redirecting any errors (which there should be at least one for the directory itself) to
nul. Some notes about this:
- Because the target directory is the current directory, the system has an open file-handle to it, and thus it cannot actually delete it, so it remains as is, which is the desired behavior.
- Because it doesn't try to remove the target directory until after its contents have been removed, it should now be empty (other than anything that also has open file handles).
- Finally, it uses
popdto return to the previously-current directory and ends the script.
(If you like, you can comment the script with the above descriptions using
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"
To delete file:
To delete folder with all files in it:
rmdir /s /q PATH_TO_FOLDER
To delete all files from specific folder (not deleting folder itself) is a little bit complicated.
del /s *.* cannot delete folders, but removes files from all subfolder. So two commands are needed:
del /q PATH_TO_FOLDER\*.* for /d %i in (PATH_TO_FOLDER\*.*) do @rmdir /s /q "%i"
You can create a script to delete whatever you want (folder or file) like this
@echo off setlocal enableextensions if "%~1"=="" ( echo Usage: %0 path exit /b 1 ) :: check whether it is folder or file set ISDIR=0 set ATTR=%~a1 set DIRATTR=%ATTR:~0,1% if /i "%DIRATTR%"=="d" set ISDIR=1 :: Delete folder or file if %ISDIR%==1 (rmdir /s /q "%~1") else (del "%~1") exit /b %ERRORLEVEL%
Few example of usage:
mydel.bat "path\to\folder with spaces" mydel.bat path\to\file_or_folder
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 )
If you wanted to empty the folder, my take is:
@ECHO OFF :choice cls set /P c=Which directory? [Desktop, Documents, Downloads, Pictures] if /I "%c%" EQU "Desktop" set Point = "Desktop" if /I "%c%" EQU "Documents" set Point = "Documents" if /I "%c%" EQU "Downloads" set Point = "Downloads" if /I "%c%" EQU "Pictures" set Point = "Pictures" if /I "%c%" EQU "Videos" set Point = "Videos" goto choice set /P d=Which subdirectory? If you are putting multiple. Your's should be like "path/to/folder" (no files!!) IF NOT EXIST C:\Users\%USERNAME%\%Point%\%d% GOTO NOWINDIR rmdir C:\Users\%USERNAME%\%Point%\%d% mkdir C:\Users\%USERNAME%\%Point%\%d% :NOWINDIR mkdir C:\Users\%USERNAME%\%Point%\%d%
Simple as that! I hope I helped you out! I recommend you to take the whole code, if you don't want to take the whole code, then you can simplify this with.
IF NOT EXIST *path here* GOTO NOWINDIR rmdir *path here* mkdir *path here* :NOWINDIR mkdir *path here*
EDIT: rmdir won't work if it isn't empty. To fix that.
IF NOT EXIST *path here* GOTO NOWINDIR del *path here*/* /S /Q (dont copy this, the above prevents the del command from deleting everything in the folder, this is simallar to another answer.) rmdir *path here* mkdir *path here* :NOWINDIR mkdir *path here*
Not sure if this works but...
sdelete -s -p *path here*/*
1. What the OP asked for
del /f /s /q "C:\some\Path\*.*" rmdir /s /q "C:\some\Path" mkdir "C:\some\Path"
That will remove all files and folders in and including the directory of
"C:\some\Path" but remakes the top directory at the end.
2. What most people will want
del /f /s /q "C:\some\Path\*.*" rmdir /s /q "C:\some\Path"
That will completely remove
"C:\some\Path" and all of its contents
If OP has some oddly specific requirement to not touch the top-level directory in any capacity... they should mention that in their question :)
Seems everyone is missing the fact that we're wanting to delete multiple sub folders, but NOT delete the parent folder. We may also no know all the names of the subfolders, and don't want to do each one individually.
So, thinking outside the box, this is how I solved this issue.
Robocopy /Purge c:\EmptyFolderToBeDeletedSoon c:\FolderIWantEmpty
Make a temp directory that's empty. Use the RoboCopy command with the /Purge switch (/PURGE :: delete dest files/dirs that no longer exist in source.) using the empty folder as the source, and the folder we want empty as the destination. Delete the empty temp folder we created to be the empty source for Robocopy.
Now, you have an empty folder of all files and folders, which is what this whole string was about.
Example: Delete everything (folders/subfolders/files) in 3D Objects folder but want to leave 3D Objects folder alone
pathThere="C:\Users\PhilosophyPoet\3D Objects" CD pathThere RMDIR /s /q pathThere
When CMD is oriented to working directory, using RMDIR will delete all folders, subfolders and files from the working directory. Seems like CMD process cannot process itself just like 'I can't throw myself into rubbish bin because the rubbish bin need to be seal by someone'
Here's a two-line solution I just came up with, possibly exploiting a bug or unexpected behavior in
robocopy. This works with the newest version of
robocopy on Windows 10 at this writing.
It mirror syncs an empty sub-folder to its parent folder. In other words, it tells the parent folder to have all the same files as the sub-folder: none. Amusingly, this means it also deletes the empty sub-folder that it is instructed to sync with.
This example will empty the
Temp folder for the current user. Note that it is using the %TEMP% environment variable, which
cmd expands to whatever that may be, for example
mkdir %TEMP%\i_like_cheez robocopy /mir %TEMP%\i_like_cheez %TEMP%