1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Windows Batch File comparison, single 'master' to around 400 altered

Discussion in 'Software' started by dynamis_dk, 7 Dec 2022.

  1. dynamis_dk

    dynamis_dk Grr... Grumpy!!

    Joined:
    23 Nov 2005
    Posts:
    3,772
    Likes Received:
    341
    I'm looking for something to do some batch file comparison. I had a master config file which I want to compare to somewhere up to around 400 files where it will contain all of the master file plus in some cases, a couple (mostly likely to be a single line or two) differences - I want to capture these so I can add into my master file.

    I don't mind doing the merge part manually as I means I can get shut of anything I no longer need to create a new 'master'

    Any suggestions for software which could make this process easy? I've found lots of title which do 2 or 3 file compare but I'd like to point to a file and say compare each one in this folder. I could likely do something in powershell to solve this one problem but if there is some software already out there, it might be good to keep in my tool belt for the future
     
  2. MLyons

    MLyons 70% Dev, 30% Doge. DevDoge. Software Dev @ Corsair Lover of bit-tech Administrator Super Moderator Moderator

    Joined:
    3 Mar 2017
    Posts:
    4,201
    Likes Received:
    2,781
    What is the file format? Just normal text? If so i could probably do it in some python
     
  3. yuusou

    yuusou Multimodder

    Joined:
    5 Nov 2006
    Posts:
    2,882
    Likes Received:
    957
    Bash, through wsl or any linux system. Or mac.

    for file in $(ls /path/to/folder/*); do diff /path/to/original.txt $file; done
     
  4. dynamis_dk

    dynamis_dk Grr... Grumpy!!

    Joined:
    23 Nov 2005
    Posts:
    3,772
    Likes Received:
    341
    Just plain text files

    I’ll give that Linux way a try, I figure there might be some Linux minds coming to help on this one, sadly my skill set in that area is pretty limited.
     
  5. RedFlames

    RedFlames ...is not a Belgian football team

    Joined:
    23 Apr 2009
    Posts:
    15,427
    Likes Received:
    3,013
    i'm no expert and this might be too jank to be useful but it also might point you in the right direction -

    powershell -

    Code:
    $prime = Get-Content C:\\path\\to\\masterfile.txt
    
    Get-ChildItem .\\*.txt | Foreach-Object {Compare-object $prime (Get-Content $_.FullName)}
    compares the .txt files in the current folder to masterfile.txt

    if the line is in both files it's ignored, if it's in the master but not the comparison file is outputs <= and if it's in the comparison but not the master file it outputs =>

    there's probably a way to filter it so it only outputs the => results but this is just me doing some quick googling whilst bored and running on fumes.
     
  6. dynamis_dk

    dynamis_dk Grr... Grumpy!!

    Joined:
    23 Nov 2005
    Posts:
    3,772
    Likes Received:
    341
    Cheers guys, I hadn't given the powershell route much of a go as I figure it would likely be more involved but having noticed diff is an alias for Compare-Object, I figured it was worth a play and have come up with something which does the trick. Pretty much what RedFlames is working towards but output a bit more info and filters the results. I'm not too bad with Powershell most of the time, think last night it was a little late for my braincells to be engaging properly as it seemed a much better task yesterday then when I took another look this morning. This is what I ended up using:

    Code:
    $InputItems = Get-ChildItem -File -Path "C:\temp\nsclient\source\"
    $SourceFile = [System.IO.File]::ReadAllLines("C:\temp\nsclient\nsclient-start.ini")
    
    ForEach ($InputItem in $InputItems){
        $Compare = $Null
        $Compare = [System.IO.File]::ReadAllLines($InputItem.Fullname)
    
        $Results += (Compare-Object $SourceFile $Compare | Where {$_.SideIndicator -eq "=>"}) | ForEach {
          $MissorDiff = $Null
          If ($SourceFile -match $_.InputObject.Split("=")[0]){
             $MissorDiff = "Diff"
          } else {
             $MissorDiff = "Missing"
          }
          [pscustomobject]@{
             SourceFile = $InputItem.Name
             Status = $MissorDiff
             Line = ($Compare | Select-String -Pattern $_.InputObject -SimpleMatch).LineNumber
             Data = $_.InputObject
          }
       }
    }
    
    $Results
    $Results | Export-CSV -NoClobber -NoTypeInformation -Path "C:\temp\nsclient\ns_updates.csv"
     
    RedFlames and MLyons like this.
  7. RedFlames

    RedFlames ...is not a Belgian football team

    Joined:
    23 Apr 2009
    Posts:
    15,427
    Likes Received:
    3,013
    My brain wasn't braining especially well, but it was 3am and i was bored... i was just confident it was possible with pwsh [and thus not needing to faff about with WSL, as handy as it can be]
     

Share This Page