【问题标题】:Deleting a folder in a directory with the same name, but its in different places inside the directory删除目录中具有相同名称但位于目录内不同位置的文件夹
【发布时间】:2015-11-24 15:41:07
【问题描述】:

所以,我有一个文件夹,该文件夹在许多不同文件夹的目录中重复出现,我希望避免对每个文件夹位置进行编码。如果可能的话,我只想从 1 行中的所有位置删除所有这些。这是我到目前为止所拥有的:

Remove-Item -Path "C:\Users\dwilson\Desktop\HTFS_v3.9_201511193\Databases" -Filter *onetimescripts* -force -Recurse -ErrorAction SilentlyContinue -Verbose

它返回的错误是:

 Remove-Item : Cannot remove item 
    C:\Users\dwilson\Desktop\HTFS_v3.9_201511193\Databases\HTFSJob\BAM\onetimescripts: 
     The directory is not empty.
At line:1 char:1
+ Remove-Item -Path "C:\Users\dwilson\Desktop\HTFS_v3.9_201511193\Databases" -Filt 
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (onetimescripts:DirectoryInfo) [Remove-It 
   em], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Comm 
   ands.RemoveItemCommand

我觉得这可能是一个非常简单的修复方法,但我找不到它。

【问题讨论】:

  • 尝试使用Get-ChildItem 而不是Remove-Item 并检查您的结果。如果它准确地找到了您要删除的内容,请再次运行该命令并将其通过管道发送到Remove-Item -Force,然后告诉我您得到了什么。
  • 我确实尝试过,在我发布问题后,get-childitem 肯定找到了我要删除的文件夹,但它告诉我“路径不存在”这是我输入的内容:@987654326 @ 并且它们确实存在。
  • 我明白了!我在remove-item 命令的末尾添加了forcerecurse,它起作用了!我之前尝试过,但它没有用......不知道为什么感谢您的帮助!
  • 将您的解决方案发布为答案并选择它作为正确答案,因此请将此问题从列表中删除。

标签: powershell delete-directory


【解决方案1】:

答案是:

Get-ChildItem -Path "C:\path" -Name "foldername" -Directory -Recurse | Remove-Item -Force -Recurse

【讨论】:

  • 这两天不让我做
  • 是的,我忘了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 2010-09-10
  • 2013-01-27
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多