“rm-rf” Windows 的等效项?

发布于 2024-07-06 04:17:52 字数 154 浏览 11 评论 0原文

我需要一种方法来递归删除文件夹及其子文件夹。

有没有为此预先构建的工具,或者我需要编写一个吗?

DEL /S 不会删除目录。

DELTREE 已从 Windows 2000+ 中删除

I need a way to recursively delete a folder and its children.

Is there a prebuilt tool for this, or do I need to write one?

DEL /S doesn't delete directories.

DELTREE was removed from Windows 2000+

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

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

发布评论

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

评论(24

亣腦蒛氧 2024-07-13 04:17:55

如果您使用的是旧版本的 Windows,还有 deltree。

您可以从这里了解更多信息:
SS64:DELTREE - 删除所有子文件夹和文件。

There is also deltree if you're on an older version of windows.

You can learn more about it from here:
SS64: DELTREE - Delete all subfolders and files.

梦纸 2024-07-13 04:17:55

Windows 注册表编辑器版本 5.00

[HKEY_CLASSES_ROOT\Folder\shell\rmdir\command]
@="cmd.exe /s /c rmdir "%V""

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\rmdir\command]
@="cmd.exe /s /c rmdir "%V""

£噩梦荏苒 2024-07-13 04:17:55

这对我有用:

尝试减少路径的长度。
ie :: 将指向此类文件的所有文件夹重命名为尽可能小的名称。 说出一个字母的名字。 继续在文件夹层次结构中向上重命名。
这样可以有效地减少路径长度。
现在最后尝试立即删除该文件。

here is what worked for me:

Just try decreasing the length of the path.
i.e :: Rename all folders that lead to such a file to smallest possible names. Say one letter names. Go on renaming upwards in the folder hierarchy.
By this u effectively reduce the path length.
Now finally try deleting the file straight away.

花伊自在美 2024-07-13 04:17:55

使用 Powershell 5.1

 get-childitem *logs* -path .\ -directory -recurse | remove-item -confirm:$false -recurse -force

logs 替换为要删除的目录名称。

get-childitem 从当前路径 (.) 递归搜索具有该名称的子目录。

remove-item 删除结果。

Using Powershell 5.1

 get-childitem *logs* -path .\ -directory -recurse | remove-item -confirm:$false -recurse -force

Replace logs with the directory name you want to delete.

get-childitem searches for the children directory with the name recursively from current path (.).

remove-item deletes the result.

迷爱 2024-07-13 04:17:55

使用风险由您自行承担。 信息按“原样”提供。 未经过广泛测试。

右键单击 Windows 图标(通常是左下角)> 单击“Windows PowerShell(管理员)”> 使用此命令(请务必小心,如果不小心,您可以轻松删除所有文件):

rd -r -include *.* -force somedir

其中 somedir 是您要删除的非空目录。

请注意,对于外部连接的磁盘或有问题的磁盘,Windows 有时会表现得很奇怪 - 删除(或任何复制尝试)时不会出错,但目录不会按照指示删除(或未复制)。 (我发现在这种情况下,至少对我来说,@n_y 在他的答案中给出的命令会产生类似“get-childitem:文件或目录已损坏且无法读取。”的错误,从而在 PowerShell 中产生错误)

USE AT YOUR OWN RISK. INFORMATION PROVIDED 'AS IS'. NOT TESTED EXTENSIVELY.

Right-click Windows icon (usually bottom left) > click "Windows PowerShell (Admin)" > use this command (with due care, you can easily delete all your files if you're not careful):

rd -r -include *.* -force somedir

Where somedir is the non-empty directory you want to remove.

Note that with external attached disks, or disks with issues, Windows sometimes behaves odd - it does not error in the delete (or any copy attempt), yet the directory is not deleted (or not copied) as instructed. (I found that in this case, at least for me, the command given by @n_y in his answer will produce errors like 'get-childitem : The file or directory is corrupted and unreadable.' as a result in PowerShell)

少女七分熟 2024-07-13 04:17:55

您可以安装 GnuWin32 并在 Windows 上本机使用 *nix 命令。 我在 Windows 的薄荷味新副本上安装其他任何东西之前先安装这个。 :)

You can install GnuWin32 and use *nix commands natively on windows. I install this before I install anything else on a minty fresh copy of windows. :)

少女情怀诗 2024-07-13 04:17:55

在 powershell 中,rm -recurse -force 工作得很好。

In powershell rm -recurse -force works quite well.

初见你 2024-07-13 04:17:54

对于在 Windows 计算机上安装 npm 软件包时遇到问题的人,迟到但重要的答案如果您看到错误提示“rm -rf...”命令未找到。
您可以使用 bash cli 在 Windows 上运行 rm 命令。

对于 npm 用户,如果您尝试安装 npm 软件包,则可以将 npm 的配置更改为 npm config set script-shell "C:\Program Files\Git\bin\bash.exe"有一个使用 rm -rf 命令的安装后脚本,您将能够运行该 rm 命令,而无需更改 npm 包中的任何内容或禁用安装后脚本配置。 (例如,styled-components 在安装后脚本中使用 rm 命令)

如果您只想使用 rm 命令,您可以轻松地使用使用 bash 并传递参数。

所以,是的,您可以在 Windows 上使用“rm”命令。

LATE BUT IMPORTANT ANSWER to anyone who is having troubles installing npm packages on windows machine and if you are seeing error saying "rm -rf..." command not found.
You can use the bash cli to run rm command on windows.

for npm users, you can change the npm's config to npm config set script-shell "C:\Program Files\Git\bin\bash.exe" this way if the npm package you are trying to install has a post install script that uses rm -rf command, you will be able to run that rm command without needing to change anything in the npm package or disabling the post install scripts config. (For example, styled-components uses rm command in their post install scripts)

If you want to just use the rm command, you can easily use the bash and pass the arguments.

So yes, you can use the 'rm' command on windows.

§对你不离不弃 2024-07-13 04:17:53

通过 Powershell

 Remove-Item -Recurse -Force "TestDirectory"

通过命令提示符

https://stackoverflow.com/a/35731786/439130

via Powershell

 Remove-Item -Recurse -Force "TestDirectory"

via Command Prompt

https://stackoverflow.com/a/35731786/439130

酒浓于脸红 2024-07-13 04:17:53

接受的答案很好,但假设您安装了 Node,您可以使用节点库“rimraf”更精确地完成此操作,它允许通配模式。 如果您经常使用它(我经常使用),只需全局安装它即可。

yarn global add rimraf

然后,例如,我经常使用的一种模式:

rimraf .\**\node_modules

或者用于单行,让您避开全局安装,但包动态下载需要稍长的时间:

npx rimraf .\**\node_modules

The accepted answer is great, but assuming you have Node installed, you can do this much more precisely with the node library "rimraf", which allows globbing patterns. If you use this a lot (I do), just install it globally.

yarn global add rimraf

then, for instance, a pattern I use constantly:

rimraf .\**\node_modules

or for a one-liner that let's you dodge the global install, but which takes slightly longer for the the package dynamic download:

npx rimraf .\**\node_modules
尽揽少女心 2024-07-13 04:17:53

要删除目录(无论是否存在),请使用以下命令:

if exist myfolder ( rmdir /s/q myfolder )

For deleting a directory (whether or not it exists) use the following:

if exist myfolder ( rmdir /s/q myfolder )
峩卟喜欢 2024-07-13 04:17:53

尝试这个命令:

del /s foldername

Try this command:

del /s foldername
北音执念 2024-07-13 04:17:53

首先,让我们回顾一下 rm -rf 的作用:

C:\Users\ohnob\things>touch stuff.txt

C:\Users\ohnob\things>rm -rf stuff.txt

C:\Users\ohnob\things>mkdir stuff.txt

C:\Users\ohnob\things>rm -rf stuff.txt

C:\Users\ohnob\things>ls -l
total 0

C:\Users\ohnob\things>rm -rf stuff.txt

在三种情况下,通常会使用 rm -rf 并期望返回 0

  1. 指定的路径不存在。
  2. 指定的路径存在并且是一个目录。
  3. 指定的路径存在并且是一个文件。

我将忽略整个权限问题,但无论如何没有人使用权限或试图拒绝自己对 Windows 中的内容进行写访问(好吧,这只是一个笑话......)。

首先 ERRORLEVEL 设置为 0,然后仅删除路径(如果存在),使用不同的命令,具体取决于是否是一个目录。 如果路径不存在,IF EXIST 不会将 ERRORLEVEL 设置为 0,因此必须首先将 ERRORLEVEL 设置为 0,才能正确检测成功一种模仿正常 rm -rf 用法的方式。 使用 IF EXIST 保护 RD 是必要的,因为 RDrm -f 不同,如果目标不存在。

以下脚本片段假定 DELPATH 已加引号。 (当您执行诸如 SET DELPATH=%1 之类的操作时,这是安全的。尝试将 ECHO %1 放入 .cmd 中并向其传递一个参数里面有空格,看看你自己会发生什么)。 代码片段完成后,您可以使用 IF ERRORLEVEL 1 检查是否失败。

: # Determine whether we need to invoke DEL or RD or do nothing.
SET DELPATH_DELMETHOD=RD
PUSHD %DELPATH% 2>NUL
IF ERRORLEVEL 1 (SET DELPATH_DELMETHOD=DEL) ELSE (POPD)
IF NOT EXIST %DELPATH% SET DELPATH_DELMETHOD=NOOP
: # Reset ERRORLEVEL so that the last command which
: # otherwise set it does not cause us to falsely detect
: # failure.
CMD /C EXIT 0
IF %DELPATH_DELMETHOD%==DEL DEL /Q %DELPATH%
IF %DELPATH_DELMETHOD%==RD RD /S /Q %DELPATH%

重点是,当环境符合 POSIX 时,一切都会变得更简单。 或者,如果您安装一个最小的 MSYS 并使用它。

First, let’s review what rm -rf does:

C:\Users\ohnob\things>touch stuff.txt

C:\Users\ohnob\things>rm -rf stuff.txt

C:\Users\ohnob\things>mkdir stuff.txt

C:\Users\ohnob\things>rm -rf stuff.txt

C:\Users\ohnob\things>ls -l
total 0

C:\Users\ohnob\things>rm -rf stuff.txt

There are three scenarios where rm -rf is commonly used where it is expected to return 0:

  1. The specified path does not exist.
  2. The specified path exists and is a directory.
  3. The specified path exists and is a file.

I’m going to ignore the whole permissions thing, but nobody uses permissions or tries to deny themselves write access on things in Windows anyways (OK, that’s meant to be a joke…).

First set ERRORLEVEL to 0 and then delete the path only if it exists, using different commands depending on whether or not it is a directory. IF EXIST does not set ERRORLEVEL to 0 if the path does not exist, so setting the ERRORLEVEL to 0 first is necessary to properly detect success in a way that mimics normal rm -rf usage. Guarding the RD with IF EXIST is necessary because RD, unlike rm -f, will throw an error if the target does not exist.

The following script snippet assumes that DELPATH is prequoted. (This is safe when you do something like SET DELPATH=%1. Try putting ECHO %1 in a .cmd and passing it an argument with spaces in it and see what happens for yourself). After the snippet completes, you can check for failure with IF ERRORLEVEL 1.

: # Determine whether we need to invoke DEL or RD or do nothing.
SET DELPATH_DELMETHOD=RD
PUSHD %DELPATH% 2>NUL
IF ERRORLEVEL 1 (SET DELPATH_DELMETHOD=DEL) ELSE (POPD)
IF NOT EXIST %DELPATH% SET DELPATH_DELMETHOD=NOOP
: # Reset ERRORLEVEL so that the last command which
: # otherwise set it does not cause us to falsely detect
: # failure.
CMD /C EXIT 0
IF %DELPATH_DELMETHOD%==DEL DEL /Q %DELPATH%
IF %DELPATH_DELMETHOD%==RD RD /S /Q %DELPATH%

Point is, everything is simpler when the environment just conforms to POSIX. Or if you install a minimal MSYS and just use that.

南城旧梦 2024-07-13 04:17:53

rmdir /S /Q %DIRNAME%

rmdir /S /Q %DIRNAME%

明月松间行 2024-07-13 04:17:53

rmdir /s 目录名

rmdir /s dirname

江南月 2024-07-13 04:17:53

这是您需要做的...

使用以下行创建一个批处理文件

RMDIR /S %1

将批处理文件保存为 Remove.bat 并将其放入 < code>C:\windows

创建以下注册表项

HKEY_CLASSES_ROOT\Directory\shell\Remove Directory (RMDIR)

启动regedit 并更新默认值HKEY_CLASSES_ROOT\Directory\shell\删除目录 (RMDIR)\default
具有以下值

"c:\windows\REMOVE.bat" "%1"

就是这样! 现在您可以右键单击任何目录并使用 RMDIR 功能

Here is what you need to do...

Create a batch file with the following line

RMDIR /S %1

Save your batch file as Remove.bat and put it in C:\windows

Create the following registry key

HKEY_CLASSES_ROOT\Directory\shell\Remove Directory (RMDIR)

Launch regedit and update the default value HKEY_CLASSES_ROOT\Directory\shell\Remove Directory (RMDIR)\default
with the following value

"c:\windows\REMOVE.bat" "%1"

Thats it! Now you can right click any directory and use the RMDIR function

玩物 2024-07-13 04:17:53

在powershell中,rm是Remove-Item的别名,所以删除一个文件,

rm -R -Fo the_file

相当于

Remove-Item -R -Fo the_file

如果你对gnu rm util感到满意,你可以rm< /code> util,由 Windows 上的 choco 包管理器提供。

使用 choco 在 powershell 中安装 gnu utils:

choco install GnuWin

最后,

rm.exe -rf the_file

in powershell, rm is alias of Remove-Item, so remove a file,

rm -R -Fo the_file

is equivalent to

Remove-Item -R -Fo the_file

if you feel comfortable with gnu rm util, you can the rm util by choco package manager on windows.

install gnu utils in powershell using choco:

choco install GnuWin

finally,

rm.exe -rf the_file
笑着哭最痛 2024-07-13 04:17:53

作为旁节点:

从包含所有子目录的 linux 版本(递归)+强制删除

$ rm -rf ./path

到 PowerShell

PS> rm -r -fo ./path

具有接近相同的参数(只是分开)(需要 -fo,因为 -f 可以匹配不同的其他参数)

注意:

Remove-Item ALIASE
    ri
    rm
    rmdir
    del
    erase
    rd

As a sidenode:

From the linux version with all subdirs (recursive) + force delete

$ rm -rf ./path

to PowerShell

PS> rm -r -fo ./path

which has the close to same params (just seperated) (-fo is needed, since -f could match different other params)

note:

Remove-Item ALIASE
    ri
    rm
    rmdir
    del
    erase
    rd
记忆之渊 2024-07-13 04:17:52

RMDIR 或 RD 如果您使用经典命令提示符 (cmd.exe):

rd /s /q "path"

RMDIR [/S] [/Q] [驱动器:]路径

RD [/S] [/Q] [驱动器:]路径

/S 删除指定目录中除目录本身之外的所有目录和文件。 用于删除目录树。

/Q 安静模式,不询问是否可以使用 /S 删除目录树

如果您使用的是 PowerShell,则可以使用 Remove-Item (其别名为 deleraserdrirmrmdir) 并采用 -Recurse 参数,该参数可以缩写为 -r

rd -r "path"

RMDIR or RD if you are using the classic Command Prompt (cmd.exe):

rd /s /q "path"

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

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

/S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S

If you are using PowerShell you can use Remove-Item (which is aliased to del, erase, rd, ri, rm and rmdir) and takes a -Recurse argument that can be shorted to -r

rd -r "path"
雪化雨蝶 2024-07-13 04:17:52
rm -r -fo <path>

是您可以在 Windows PowerShell 中获得的最接近的结果。 的缩写。

Remove-Item -Recurse -Force -Path <path>

它是(更多详细信息)

rm -r -fo <path>

is the closest you can get in Windows PowerShell. It is the abbreviation of

Remove-Item -Recurse -Force -Path <path>

(more details).

箹锭⒈辈孓 2024-07-13 04:17:52

你可以安装cygwin,它有rm以及ls等。

You can install cygwin, which has rm as well as ls etc.

太傻旳人生 2024-07-13 04:17:52

转到该路径并触发此命令。

rd /s /q "FOLDER_NAME"

/s :删除指定的目录和所有子目录,包括任何文件。 使用 /s 删除一棵树。

/q :以安静模式运行 rmdir。 删除目录而不确认。

/? :在命令提示符处显示帮助。

Go to the path and trigger this command.

rd /s /q "FOLDER_NAME"

/s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree.

/q : Runs rmdir in quiet mode. Deletes directories without confirmation.

/? : Displays help at the command prompt.

小清晰的声音 2024-07-13 04:17:52

RMDIR [/S] [/Q] [驱动器:]路径

RD [/S] [/Q] [驱动器: ]path

  • /S 删除指定目录中除目录本身之外的所有目录和文件。 用于删除目录树。

  • /Q 安静模式,不询问是否可以使用 删除目录树/S

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

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

  • /S Removes all directories and files in the specified directory in addition to the directory itself. Used to remove a directory tree.

  • /Q Quiet mode, do not ask if ok to remove a directory tree with /S

濫情▎り 2024-07-13 04:17:52

管理:

takeown /r /f folder
cacls folder /c /G "ADMINNAME":F /T
rmdir /s folder

适用于任何内容,包括系统文件

编辑:我实际上找到了也解决文件路径太长问题的最佳方法:

mkdir \empty
robocopy /mir \empty folder

admin:

takeown /r /f folder
cacls folder /c /G "ADMINNAME":F /T
rmdir /s folder

Works for anything including sys files

EDIT: I actually found the best way which also solves file path too long problem as well:

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