Home > Software engineering >  Print to output file hash:relativePath/filename
Print to output file hash:relativePath/filename

Time:11-05

i'm trying to get file list, calculate their hashes and print it on an external file .info with powershell. The file must be like (one row example):

4bfc12adfe4842bf07b657f0369c4cb522955686:dist/lib/commons-logging-1.2.jar

The separator : is mandatory (it'll be used by a parser later). I tried different types of scripts, but without complete result.

Here i get a good file only with hash, but i can't find a way to add :relativepath\filename

Get-FileHash -Algorithm SHA1 -Path (
        Get-ChildItem "$WorkPath" -Recurse -Force | 
        ForEach-Object {$_.FullName}).Hash        | 
        Out-File -FilePath "$WorkPath\us\update.info"

here second:

Get-ChildItem "$WorkPath" -Recurse -Force | Get-FileHash -Algorithm SHA1 | Select-Object Hash, Path | Out-File -FilePath "$WorkPath\us\update.info"

I tried to remove Select-Object Hash, Path or only Hash, Path to remove title, but it prints Algorithm too... With complete command:

Hash  Path
----  ----

Withouth that part:

Algorithm    Hash    Path
---------    ----    ----

I specify that file must contains only hash:relativepath/filename, not heading or another elements. Someone can help me? Thanks in advance!

CodePudding user response:

One solution could be to fetch all files:

$Files = Get-ChildItem -Path $Dir -Recurse -Force -File

Calculate the hash for each file and add it to output file:

foreach ($File in $Files)
{
    $FileHash = Get-FileHash -Path $File.FullName -Algorithm SHA1
    Add-Content -Path $Output -Value "$($FileHash.Hash):$($File.FullName)"
}
  • Related