批处理文件。删除目录中的所有文件和文件夹。

时间:2022-01-25 05:28:46

I want to have a batch file that will delete all the folders and files in my Cache folder for my wireless toolkit.

我想要一个批处理文件,它将删除我的无线工具箱的缓存文件夹中的所有文件夹和文件。

Currently I have the following:

目前我有以下几点:

cd "C:\Users\tbrollo\j2mewtk\2.5.2\appdb\RMS"
del *.db

This will delete all .db files in my RMS directory, however I want to delete every single thing from this directory

这将删除我的RMS目录中的所有.db文件,但是我想从这个目录中删除所有的东西。

Can you help me out? Thanks.

你能帮我吗?谢谢。

15 个解决方案

#1


39  

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

德尔*。代替del *.db。这将删除一切。

#2


128  

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

将删除所有文件和文件夹?

#3


26  

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

这将从文件夹(以及文件夹本身)中删除所有内容。

#4


9  

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

德尔*。*只会删除文件,但不会删除子目录。若要nuke目录的内容,您可以使用此脚本:

@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

#5


9  

Just put this together from what morty346 posted:

这是morty346发布的内容:

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.

添加一个快速检查,在变量中定义的文件夹首先存在,然后将目录更改为文件夹并删除内容。

#6


6  

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

您可以使用del和/S标志(告诉它递归所有子目录中的所有文件):

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

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

也可以使用RD命令。递归地删除,没有提示。

@RD /S /Q %VAR_PATH%

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

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

#7


4  

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

试试这个,对我来说很有用,我有一个应用程序,可以在我的“C:\tmp”文件夹中转储数据,下面的工作对我来说是最好的,它甚至不要求删除数据,我已经为它制定了每5分钟运行一次的时间表。

cd "C:\tmp"

del *.* /Q

#8


4  

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
)

#9


2  

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

更好的是,假设我想删除c:\windows\temp文件夹下的所有内容。

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

#10


2  

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

您可以使用robocopy将一个空文件夹镜像到您正在清理的文件夹。

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.

它确实需要一个现有的空目录。

#11


1  

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.

这将明确的C:\ foo1 \ foo \ foo \ foo3。

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

我想提一下@Abdullah Sabouin,这个答案是https://*.com/a/44578851/8238944。关于我抄袭他的事有一种混淆。我没有注意到他的帖子。

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

我要感谢@melpomene指出错误!

#12


0  

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

只需修改版本的https://*.com/users/478183/morty346答案。

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

#13


0  

You cannot delete everything with either rmdir or del alone:

您不能仅用rmdir或del单独删除所有内容:

  • rmdir /s /q does not accept wildcard params. So rmdir /s /q * will error.
  • rmdir /s /q不接受通配符参数。所以rmdir /s /q *会出错。
  • del /s /f /q will delete all files, but empty subdirectories will remain.
  • del /s /f /q将删除所有文件,但空的子目录将保留。

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

我喜欢的解决方案(正如我在许多其他批处理文件中使用的)是:

rmdir /s /q . 2>NUL

#14


0  

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

您应该运行这个命令来删除所有的文件。*。

#15


-1  

@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

您在正确的轨道上添加了一个代码来添加被删除的文件夹。

#1


39  

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

德尔*。代替del *.db。这将删除一切。

#2


128  

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

将删除所有文件和文件夹?

#3


26  

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

这将从文件夹(以及文件夹本身)中删除所有内容。

#4


9  

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

德尔*。*只会删除文件,但不会删除子目录。若要nuke目录的内容,您可以使用此脚本:

@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

#5


9  

Just put this together from what morty346 posted:

这是morty346发布的内容:

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.

添加一个快速检查,在变量中定义的文件夹首先存在,然后将目录更改为文件夹并删除内容。

#6


6  

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

您可以使用del和/S标志(告诉它递归所有子目录中的所有文件):

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

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

也可以使用RD命令。递归地删除,没有提示。

@RD /S /Q %VAR_PATH%

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

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

#7


4  

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

试试这个,对我来说很有用,我有一个应用程序,可以在我的“C:\tmp”文件夹中转储数据,下面的工作对我来说是最好的,它甚至不要求删除数据,我已经为它制定了每5分钟运行一次的时间表。

cd "C:\tmp"

del *.* /Q

#8


4  

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
)

#9


2  

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

更好的是,假设我想删除c:\windows\temp文件夹下的所有内容。

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

#10


2  

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

您可以使用robocopy将一个空文件夹镜像到您正在清理的文件夹。

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.

它确实需要一个现有的空目录。

#11


1  

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.

这将明确的C:\ foo1 \ foo \ foo \ foo3。

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

我想提一下@Abdullah Sabouin,这个答案是https://*.com/a/44578851/8238944。关于我抄袭他的事有一种混淆。我没有注意到他的帖子。

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

我要感谢@melpomene指出错误!

#12


0  

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

只需修改版本的https://*.com/users/478183/morty346答案。

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

#13


0  

You cannot delete everything with either rmdir or del alone:

您不能仅用rmdir或del单独删除所有内容:

  • rmdir /s /q does not accept wildcard params. So rmdir /s /q * will error.
  • rmdir /s /q不接受通配符参数。所以rmdir /s /q *会出错。
  • del /s /f /q will delete all files, but empty subdirectories will remain.
  • del /s /f /q将删除所有文件,但空的子目录将保留。

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

我喜欢的解决方案(正如我在许多其他批处理文件中使用的)是:

rmdir /s /q . 2>NUL

#14


0  

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

您应该运行这个命令来删除所有的文件。*。

#15


-1  

@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

您在正确的轨道上添加了一个代码来添加被删除的文件夹。