Batch file. Delete all files and folders in a directory

青春壹個敷衍的年華 提交于 2019-11-27 06:11:54
Jon Martin

del *.* instead of del *.db. That will remove everything.

Create a batch file

copy below text into batch file

set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

Will delete all files AND folders

IF EXIST "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" (
    rmdir "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS" /s /q
)

This will delete everything from the folder (and the folder itself).

del *.* will only delete files, but not subdirectories. To nuke the contents of a directory, you can use this script:

@echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START

:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE

:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd

:DONE
endlocal

Just put this together from what morty346 posted:

set folder="C:\test"
IF EXIST "%folder%" (
    cd /d %folder%
    for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)

Adds a quick check that the folder defined in the variable exists first, then changes directory to the folder and deletes the contents.

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

del /S C:\Path\to\directory\*

RD command can also be used. Recursively delete quietly without prompt

@RD /S /Q %VAR_PATH%

https://technet.microsoft.com/en-gb/library/bb490990.aspx

set "DIR_TO_DELETE=your_path_to_the_folder"

IF EXIST %DIR_TO_DELETE% (
    FOR /D %%p IN ("%DIR_TO_DELETE%\*.*") DO rmdir "%%p" /S /Q
    del %DIR_TO_DELETE%\*.* /F /Q
)

Try this, it works for me, I have an application which dumps data in my "C:\tmp" folder & the following works the best for me, it doesn't even ask Yes or No to delete the data, I have made a schedule for it to run after every 5 minutes

cd "C:\tmp"

del *.* /Q
user3788752

Better yet, let's say I want to remove everything under the c:\windows\temp folder.

@echo off
rd c:\windows\temp /s /q

You could use robocopy to mirror an empty folder to the folder you are clearing.

robocopy "C:\temp\empty" "C:\temp\target" /E /MIR

It also works if you can't remove or recreate the actual folder.

It does require an existing empty directory.

Use

set dir="Your Folder Path Here"
rmdir /s %dir%
mkdir %dir%

This version deletes without asking:

set dir="Your Folder Here"
rmdir /s /q %dir%
mkdir %dir%

Example:

set dir="C:\foo1\foo\foo\foo3"
rmdir /s /q %dir%
mkdir %dir%

This will clear C:\foo1\foo\foo\foo3.

I would like to mention @Abdullah Sabouin with this answer https://stackoverflow.com/a/44578851/8238944. There was a mix up about me copying him. I did not notice his post.

I would like to thank @melpomene for pointing out errors!

grenix

Just a modified Version of https://stackoverflow.com/users/478183/morty346 answer

set folder="C:\test"
cd /D %folder%
if NOT %errorlevel% == 0 (exit /b 1)
echo entire content of %cd% will be deleted, press Ctrl-C to abort
pause
REM first the directories /ad option of dir
for /F "delims=" %%i in ('dir /b /ad') do (echo rmdir "%%i" /s/q)
REM now the files /a-d option of dir
for /F "delims=" %%i in ('dir /b /a-d') do (echo del "%%i" /q)
REM to deactivate simulation mode remove the word echo before rmdir and del
butfly

You cannot delete everything with either rmdir or del alone:

  • rmdir /s /q does not accept wildcard params. So rmdir /s /q * will error.
  • del /s /f /q will delete all files, but empty subdirectories will remain.

My preferred solution (as I have used in many other batch files) is:

rmdir /s /q . 2>NUL
Abdullah Sabouni
@echo off
@color 0A

echo Deleting logs

rmdir /S/Q c:\log\

ping 1.1.1.1 -n 5 -w 1000 > nul

echo adding log folder back

md c:\log\

you was on the right track just add a code to add the folder which is deleted back again

You should run this command in order to delete all the files del*.*

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!