如何在Windows中命令提示符下的特定目录中删除文件/子文件夹?

时间:2022-08-16 23:58:35

Say, there is a variable called %pathtofolder%, as it makes clear it is a full path of a folder.

比方说,有一个名为%pathtofolder%的变量,因为它表明它是一个文件夹的完整路径。

I want to delete every single file and subfolder in this directory, but not the directory itself.

我想删除这个目录中的每一个文件和子文件夹,而不是目录本身。

But, there might be an error like 'this file/folder is already in use'... when that happens, it should just continue and skip that file/folder.

但是,可能会出现“这个文件/文件夹已经在使用”的错误……当这种情况发生时,它应该继续并跳过该文件/文件夹。

Can anyone give me some command for this?

有人能给我一些命令吗?

13 个解决方案

#1


191  

You can use this shell script to clean up the folder and files within C:\Temp source:

您可以使用这个shell脚本来清理C:\Temp源代码中的文件夹和文件:

del /q "C:\Temp\*"
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q

Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat

创建一个包含上述命令的批处理文件(例如,delete.bat)。转到删除的位置。bat文件位于然后运行该命令:delete.bat。

#2


252  

rmdir is my all time favorite command for the job. It works for deleting huge files and folders with subfolders. A backup is not created, so make sure that you have copied your files safely before running this command.

rmdir是我最喜欢的工作命令。它用于删除带有子文件夹的大文件和文件夹。没有创建备份,所以在运行此命令之前,请确保您已经安全地复制了文件。

RMDIR "FOLDERNAME" /S /Q

This silently removes the folder and all files and subfolders.

这将静默地删除文件夹和所有文件和子文件夹。

#3


71  

The simplest solution I can think of is removing the whole directory with

我能想到的最简单的解决方案是将整个目录删除。

RD /S /Q folderPath

Then creating this directory again:

然后再创建这个目录:

MD folderPath

#4


43  

This will remove the folders and files and leave the folder behind.

这将删除文件夹和文件,并将文件夹放在后面。

pushd "%pathtofolder%" && (rd /s /q "%pathtofolder%" 2>nul & popd)

#5


33  

@ECHO OFF

Set dir=path-to-dir

Echo Deleting all files from %dir%
del %dir%\* /F /Q

Echo Deleting all folders from %dir%
for /d %%p in (%dir%\*) Do rd /Q /S "%%p"
@echo Folder deleted.


exit

...deletes all files and folders underneath the given directory, but not the directory itself.

删除给定目录下的所有文件和文件夹,但不删除目录本身。

#6


17  

CD [Your_Folder]
RMDIR /S /Q .

You'll get an error message, tells you that the RMDIR command can't access the current folder, thus it can't delete it.

您将得到一个错误消息,告诉您RMDIR命令不能访问当前文件夹,因此不能删除它。

Update:

更新:

From this useful comment (thanks to Moritz Both), you may add && between, so RMDIR won't run if the CD command fails (e.g. mistyped directory name):

从这个有用的注释(感谢Moritz Both),您可以添加&& between,所以如果CD命令失败(例如,错误的目录名),RMDIR不会运行:

CD [Your_Folder] && RMDIR /S /Q .

From Windows Command-Line Reference:

从Windows命令行参考:

/S: Deletes a directory tree (the specified directory and all its subdirectories, including all files).

/S:删除目录树(指定目录及其所有子目录,包括所有文件)。

/Q: Specifies quiet mode. Does not prompt for confirmation when deleting a directory tree. (Note that /q works only if /s is specified.)

/问:指定安静模式。在删除目录树时,不提示确认。(注意,/q只在指定的情况下才有效。)

#7


8  

RD stands for REMOVE Directory.

RD表示删除目录。

/S : Delete all files and subfolders in addition to the folder itself. Use this to remove an entire folder tree.

/S:删除除文件夹本身之外的所有文件和子文件夹。使用此方法删除整个文件夹树。

/Q : Quiet - do not display YN confirmation

Q:安静-不要显示YN的确认。

Example :

例子:

RD /S /Q C:/folder_path/here

#8


6  

I use Powershell

我使用Powershell

Remove-Item c:\scripts\* -recurse

It will remove the contents of the folder, not the folder itself.

它将删除文件夹的内容,而不是文件夹本身。

#9


4  

Use notepad to create text document and copy/paste this:

使用记事本创建文本文件和复制/粘贴:

rmdir /s/q "%temp%"
mkdir "%temp%"

Select save as, File name:

选择save as,文件名:

delete_temp.bat

delete_temp.bat

Save as type: All files and click Save button.

保存为类型:所有文件和点击保存按钮。

Work on any kind of account(administrator or a standard user), just run it! I use temp variable in this example, but you can use any other! p.s. For Windows OS only!

处理任何类型的帐户(管理员或标准用户),运行它!在这个示例中,我使用了temp变量,但是您可以使用任何其他变量!只适用于Windows操作系统!

#10


3  

You can do by using the following command to delete all contents and parent folder itself:

您可以使用以下命令删除所有内容和父文件夹本身:

RMDIR [/S] [/Q] [drive:]path            

#11


2  

To delete file:

删除文件:

del PATH_TO_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:

要从特定的文件夹(而不是删除文件夹本身)中删除所有文件有点复杂。德尔/ s *。不能删除文件夹,但删除所有子文件夹中的文件。因此需要两个命令:

del /q PATH_TO_FOLDER\*.*
for /d %i in (PATH_TO_FOLDER\*.*) do @rmdir /s /q "%i"

#12


0  

@ECHO OFF
rem next line removes all files in temp folder
DEL /A /F /Q /S "%temp%\*.*"
rem next line cleans up the folder's content
FOR /D %%p IN ("%temp%\*.*") DO RD "%%p" /S /Q

#13


-9  

del %pathtofolder%\*.*   /s /f  /q

delete all files and subfolders in %pathtofolder% , including read-only files, do not prompt for confirmation

删除%pathtofolder%的所有文件和子文件夹,包括只读文件,不提示确认。

#1


191  

You can use this shell script to clean up the folder and files within C:\Temp source:

您可以使用这个shell脚本来清理C:\Temp源代码中的文件夹和文件:

del /q "C:\Temp\*"
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q

Create a batch file (say, delete.bat) containing the above command. Go to the location where the delete.bat file is located and then run the command: delete.bat

创建一个包含上述命令的批处理文件(例如,delete.bat)。转到删除的位置。bat文件位于然后运行该命令:delete.bat。

#2


252  

rmdir is my all time favorite command for the job. It works for deleting huge files and folders with subfolders. A backup is not created, so make sure that you have copied your files safely before running this command.

rmdir是我最喜欢的工作命令。它用于删除带有子文件夹的大文件和文件夹。没有创建备份,所以在运行此命令之前,请确保您已经安全地复制了文件。

RMDIR "FOLDERNAME" /S /Q

This silently removes the folder and all files and subfolders.

这将静默地删除文件夹和所有文件和子文件夹。

#3


71  

The simplest solution I can think of is removing the whole directory with

我能想到的最简单的解决方案是将整个目录删除。

RD /S /Q folderPath

Then creating this directory again:

然后再创建这个目录:

MD folderPath

#4


43  

This will remove the folders and files and leave the folder behind.

这将删除文件夹和文件,并将文件夹放在后面。

pushd "%pathtofolder%" && (rd /s /q "%pathtofolder%" 2>nul & popd)

#5


33  

@ECHO OFF

Set dir=path-to-dir

Echo Deleting all files from %dir%
del %dir%\* /F /Q

Echo Deleting all folders from %dir%
for /d %%p in (%dir%\*) Do rd /Q /S "%%p"
@echo Folder deleted.


exit

...deletes all files and folders underneath the given directory, but not the directory itself.

删除给定目录下的所有文件和文件夹,但不删除目录本身。

#6


17  

CD [Your_Folder]
RMDIR /S /Q .

You'll get an error message, tells you that the RMDIR command can't access the current folder, thus it can't delete it.

您将得到一个错误消息,告诉您RMDIR命令不能访问当前文件夹,因此不能删除它。

Update:

更新:

From this useful comment (thanks to Moritz Both), you may add && between, so RMDIR won't run if the CD command fails (e.g. mistyped directory name):

从这个有用的注释(感谢Moritz Both),您可以添加&& between,所以如果CD命令失败(例如,错误的目录名),RMDIR不会运行:

CD [Your_Folder] && RMDIR /S /Q .

From Windows Command-Line Reference:

从Windows命令行参考:

/S: Deletes a directory tree (the specified directory and all its subdirectories, including all files).

/S:删除目录树(指定目录及其所有子目录,包括所有文件)。

/Q: Specifies quiet mode. Does not prompt for confirmation when deleting a directory tree. (Note that /q works only if /s is specified.)

/问:指定安静模式。在删除目录树时,不提示确认。(注意,/q只在指定的情况下才有效。)

#7


8  

RD stands for REMOVE Directory.

RD表示删除目录。

/S : Delete all files and subfolders in addition to the folder itself. Use this to remove an entire folder tree.

/S:删除除文件夹本身之外的所有文件和子文件夹。使用此方法删除整个文件夹树。

/Q : Quiet - do not display YN confirmation

Q:安静-不要显示YN的确认。

Example :

例子:

RD /S /Q C:/folder_path/here

#8


6  

I use Powershell

我使用Powershell

Remove-Item c:\scripts\* -recurse

It will remove the contents of the folder, not the folder itself.

它将删除文件夹的内容,而不是文件夹本身。

#9


4  

Use notepad to create text document and copy/paste this:

使用记事本创建文本文件和复制/粘贴:

rmdir /s/q "%temp%"
mkdir "%temp%"

Select save as, File name:

选择save as,文件名:

delete_temp.bat

delete_temp.bat

Save as type: All files and click Save button.

保存为类型:所有文件和点击保存按钮。

Work on any kind of account(administrator or a standard user), just run it! I use temp variable in this example, but you can use any other! p.s. For Windows OS only!

处理任何类型的帐户(管理员或标准用户),运行它!在这个示例中,我使用了temp变量,但是您可以使用任何其他变量!只适用于Windows操作系统!

#10


3  

You can do by using the following command to delete all contents and parent folder itself:

您可以使用以下命令删除所有内容和父文件夹本身:

RMDIR [/S] [/Q] [drive:]path            

#11


2  

To delete file:

删除文件:

del PATH_TO_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:

要从特定的文件夹(而不是删除文件夹本身)中删除所有文件有点复杂。德尔/ s *。不能删除文件夹,但删除所有子文件夹中的文件。因此需要两个命令:

del /q PATH_TO_FOLDER\*.*
for /d %i in (PATH_TO_FOLDER\*.*) do @rmdir /s /q "%i"

#12


0  

@ECHO OFF
rem next line removes all files in temp folder
DEL /A /F /Q /S "%temp%\*.*"
rem next line cleans up the folder's content
FOR /D %%p IN ("%temp%\*.*") DO RD "%%p" /S /Q

#13


-9  

del %pathtofolder%\*.*   /s /f  /q

delete all files and subfolders in %pathtofolder% , including read-only files, do not prompt for confirmation

删除%pathtofolder%的所有文件和子文件夹,包括只读文件,不提示确认。