locked
Read a column from a file and compare it. RRS feed

  • Question

  • Hi everyone!

      I need to read a column from a file and compare to a column on a second file:


      param([string]$path=$(Throw "Parameter missing: -name Path"))
    
      Get-ChildItem -Path $path -File -Recurse | Get-FileHash -Algorithm MD5 | Select-Object -Property Hash,Path | Export-Csv -Path .\tempfile1.csv -NoTypeInformation
      Get-ChildItem -Path $path -File -Recurse | Get-FileHash -Algorithm MD5 | Select-Object -Property Hash,Path | Group-Object -Property Hash -NoElement | Where-Object {$_.count -gt 1} | Sort-Object -Property Count -Descending | Export-Csv -Path .\tempfile2.csv -NoTypeInformation
    
      $file1=Get-Content -Path .\tempfile1.csv 
      $file2=Get-Content -Path .\tempfile2.csv
    
      foreach($line1 in $file1)
      {
        foreach($line2 in $file2)
        {
      
          # Write-Host $line1.Hash " " $line2.Name
     
          if($line1.Hash -eq $line2.Name)
          {
             # Work here
             Write-Host $line1.Path
          }
        }
        Write-Host ------- -ForegroundColor Yellow
        Write-Host ""
      }

     

      Perhaps, the output shows me the following:


    -------



    -------



    -------


      May someone help me how to identity correctly a column from a file?

    Thanks.


    Doria




    • Edited by dydoria Monday, February 18, 2019 4:10 PM
    Monday, February 18, 2019 2:53 PM

Answers

  • try import-csv if you have a delimiter in your file


    Chris

    • Marked as answer by dydoria Tuesday, February 19, 2019 1:38 PM
    Monday, February 18, 2019 4:49 PM
  • Instead of "Get-Content" you should be using "Import-CSV". There's no "$line1.hash" or "$line2.name" in a plain text file. Those "hash", "path", and "line" are column headers in the CSV files you exported.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Monday, February 18, 2019 7:41 PM

All replies

  • Please only post code that is correctly formatted and use the code posting tool provided on the edit bar.

    Please edit your post and fix.


    \_(ツ)_/

    Monday, February 18, 2019 3:47 PM
  • try import-csv if you have a delimiter in your file


    Chris

    • Marked as answer by dydoria Tuesday, February 19, 2019 1:38 PM
    Monday, February 18, 2019 4:49 PM
  • Instead of "Get-Content" you should be using "Import-CSV". There's no "$line1.hash" or "$line2.name" in a plain text file. Those "hash", "path", and "line" are column headers in the CSV files you exported.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Monday, February 18, 2019 7:41 PM
  • Hello,

    Excel is awesome and has built in tools for this, give a look at VLookup 

    SCott 

    Monday, February 18, 2019 11:33 PM
  • Thanks all!

      $file1=Import-Csv -Path .\tempfile1.csv -Header "Hash","Path"
      $file2=Import-Csv -Path .\tempfile2.csv -Header "Values","Count","Group","Name"
    
       

      Import-Csv Cmdlet did the job!

    Thanks again.


    Doria

    Tuesday, February 19, 2019 1:38 PM
  • PowerShell is much bigger than Excel SCott, thanks.


    Doria

    Tuesday, February 19, 2019 1:39 PM