Windows 中的递归删除错误
Error with recursive deletion in Windows
我目前正在卸载一些为大量文件生成数据的软件,所有 .jpg
和 .nfo
我想删除这些文件。我正在尝试使用 PowerShell 执行此操作,我尝试过的命令是:
del /S *.jpg
并收到以下结果:
Remove-Item : A positional parameter cannot be found that accepts argument '*.jpg'.
At line:1 char:1
+ del /S *.jpg
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
谢谢!
您正在寻找:
Get-ChildItem {Path to root of folder to search from} -Recurse | Where-Object (($_.FullName -like "*.jpg") -or ($_.FullName -like "*.nfo")) | Remove-Item -Force
我还没有测试这个,因为我正在使用该应用程序在我的 iPhone 上写作,所以如果它有错请告诉我,我会修复它。
tl;dr
Either: 运行 你的 cmd.exe
命令原样,在它前面加上前缀 cmd --% /c
:
cmd --% /c del /S *.jpg
--%
,停止解析符号,告诉 PowerShell 停止解析剩余参数并按原样传递它们(扩展 %<name>%
环境变量引用除外)- 请参阅 Get-Help about_Parsing
.
或:最好使用PowerShell的原生Remove-Item
cmdlet:
Remove-Item * -Recurse -Include *.jpg -WhatIf
请注意 common parameter -WhatIf
如何方便地让您在提交之前 预览 操作;参见 Get-Help Remove-Item
。
- 警告:从 PSv5.1 开始,
Remove-Item
、Copy-Item
和 Move-Item
cmdlet 以及在较小程度上 Get-ChildItem
在递归操作和 -Include
和 -Exclude
参数方面是出了名的挑剔 - 有很多微妙之处。
然而,总的来说,PowerShell 的 cmdlet 比 cmd.exe
对应的 cmdlet 强大得多,学习如何有效使用它们的任何努力都是值得的 - 尤其是因为您很快就能使用Unix 平台上的 PowerShell .
cmd.exe
命令,如 del
和 dir
内置于 cmd.exe
中,不能从 PowerShell 直接 调用;要调用它们,您必须使用 cmd /c
显式调用 cmd.exe
,如上所示。
可能令人困惑的是,PowerShell 为其 own 命令(cmdlet)定义了 aliases,这些命令以其 大致相当于cmd.exe
对等。
PowerShell cmdlet 的真实名称非常不同,遵循冗长的 very consistent naming scheme(甚至管理 PowerShell-native 别名的命名)。
例如,del
是 PowerShell 的 Remove-Item
cmdlet 的别名。
这个别名是一把双刃剑:一方面,它使习惯于 cmd.exe
(批处理编程)的人更容易发现他们的 PowerShell 副本;另一方面,它掩盖了 PowerShell 的命令行语法完全不同的事实。
要了解当您键入名称时 PowerShell 实际调用的命令,请使用 Get-Command
:
> Get-Command del
CommandType Name Version Source
----------- ---- ------- ------
Alias del -> Remove-Item
要快速了解命令的语法,请使用 -?
调用它或使用 Get-Help
:
del /?
要获得更广泛的帮助,请使用 Get-Help -detailed <cmd>
或 Get-Help -full <cmd>
; 参见 Get-Help -detailed Get-Help
。
如您所见,您也可以将 -?
传递给 Get-Help
并使用 alias 形式的命令调用 Get-Help
。
我目前正在卸载一些为大量文件生成数据的软件,所有 .jpg
和 .nfo
我想删除这些文件。我正在尝试使用 PowerShell 执行此操作,我尝试过的命令是:
del /S *.jpg
并收到以下结果:
Remove-Item : A positional parameter cannot be found that accepts argument '*.jpg'.
At line:1 char:1
+ del /S *.jpg
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
谢谢!
您正在寻找:
Get-ChildItem {Path to root of folder to search from} -Recurse | Where-Object (($_.FullName -like "*.jpg") -or ($_.FullName -like "*.nfo")) | Remove-Item -Force
我还没有测试这个,因为我正在使用该应用程序在我的 iPhone 上写作,所以如果它有错请告诉我,我会修复它。
tl;dr
Either: 运行 你的
cmd.exe
命令原样,在它前面加上前缀cmd --% /c
:
cmd --% /c del /S *.jpg
--%
,停止解析符号,告诉 PowerShell 停止解析剩余参数并按原样传递它们(扩展%<name>%
环境变量引用除外)- 请参阅Get-Help about_Parsing
.或:最好使用PowerShell的原生
Remove-Item
cmdlet:
Remove-Item * -Recurse -Include *.jpg -WhatIf
请注意 common parameter-WhatIf
如何方便地让您在提交之前 预览 操作;参见Get-Help Remove-Item
。- 警告:从 PSv5.1 开始,
Remove-Item
、Copy-Item
和Move-Item
cmdlet 以及在较小程度上Get-ChildItem
在递归操作和-Include
和-Exclude
参数方面是出了名的挑剔 - 有很多微妙之处。
然而,总的来说,PowerShell 的 cmdlet 比cmd.exe
对应的 cmdlet 强大得多,学习如何有效使用它们的任何努力都是值得的 - 尤其是因为您很快就能使用Unix 平台上的 PowerShell .
- 警告:从 PSv5.1 开始,
cmd.exe
命令,如 del
和 dir
内置于 cmd.exe
中,不能从 PowerShell 直接 调用;要调用它们,您必须使用 cmd /c
显式调用 cmd.exe
,如上所示。
可能令人困惑的是,PowerShell 为其 own 命令(cmdlet)定义了 aliases,这些命令以其 大致相当于cmd.exe
对等。
PowerShell cmdlet 的真实名称非常不同,遵循冗长的 very consistent naming scheme(甚至管理 PowerShell-native 别名的命名)。
例如,del
是 PowerShell 的 Remove-Item
cmdlet 的别名。
这个别名是一把双刃剑:一方面,它使习惯于 cmd.exe
(批处理编程)的人更容易发现他们的 PowerShell 副本;另一方面,它掩盖了 PowerShell 的命令行语法完全不同的事实。
要了解当您键入名称时 PowerShell 实际调用的命令,请使用 Get-Command
:
> Get-Command del
CommandType Name Version Source
----------- ---- ------- ------
Alias del -> Remove-Item
要快速了解命令的语法,请使用
-?
调用它或使用Get-Help
:
del /?
要获得更广泛的帮助,请使用
Get-Help -detailed <cmd>
或Get-Help -full <cmd>
; 参见Get-Help -detailed Get-Help
。
如您所见,您也可以将 -?
传递给 Get-Help
并使用 alias 形式的命令调用 Get-Help
。