[Help] Script for file hash comparison.

Discussion in 'Scripting' started by SalviaSage, Apr 17, 2018.

Tags:
  1. SalviaSage

    SalviaSage MDL Novice

    Apr 9, 2018
    30
    1
    0
    Hi, this is my second post after my introductory post in the introduction sub-channel.

    I like to check if two or more different files are the same files by comparing their hashes.

    I want to be able to check two or more files to see if they are the same by selecting these files and then calling a .bat script or a .vbs script from the context menu or by another means which would then print file hashes for the selected files AND make a comparison between these hash values to see if they are the same. For example:

    --------------------------------------------------
    file1.txt - CRC32 Hash Value : 1B0ECF0B
    file2.txt - CRC32 Hash Value : 1B0ECF0B

    The files are the SAME.
    --------------------------------------------------

    I like the CRC32 hashing which I used here for the example, but it can be other ones too.
     
  2. netlords

    netlords MDL Novice

    Jun 20, 2012
    35
    6
    0
    Hi

    what about powershell?

    Actually I´m doing this, and it works well

    Code:
    Clear-Host ; $ErrorActionPreference = "SilentlyContinue"
    $source_path="C:\source" ; $target_path="C:\target" #ADJUST HERE
    $source_path=$source_path.ToLower() ; $source_hash="$source_path\filehash.csv"
    $target_path=$target_path.ToLower() ; $target_hash="$target_path\filehash.csv"
    
    Get-ChildItem $source_path -File -Recurse -Exclude filehash.csv | Get-FileHash -Algorithm MD5 | select Path, Hash | Export-csv $source_hash -NoTypeInformation
    Get-ChildItem $target_path -File -Recurse -Exclude *.csv | Get-FileHash -Algorithm MD5 | select Path, Hash | Export-csv $target_hash -NoTypeInformation
    
    $source_file = import-csv -Path $source_hash
    $target_file = import-csv -Path $target_hash
    
    Compare-Object $source_file $target_file -property Hash -PassThru | where SideIndicator -EQ "<=" | select Path -Unique | Export-Csv $target_path\diff.csv -NoTypeInformation
    (Get-Content $target_path\diff.csv).ToLower() | Select -Skip 1 | %{$_ -Replace '"',""} | Out-File $target_path\diff.csv
    
    If ((Get-Content "$target_path\diff.csv") -eq $Null) {
        Write-Host -ForegroundColor Green -BackgroundColor Black "No differences detected - Thats OK"
        Remove-Item $target_path\*.csv ; Break 0
    } else {
        Write-Host -ForegroundColor Red -BackgroundColor Black "Differences found - now copying"
        foreach ($diff in Get-Content $target_path\diff.csv) {
            $diff2=Split-Path $diff
            $diff2=$diff2.replace($source_path,$target_path) + "\"
            & xcopy /H /R /F /I /Y /C $diff $diff2
        Remove-Item $target_path\*.csv
    }}
    Clear-Host ; Write-Host -ForegroundColor Green -BackgroundColor Black "Syncronised succsessful"
    
     
  3. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    37
    0
  4. SalviaSage

    SalviaSage MDL Novice

    Apr 9, 2018
    30
    1
    0
    But, just so you know. I already know how to check the hash of the file.

    What I want to be able to do is compare two or more files by selecting them, and quickly checking if their hash is the same.

    Can your script do this?
     
  5. NormieLyfe

    NormieLyfe MDL Novice

    May 1, 2017
    37
    37
    0
    The HashShell can give you an option to claculate a file's hash (MD2,4,5 SHA1-256,512) in the right click menu
     
  6. SalviaSage

    SalviaSage MDL Novice

    Apr 9, 2018
    30
    1
    0
    Sure, I can check the hashes of files.

    But, what I really want to do is to simply select two or more different files, and then press a hotkey on my keyboard in order to learn if two files are the same or not.

    I often need to check if two files are identical. Currently I do this manually by hashing each file and comparing the two myself.

    I just think the computer can easily do this for me and tell me if the files are identical or not.

    Does anyone know how to do this?

    Thanks.
     
  7. pf100

    pf100 Duct Tape Coder

    Oct 22, 2010
    2,069
    3,447
    90
    fc /b "path to\file1" "path to\file2"
    Nevermind, This won't work as a hotkey.
     
  8. tnexim

    tnexim MDL Novice

    Jul 21, 2018
    3
    0
    0
    I use Multihasher to check and compare the file hash against clipboard entry or hash digests which you can also upload to virustotal. There's also a context menu for quick check.

    Are you looking for a powershell script which runs Get-FileHash and use Match (-eq) to compare the file hashes or something which runs a diff on the relevant strings?

    That'd be the way I'd approach it. If else statements ;)
     
  9. SalviaSage

    SalviaSage MDL Novice

    Apr 9, 2018
    30
    1
    0
    tnexim, This is a pretty good program. It does everything except it doesn't tell me if the selected files have the same hash or not...
    It could easily also do that...

    So I still have to manually compare after hashing the files with my eyes...
     
  10. tnexim

    tnexim MDL Novice

    Jul 21, 2018
    3
    0
    0
    You have to right-click on the file and select "Compare digests". Copy-Paste or drag and drop the hash digest to compare all the hashes against files in the same folder.
    If the hash matches MultiHasher will display a green check mark. If it does not match it will display a message and a cross sign.
     
  11. SalviaSage

    SalviaSage MDL Novice

    Apr 9, 2018
    30
    1
    0
    I clicked on compare digests and it is asking me to enter in some other hash... this kills the purpose of any kind of automation...
    I think this program should be able to automate this, I don't know why it doesn't?
     
  12. tnexim

    tnexim MDL Novice

    Jul 21, 2018
    3
    0
    0
    #12 tnexim, Jul 27, 2018
    Last edited: Jul 27, 2018
    Found references to PS scripts to find duplicate files based on around group-object and get length,hash

    DuplicateFiles Deleter is also recommended. It uses CRC32 checksum and file length to detect duplicates.
     
  13. SalviaSage

    SalviaSage MDL Novice

    Apr 9, 2018
    30
    1
    0
    hmm, I wonder if that can be adapted to do what want to do... one button press and I know if 2 or more files are the same,
    without having to manually check the hashes with eyes.
     
  14. SalviaSage

    SalviaSage MDL Novice

    Apr 9, 2018
    30
    1
    0
    powershell looks promising... finally we may be making some progress here.
    I will try to adapt some of the example codes in powershell.