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

发布于 2024-08-16 07:20:41 字数 184 浏览 2 评论 0原文

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

我想删除此目录中的每个文件和子文件夹,但不删除目录本身。

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

有一些命令吗?

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

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.

Is there some command for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

惜醉颜 2024-08-23 07:20:41

rmdir 是我一直以来最喜欢的命令为了工作。它适用于删除大文件和带有子文件夹的文件夹。不会创建备份,因此请确保在运行此命令之前已安全复制文件。

RMDIR "FOLDERNAME" /S /Q

这会默默地删除该文件夹以及所有文件和子文件夹。

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 "FOLDERNAME" /S /Q

This silently removes the folder and all files and subfolders.

浅忆流年 2024-08-23 07:20:41

您可以使用此 shell 脚本清理 C:\Temp source 中的文件夹和文件:

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

创建包含上述命令的批处理文件(例如,delete.bat)。进入delete.bat文件所在位置,然后运行命令:delete.bat

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

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

夏九 2024-08-23 07:20:41

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

RD /S /Q folderPath

再​​次创建此目录:

MD folderPath

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

RD /S /Q folderPath

Then creating this directory again:

MD folderPath
不念旧人 2024-08-23 07:20:41

这将删除文件夹和文件并留下文件夹。

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

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

pushd "%pathtofolder%" && (rd /s /q "%pathtofolder%" 2>nul & popd)
初心 2024-08-23 07:20:41
@ECHO OFF

SET THEDIR=path-to-folder

Echo Deleting all files from %THEDIR%
DEL "%THEDIR%\*" /F /Q /A

Echo Deleting all folders from %THEDIR%
FOR /F "eol=| delims=" %%I in ('dir "%THEDIR%\*" /AD /B 2^>nul') do rd /Q /S "%THEDIR%\%%I"
@ECHO Folder deleted.

EXIT

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

@ECHO OFF

SET THEDIR=path-to-folder

Echo Deleting all files from %THEDIR%
DEL "%THEDIR%\*" /F /Q /A

Echo Deleting all folders from %THEDIR%
FOR /F "eol=| delims=" %%I in ('dir "%THEDIR%\*" /AD /B 2^>nul') do rd /Q /S "%THEDIR%\%%I"
@ECHO Folder deleted.

EXIT

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

人│生佛魔见 2024-08-23 07:20:41
CD [Your_Folder]
RMDIR /S /Q .

您将收到一条错误消息,告诉您 RMDIR 命令无法访问当前文件夹,因此无法删除它。

更新

来自这条有用的评论(感谢Moritz Both),您可以在其间添加 &&,这样如果 CD 命令失败(例如输入错误的目录名称),RMDIR 将不会运行:

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

来自 < a href="https://technet.microsoft.com/en-us/library/cc754993(v=ws.11).aspx" rel="noreferrer">Windows 命令行参考:

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

/Q: 指定安静模式。不提示确认时
删除目录树。 (请注意,仅当 /s 为
指定。)

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.

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

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

From Windows Command-Line Reference:

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

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

夜深人未静 2024-08-23 07:20:41

我使用 Powershell

Remove-Item c:\scripts\* -recurse

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

I use Powershell

Remove-Item c:\scripts\* -recurse

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

残花月 2024-08-23 07:20:41

2018 年 6 月 1 日发布的所有答案都没有,除了 foxidrive,真正删除%PathToFolder%中的所有文件和所有文件夹/目录。这就是使用非常简单的单个命令行发布另一个答案的原因,以删除文件夹的所有文件和子文件夹以及批处理文件,并使用更复杂的解决方案解释为什么 2018-06-01 上发布的所有其他答案都使用 <带有RD的strong>DEL和FOR未能完全清理文件夹。


简单的单命令行解决方案当然也可以在批处理文件中使用:

pushd "%PathToFolder%" 2>nul && ( rd /Q /S "%PathToFolder%" 2>nul & popd )

该命令行包含三个依次执行的命令。

第一个命令 PUSHD 将当前目录路径压入堆栈,接下来使 %PathToFolder% 成为运行命令进程的当前目录。

默认情况下,这也适用于 UNC 路径,因为默认情况下启用命令扩展,在本例中 < PUSHD 创建一个指向指定网络资源的临时驱动器号,然后使用新定义的驱动器号更改当前驱动器和目录。

如果指定的目录根本不存在,PUSHD 输出以下错误消息来处理 STDERR

系统找不到指定的路径。

通过使用 2>nul 将其重定向到设备 NUL,可以抑制此错误消息。

仅当将当前命令进程的当前目录更改为指定目录成功,即指定目录完全存在时,才会执行下一个命令RD

带有选项/Q/S的命令RD安静删除目录及其所有子目录< /strong> 即使指定的目录包含具有隐藏属性或设置了只读属性的文件或文件夹。系统属性永远不会阻止删除文件或文件夹。

未删除的有:

  1. 用作任何正在运行的进程的当前目录的文件夹。如果某个文件夹用作任何正在运行的进程的当前目录,则无法删除该文件夹的整个文件夹树。

  2. 当前由任何正在运行的进程打开的文件,并且在文件打开时设置了文件访问权限,以防止在由正在运行的应用程序/进程打开时删除文件。这样打开的文件还可以防止删除打开的文件的整个文件夹树。

  3. 当前用户没有删除文件/文件夹所需的 (NTFS) 权限的文件/文件夹,这也阻止了删除此文件/文件夹的文件夹树。

不删除文件夹的第一个原因是该命令行使用删除指定文件夹的所有文件和子文件夹,但不删除文件夹本身。该文件夹临时成为运行命令进程的当前目录,以防止删除文件夹本身。当然,这会导致命令 RD 输出错误消息:

该进程无法访问该文件,因为该文件正在被另一个进程使用。

文件在这里是错误的术语,因为实际上该文件夹正在被另一个进程使用,即执行命令RD的当前命令进程。嗯,实际上,文件夹对于文件系统来说是一个具有文件属性目录的特殊文件,它解释了此错误消息。但我不想太深入地了解文件系统管理。

与所有其他错误消息一样,由于上述三个原因可能会出现此错误消息,可以通过使用 2>nul 将其从句柄 STDERR 重定向到设备 <强>NUL。

第三个命令POPD 的执行与命令RD 的退出值无关。

POPDPUSHD推送的目录路径从堆栈中弹出,并将运行命令进程的当前目录更改为该目录,即恢复初始当前目录。如果是 UNC 文件夹路径,POPD 会删除 PUSHD 创建的临时驱动器号。

注意: POPD 如果初始当前目录是要清理的目录的子目录且不再存在,则可能无法恢复初始当前目录。在这种特殊情况下,%PathToFolder% 保留当前目录。因此,建议不要从 %PathToFolder% 的子目录运行上面的命令行。

还有一个有趣事实:
我尝试使用 UNC 路径使用命令行,方法是使用共享名 Temp 共享本地目录 C:\Temp 并使用 UNC 路径 \\%COMPUTERNAME%\Temp \CleanTest 分配给 Windows 7 上的环境变量 PathToFolder。如果运行命令行时的当前目录是使用 UNC 路径访问的共享本地文件夹的子目录,即 C: \Temp\CleanTest\Subfolder1Subfolder1RD 删除,并且下一个 POPD 在创建 C 时默默失败: \Temp\CleanTest\Subfolder1 再次当前目录,导致 Z:\CleanTest 保留为运行命令进程的当前目录。因此,在这种非常非常特殊的情况下,临时驱动器号将保留,直到当前目录更改为例如使用 cd /D %SystemRoot% 更改为实际存在的本地目录。不幸的是,如果 POPD 无法恢复初始当前目录,则它不会以大于 0 的值退出,因此无法仅使用 POPD 的退出代码来检测这种非常特殊的错误情况。 。但是,可以假设没有人会遇到这种非常特殊的错误情况,因为 UNC 路径通常不用于访问本地文件和文件夹。

为了更好地理解所使用的命令,请打开命令提示符窗口,执行以下命令,并仔细阅读每个命令显示的帮助。

  • pushd /?
  • popd /?
  • rd /?

使用 Windows 批处理文件的单行多个命令 解释了此处使用的运算符 &&&


接下来我们看看使用DEL命令删除%PathToFolder%FORRD 删除 %PathToFolder% 中的子文件夹。

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Clean the folder for temporary files if environment variable
rem PathToFolder is not defined already outside this batch file.
if not defined PathToFolder set "PathToFolder=%TEMP%"

rem Remove all double quotes from folder path.
set "PathToFolder=%PathToFolder:"=%"

rem Did the folder path consist only of double quotes?
if not defined PathToFolder goto EndCleanFolder

rem Remove a backslash at end of folder path.
if "%PathToFolder:~-1%" == "\" set "PathToFolder=%PathToFolder:~0,-1%"

rem Did the folder path consist only of a backslash (with one or more double quotes)?
if not defined PathToFolder goto EndCleanFolder

rem Delete all files in specified folder including files with hidden
rem or read-only attribute set, except the files currently opened by
rem a running process which prevents deletion of the file while being
rem opened by the application, or on which the current user has not
rem the required permissions to delete the file.
del /A /F /Q "%PathToFolder%\*" >nul 2>nul

rem Delete all subfolders in specified folder including those with hidden
rem attribute set recursive with all files and subfolders, except folders
rem being the current directory of any running process which prevents the
rem deletion of the folder and all folders above, folders containing a file
rem opened by the application which prevents deletion of the file and the
rem entire folder structure to this file, or on which the current user has
rem not the required permissions to delete a folder or file in folder tree
rem to delete.
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%\*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%\%%I" 2>nul

:EndCleanFolder
endlocal

批处理文件首先确保环境变量 PathToFolder 确实定义为不带双引号且末尾不带反斜杠的文件夹路径。末尾的反斜杠不会有问题,但文件夹路径中的双引号可能会出现问题,因为 PathToFolder 的值在批处理文件执行期间与其他字符串连接。

重要的是两行:

del /A /F /Q "%PathToFolder%\*" >nul 2>nul
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%\*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%\%%I" 2>nul

命令DEL用于删除指定目录中的所有文件。

  • 选项 /A 对于处理所有文件(包括具有隐藏属性的文件)是必需的,如果不使用选项 /ADEL 将忽略这些文件。
  • 要强制删除设置了只读属性的文件,需要使用 /F 选项。
  • 选项/Q对于运行多个文件的安静删除是必需的,而不提示用户是否应该真正删除多个文件。
  • >nul 是将用于处理 STDOUT 的文件名的输出重定向到设备 NUL 所必需的,该设备由于以下原因而无法删除文件当前已打开或用户无权删除该文件。
  • 2>nul 是将每个无法从句柄 STDERR 删除的文件的错误消息输出重定向到设备 NUL 所必需的。

命令FORRD用于删除指定目录中的所有子目录。但不使用 for /D ,因为 FOR 在这种情况下忽略具有隐藏属性集的子目录。因此,for /F 用于在使用 %ComSpec% /c 在后台启动的单独命令进程中运行以下命令行:

dir "%PathToFolder%\*" /AD /B 2>nul

DIR 以裸格式输出,因为 /B 目录条目具有属性 D,即指定目录中的所有子目录的名称独立于其他属性(如隐藏属性),而没有一条路径。 2>nul 用于将 DIR 在没有从句柄 STDERR 找到目录时输出的错误消息重定向到设备 NUL

重定向运算符 > 必须在 FOR 命令行上使用插入符 ^ 进行转义,以便在以下情况下将其解释为文字字符: Windows 命令解释器在执行命令 FOR 之前处理此命令行,该命令在后台启动的单独命令进程中执行嵌入的 dir 命令行。

FOR 处理捕获的输出,这些输出写入到处理已启动命令进程的 STDOUT 中,这些输出是不带路径且从不用双引号引起来的子目录的名称。

带有选项 /FFOR 会忽略此处未出现的空行,因为带有选项 /BDIR 不会出现输出空行。

FOR 也会忽略以分号开头的行,分号是默认的行结束符。目录名称可以以分号开头。因此,eol=| 用于将竖线字符定义为行尾字符,任何目录或文件的名称中都不能包含该字符。

FOR 会使用空格和水平制表符作为分隔符将行拆分为子字符串,并且仅将第一个空格/制表符分隔的字符串分配给指定的循环变量 I。此处不需要这种拆分行为,因为目录名称可以包含一个或多个空格。因此,delims= 用于定义一个空的分隔符列表,以禁用行分割行为,并分配给循环变量 I,始终是完整的目录名称。

命令 FOR 对每个不带路径的目录名称运行命令 RD,这就是为什么在 RD 命令行上必须指定文件夹路径的原因再次与子文件夹名称连接。

为了了解所使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • del /?
  • dir /
  • ? echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rd /?
  • rem /?
  • set /?
  • 设置本地/?

None of the answers as posted on 2018-06-01, with the exception of the single command line posted by foxidrive, really deletes all files and all folders/directories in %PathToFolder%. That's the reason for posting one more answer with a very simple single command line to delete all files and subfolders of a folder as well as a batch file with a more complex solution explaining why all other answers as posted on 2018-06-01 using DEL and FOR with RD failed to clean up a folder completely.


The simple single command line solution which of course can be also used in a batch file:

pushd "%PathToFolder%" 2>nul && ( rd /Q /S "%PathToFolder%" 2>nul & popd )

This command line contains three commands executed one after the other.

The first command PUSHD pushes current directory path on stack and next makes %PathToFolder% the current directory for running command process.

This works also for UNC paths by default because of command extensions are enabled by default and in this case PUSHD creates a temporary drive letter that points to that specified network resource and then changes the current drive and directory, using the newly defined drive letter.

PUSHD outputs following error message to handle STDERR if the specified directory does not exist at all:

The system cannot find the path specified.

This error message is suppressed by redirecting it with 2>nul to device NUL.

The next command RD is executed only if changing current directory for current command process to specified directory was successful, i.e. the specified directory exists at all.

The command RD with the options /Q and /S removes a directory quietly with all subdirectories even if the specified directory contains files or folders with hidden attribute or with read-only attribute set. The system attribute does never prevent deletion of a file or folder.

Not deleted are:

  1. Folders used as the current directory for any running process. The entire folder tree to such a folder cannot be deleted if a folder is used as the current directory for any running process.

  2. Files currently opened by any running process with file access permissions set on file open to prevent deletion of the file while opened by the running application/process. Such an opened file prevents also the deletion of entire folder tree to the opened file.

  3. Files/folders on which the current user has not the required (NTFS) permissions to delete the file/folder which prevents also the deletion of the folder tree to this file/folder.

The first reason for not deleting a folder is used by this command line to delete all files and subfolders of the specified folder, but not the folder itself. The folder is made temporarily the current directory for running command process which prevents the deletion of the folder itself. Of course this results in output of an error message by command RD:

The process cannot access the file because it is being used by another process.

File is the wrong term here as in reality the folder is being used by another process, the current command process which executed command RD. Well, in reality a folder is for the file system a special file with file attribute directory which explains this error message. But I don't want to go too deep into file system management.

This error message, like all other error messages, which could occur because of the three reasons written above, is suppressed by redirecting it with 2>nul from handle STDERR to device NUL.

The third command, POPD, is executed independently of the exit value of command RD.

POPD pops the directory path pushed by PUSHD from the stack and changes the current directory for running the command process to this directory, i.e. restores the initial current directory. POPD deletes the temporary drive letter created by PUSHD in case of a UNC folder path.

Note: POPD can silently fail to restore the initial current directory in case of the initial current directory was a subdirectory of the directory to clean which does not exist anymore. In this special case %PathToFolder% remains the current directory. So it is advisable to run the command line above not from a subdirectory of %PathToFolder%.

One more interesting fact:
I tried the command line also using a UNC path by sharing local directory C:\Temp with share name Temp and using UNC path \\%COMPUTERNAME%\Temp\CleanTest assigned to environment variable PathToFolder on Windows 7. If the current directory on running the command line is a subdirectory of a shared local folder accessed using UNC path, i.e. C:\Temp\CleanTest\Subfolder1, Subfolder1 is deleted by RD, and next POPD fails silently in making C:\Temp\CleanTest\Subfolder1 again the current directory resulting in Z:\CleanTest remaining as the current directory for the running command process. So in this very, very special case the temporary drive letter remains until the current directory is changed for example with cd /D %SystemRoot% to a local directory really existing. Unfortunately POPD does not exit with a value greater 0 if it fails to restore the initial current directory making it impossible to detect this very special error condition using just the exit code of POPD. However, it can be supposed that nobody ever runs into this very special error case as UNC paths are usually not used for accessing local files and folders.

For understanding the used commands even better, open a command prompt window, execute there the following commands, and read the help displayed for each command very carefully.

  • pushd /?
  • popd /?
  • rd /?

Single line with multiple commands using Windows batch file explains the operators && and & used here.


Next let us look on the batch file solution using the command DEL to delete files in %PathToFolder% and FOR and RD to delete the subfolders in %PathToFolder%.

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Clean the folder for temporary files if environment variable
rem PathToFolder is not defined already outside this batch file.
if not defined PathToFolder set "PathToFolder=%TEMP%"

rem Remove all double quotes from folder path.
set "PathToFolder=%PathToFolder:"=%"

rem Did the folder path consist only of double quotes?
if not defined PathToFolder goto EndCleanFolder

rem Remove a backslash at end of folder path.
if "%PathToFolder:~-1%" == "\" set "PathToFolder=%PathToFolder:~0,-1%"

rem Did the folder path consist only of a backslash (with one or more double quotes)?
if not defined PathToFolder goto EndCleanFolder

rem Delete all files in specified folder including files with hidden
rem or read-only attribute set, except the files currently opened by
rem a running process which prevents deletion of the file while being
rem opened by the application, or on which the current user has not
rem the required permissions to delete the file.
del /A /F /Q "%PathToFolder%\*" >nul 2>nul

rem Delete all subfolders in specified folder including those with hidden
rem attribute set recursive with all files and subfolders, except folders
rem being the current directory of any running process which prevents the
rem deletion of the folder and all folders above, folders containing a file
rem opened by the application which prevents deletion of the file and the
rem entire folder structure to this file, or on which the current user has
rem not the required permissions to delete a folder or file in folder tree
rem to delete.
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%\*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%\%%I" 2>nul

:EndCleanFolder
endlocal

The batch file first makes sure that environment variable PathToFolder is really defined with a folder path without double quotes and without a backslash at the end. The backslash at the end would not be a problem, but double quotes in a folder path could be problematic because of the value of PathToFolder is concatenated with other strings during batch file execution.

Important are the two lines:

del /A /F /Q "%PathToFolder%\*" >nul 2>nul
for /F "eol=| delims=" %%I in ('dir "%PathToFolder%\*" /AD /B 2^>nul') do rd /Q /S "%PathToFolder%\%%I" 2>nul

The command DEL is used to delete all files in the specified directory.

  • The option /A is necessary to process really all files including files with the hidden attribute which DEL would ignore without using option /A.
  • The option /F is necessary to force deletion of files with the read-only attribute set.
  • The option /Q is necessary to run a quiet deletion of multiple files without prompting the user if multiple files should be really deleted.
  • >nul is necessary to redirect the output of the file names written to handle STDOUT to device NUL of which can't be deleted because of a file is currently opened or user has no permission to delete the file.
  • 2>nul is necessary to redirect the error message output for each file which can't be deleted from handle STDERR to device NUL.

The commands FOR and RD are used to remove all subdirectories in specified directory. But for /D is not used because of FOR is ignoring in this case subdirectories with the hidden attribute set. For that reason for /F is used to run the following command line in a separate command process started in the background with %ComSpec% /c:

dir "%PathToFolder%\*" /AD /B 2>nul

DIR outputs in bare format because of /B the directory entries with attribute D, i.e. the names of all subdirectories in specified directory independent on other attributes like the hidden attribute without a path. 2>nul is used to redirect the error message output by DIR on no directory found from handle STDERR to device NUL.

The redirection operator > must be escaped with the caret character, ^, on the FOR command line to be interpreted as a literal character when the Windows command interpreter processes this command line before executing the command FOR which executes the embedded dir command line in a separate command process started in the background.

FOR processes the captured output written to handle STDOUT of a started command process which are the names of the subdirectories without path and never enclosed in double quotes.

FOR with option /F ignores empty lines which don't occur here as DIR with option /B does not output empty lines.

FOR would also ignore lines starting with a semicolon which is the default end of line character. A directory name can start with a semicolon. For that reason eol=| is used to define the vertical bar character as the end-of-line character which no directory or file can have in its name.

FOR would split up the line into substrings using space and horizontal tab as delimiters and would assign only the first space/tab delimited string to specified loop variable I. This splitting behavior is not wanted here because of a directory name can contain one or more spaces. Therefore delims= is used to define an empty list of delimiters to disable the line splitting behavior and get assigned to the loop variable, I, always the complete directory name.

Command FOR runs the command RD for each directory name without a path which is the reason why on the RD command line the folder path must be specified once again which is concatenated with the subfolder name.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?
滥情哥ㄟ 2024-08-23 07:20:41

RD 代表删除目录。

/S :删除所有文件和子文件夹
除了文件夹本身。
使用它可以删除整个文件夹树。

/Q:安静 - 不显示 YN 确认

示例:

RD /S /Q C:/folder_path/here

RD stands for REMOVE Directory.

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

/Q : Quiet - do not display YN confirmation

Example :

RD /S /Q C:/folder_path/here
情魔剑神 2024-08-23 07:20:41

使用记事本创建文本文档并复制/粘贴此文档:

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

选择另存为和文件名:

删除_temp.bat

保存类型:所有文件,然后单击保存按钮。

它适用于任何类型的帐户(管理员或标准用户)。运行吧!

我在此示例中使用临时变量,但您可以使用任何其他变量! PS:仅适用于Windows操作系统!

Use Notepad to create a text document and copy/paste this:

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

Select Save As and file name:

delete_temp.bat

Save as type: All files and click the Save button.

It works on any kind of account (administrator or a standard user). Just run it!

I use a temporary variable in this example, but you can use any other! PS: For Windows OS only!

╭⌒浅淡时光〆 2024-08-23 07:20:41

删除文件:

del PATH_TO_FILE

删除包含所有文件的文件夹:

rmdir /s /q PATH_TO_FOLDER

要删除特定文件夹中的所有文件(而不是删除文件夹本身)有点复杂。 del /s *.* 无法删除文件夹,但会删除所有子文件夹中的文件。所以需要两条命令:

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

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:

del /q PATH_TO_FOLDER\*.*
for /d %i in (PATH_TO_FOLDER\*.*) do @rmdir /s /q "%i"
柏林苍穹下 2024-08-23 07:20:41

我有以下适合我的解决方案:

for /R /D %A in (*node_modules*) do rmdir "%A" /S /Q

它从当前目录及其中删除所有节点模块文件夹子文件夹。

这与上面发布的解决方案类似,但我仍然将其发布在这里,以防万一有人发现它有用

I had following solution that worked for me:

for /R /D %A in (*node_modules*) do rmdir "%A" /S /Q

It removes all node modules folder from current directory and its sub-folders.

This is similar to solutions posted above, but i am still posting this here, just in case someone finds it useful

失退 2024-08-23 07:20:41

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

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

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

RMDIR [/S] [/Q] [drive:]path            
夜雨飘雪 2024-08-23 07:20:41
@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
@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
梦年海沫深 2024-08-23 07:20:41

我尝试了其中几种方法,但没有一个能正常工作。

我在网站 Windows 命令上找到了这种两步方法Line

forfiles /P %pathtofolder% /M * /C "cmd /c if @isdir==FALSE del @file"

forfiles /P %pathtofolder%  /M * /C "cmd /c if @isdir==TRUE rmdir /S /Q @file"

它完全按照我的需要和OP的指定工作。

I tried several of these approaches, but none worked properly.

I found this two-step approach on the site Windows Command Line:

forfiles /P %pathtofolder% /M * /C "cmd /c if @isdir==FALSE del @file"

forfiles /P %pathtofolder%  /M * /C "cmd /c if @isdir==TRUE rmdir /S /Q @file"

It worked exactly as I needed and as specified by the OP.

热风软妹 2024-08-23 07:20:41

使用:

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

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

Use:

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

This deletes all files and subfolders in %pathtofolder%, including read-only files, and does not prompt for confirmation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文