【问题标题】:How to delete files and then directory in Powershell?如何在Powershell中删除文件然后删除目录?
【发布时间】:2021-02-19 06:03:00
【问题描述】:

我正在努力寻找一种方法来处理这样的任务:

有几个目录名称不同。它们包含 2 个文件 hi.exe 和 newScript.bat 我需要编写脚本,在当前目录及其后代中查找包含这两个文件的文件夹,删除它们,然后删除包含目录。

我找到了删除这些文件的方法以及如何删除空目录。有没有办法在删除这些文件之前链接目录名称?还是其他方式?

我找到了一种删除文件的方法,但我需要以某种方式保存它们的路径,然后删除一个文件夹: Remove-Item -path H:* -include hi.exe , newScript.bat -recurse -whatif

我也可以删除空文件夹,但这不是我正在寻找的解决方案。

更新: 为什么使用 - 并且它不起作用?但它适用于 -or? powershell execution

【问题讨论】:

  • 请张贴您的代码......以及什么不能按预期工作。为什么? lookee ... 游览 - 堆栈溢出 — stackoverflow.com/tour

标签: powershell


【解决方案1】:

如果你走这条路,那就是一个开始。

Get-ChildItem -Path C:\ -Recurse | Where-Object {$_.Name -eq "hi.exe" -or $_.Name -eq "newScript.bat" } | foreach { $_.Directory }

您可以删除它们所在的目录,而不是将目录名称发送到控制台。

编辑:

由于我错过了“包含两个文件”,我建议这样做。随意将它写成单行,但我是一步一步地写的,所以路上会有“检查点”。

$potensial_files = Get-ChildItem -Path C:\ -File -Recurse | Where-Object { @("calc.exe", "cmd.exe").Contains($_.Name) }

$potensial_files.Directory.FullName | select –unique | foreach { 

    $dir = $_
    $files = $potensial_files | Where-Object { $_.Directory.FullName -eq $dir }
    if(($files | Measure-Object).Count -eq 2)
    {
        Write-Host "$dir contains $files"
    }
}

而且我确信这可以写得更短,但是“运行一次”脚本的意义何在?

【讨论】:

  • 我可以使用 -and 吗?还是没有这种东西?我需要找到一个包含两个文件的文件夹
  • 是的,并且存在,但它不会给你你想要的东西,因为它找到的文件被一个接一个地传送到 where-object。我认为您需要调查每个文件夹。
  • Get-ChildItem -Path H:\ -Recurse | Where-Object {($_.Name -eq "hi.exe") -and ($_.Name -eq "newScript.bat") } | foreach { rm -Force $_.Directory } 不应该这样吗?
  • 不需要括号
  • 对不起,错过了“两个都在”。我认为你需要一步一步地做到这一点。识别存在的所有目录,并计算/测量目录的全名。这里有两个相等的目录名(FullName)我假设你可以放心删除。
【解决方案2】:

如果我理解正确,您需要找到找到两个特定文件的文件夹,如果是这种情况,请输出路径以便将其写入文件,然后删除文件夹,包括其文件和子目录,对吗?

# this is where you log the folder fullnames that have been deleted
$outFile = 'D:\Test\RemovedItems.log'
$removed = (Get-ChildItem -Path 'D:\Test' -Directory -Recurse).FullName | 
           Sort-Object -Property Length -Descending |   # sort descending is needed, see *
           ForEach-Object {
                # get a list of the files in each directory
                $files = (Get-ChildItem -Path $_ -File).Name
                # only if if BOTH files are present
                if ($files -contains 'hi.exe' -and $files -contains 'newscript.bat') {
                    # output the path of the directory before you delete the folder in order to save these
                    $_
                    # now remove the folder and its files
                    $_ | Remove-Item -Recurse -Force -WhatIf
                }
            }

$removed | Set-Content -Path $outFile -PassThru

* -Recurse 开关在 Remove-Item 上不能很好地工作(它会在文件夹中的所有子项都被删除之前尝试删除文件夹)。 按长度降序对全名进行排序可确保在删除文件夹中的所有子项之前不会删除任何文件夹。

  1. -WhatIf 开关只向控制台输出会发生什么。还没有真正删除任何内容。如果您对该输出感到满意,请移除该开关。
  2. 最后的 -PassThru 开关还将写入 RemovedItems.log 文件的所有内容写入控制台

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2017-06-25
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    相关资源
    最近更新 更多