【问题标题】:Powershell script to check if all files from a list of files exist in given folder folderPowershell脚本检查给定文件夹中是否存在文件列表中的所有文件
【发布时间】:2016-11-24 18:17:25
【问题描述】:

我需要编写一个 powershell 脚本,输入文件名列表并检查给定的文件夹。如果列表中的所有文件都存在于文件夹中(如果不存在),我需要它来回显,反之亦然 - 是输入的文件名列表中列出的文件夹中的所有文件。

我是 powershell 新手,刚刚完成了我的第一个脚本来重命名文件夹中的所有文件,我不知道如何输入列表并在检查文件夹中的文件名时遍历它。

我已经设法写了这样的东西:

$Dir2 = "C:\Users\Administrator\Desktop\testDir2"
$filenames = 'a.txt', 'b.txt', 'c.txt', 'd.txt'
foreach ($filename in $filenames) {
$found=$false; 
Get-ChildItem -Path $Dir2 -Recurse | ForEach-Object {if($filename -eq $_.Name) {Write-Host $filename ' Ok' -foregroundcolor green; $found=$true;CONTINUE }$found=$false;} -END {if($found -ne $true){ Write-Host $filename ' missing' -foregroundcolor red}}
}

我仍然需要检查其他方式 + 我需要以某种方式将行从 excel 表转换为文件名列表

【问题讨论】:

    标签: arrays powershell directory filenames


    【解决方案1】:

    要从文本文件中检索列表,请使用 [Get-Content cmdlet]:

    $FileList = Get-Content -Path .\myFileList.txt
    

    要检索文件夹中的文件,请使用Get-ChildItem cmdlet

    $Files = Get-ChildItem -Path C:\path\to\folder -File
    

    Select-Object获取文件名:

    $Files = $Files |Select-Object -Property Name
    

    最后用Compare-Object比较两个列表:

    $Discrepancies = @(Compare-Object $FileList $Files)
    

    如果Compare-Object 没有返回任何内容,那么这两个列表之间就没有区别:

    if($Discrepancies.Count -eq 0)
    {
        Write-Host "Everything is as expected!"
    }
    

    【讨论】:

    • hmm Compare-Object 没有返回任何内容,但我的测试列表和测试文件夹中应该存在差异
    • @AdamWróblewski 使用Compare-Object $FileList $Files -IncludeEqual 查看哪些比较匹配,可能会给您提示
    【解决方案2】:

    好的,我有适合我需要的代码: (文件列表在一个文件中给出,它可以是 csv)

    $Dir2 = 'C:\Users\Administrator\Desktop\testDir2'
    $filenames=Get-Content $Dir2\filenamesnoext.csv
    foreach ($filename in $filenames) {
    $found=$false; 
    Get-ChildItem -Path $Dir2 -Recurse | ForEach-Object {if($filename -eq $_.BaseName) {Write-Host 'FILE ' $filename ' Ok' -foregroundcolor green; $found=$true;CONTINUE }$found=$false;} -END {if($found -ne $true){ Write-Host 'FILE ' $filename ' missing in the folder' -foregroundcolor red}}
    }
    Get-ChildItem -Path $Dir2 -Recurse | ForEach-Object  {$found=$false; foreach ($filename in $filenames) {if($filename -eq $_.BaseName) {Write-Host 'FILE ' $_.BaseName ' was found on the list' -foregroundcolor cyan; $found=$true;BREAK }} if($found -ne $true){ Write-Host 'FILE ' $_.BaseName ' missing on the list of files' -foregroundcolor Magenta} }
    

    【讨论】:

      【解决方案3】:

      如果有人觉得它更易读,这里是另一个版本(没有跳过循环迭代,只获取一次文件名):

          $folder = 'D:\stuff'
          $files = @(
              "one.txt",
              "two.txt"
          )
          Write-Host "Folder: $folder."
          # Get only files and only their names
          $folderFiles = Get-ChildItem -Path $folder -Recurse -File -Name
          foreach ($f in $files) {
              if ($folderFiles -contains $f) { 
                  Write-Host "File $f was found." -foregroundcolor green
              } else { 
                  Write-Host "File $f was not found!" -foregroundcolor red 
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-08
        • 1970-01-01
        • 2018-04-21
        • 2012-02-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多