当前位置: 首页 > news >正文

PowerShell 脚本 比较两文件差异(带粗狂进度条)并汇总输出

一上来就放代码

function Compare-FileHex {param ([Parameter(Mandatory=$true)][string]$SourceFile,[Parameter(Mandatory=$true)][string]$CompareFile,[Parameter(Mandatory=$false)][string]$OutputFile,[Parameter(Mandatory=$false)][int]$BufferSize = 1MB)function Format-FileSize {param ([long]$Size)if ($Size -ge 1GB) { "{0:N2} GB" -f ($Size / 1GB) }elseif ($Size -ge 1MB) { "{0:N2} MB" -f ($Size / 1MB) }elseif ($Size -ge 1KB) { "{0:N2} KB" -f ($Size / 1KB) }else { "$Size Bytes" }}$sourceStream = [System.IO.File]::OpenRead($SourceFile)$compareStream = [System.IO.File]::OpenRead($CompareFile)$results = New-Object System.Collections.Generic.List[PSCustomObject]$buffer1 = New-Object byte[] $BufferSize$buffer2 = New-Object byte[] $BufferSize$totalLength = [Math]::Max($sourceStream.Length, $compareStream.Length)$position = 0$sourceSize = $sourceStream.Length$compareSize = $compareStream.Lengthtry {while ($position -lt $totalLength) {$read1 = $sourceStream.Read($buffer1, 0, $BufferSize)$read2 = $compareStream.Read($buffer2, 0, $BufferSize)$maxRead = [Math]::Max($read1, $read2)for ($i = 0; $i -lt $maxRead; $i++) {$sourceByte = if ($i -lt $read1) { $buffer1[$i] } else { $null }$compareByte = if ($i -lt $read2) { $buffer2[$i] } else { $null }if ($sourceByte -ne $compareByte) {$results.Add([PSCustomObject]@{Address = $position + $iSourceFile = if ($sourceByte -ne $null) { "{0:X2}" -f $sourceByte } else { "--" }CompareFile = if ($compareByte -ne $null) { "{0:X2}" -f $compareByte } else { "--" }})}}$position += $maxRead$percentComplete = [math]::Round(($position / $totalLength) * 100, 2)Write-Progress -Activity "比较文件" -Status "$percentComplete% 完成" -PercentComplete $percentComplete}}finally {$sourceStream.Close()$compareStream.Close()}Write-Progress -Activity "比较文件" -Completed$sourceSizeFormatted = Format-FileSize -Size $sourceSize$compareSizeFormatted = Format-FileSize -Size $compareSize$summary = @"
源文件:$SourceFile
大小: $sourceSizeFormatted比较文件:$CompareFile
大小: $compareSizeFormatted不同之处: $($results.Count) 字节
详细如下:"@$detailedOutput = "地址       源文件    比较文件`n"$detailedOutput += "--------------------------`n"$lastAddress = -1foreach ($result in $results) {if ($lastAddress -ne -1 -and $result.Address -ne $lastAddress + 1) {$detailedOutput += "`n"  # 添加空行表示不连续}$detailedOutput += "0x{0:X8}  {1}        {2}`n" -f $result.Address, $result.SourceFile, $result.CompareFile$lastAddress = $result.Address}$fullOutput = $summary + $detailedOutputif ($OutputFile) {$fullOutput | Out-File -FilePath $OutputFile -Encoding utf8Write-Host "结果已保存到: $OutputFile"}Write-Host $fullOutput
}

2、调用

        假设您将函数保存在名为 Compare-FileHex.ps1 的文件中。在您想要使用这个函数的其他 PowerShell 脚本中,您可以这样导入和使用它:

  1.  导入 Compare-FileHex 函数 . "C:\Path\To\Compare-FileHex.ps1"

  2.  现在您可以使用这个函数了 Compare-FileHex -SourceFile "path\to\source\file" -CompareFile "path\to\compare\file" -OutputFile "path\to\output.txt"

 3、效果 (gif 随便看了。渣画质)

分别采用两组参数 (不带生成txt与带生成txt的 f:\play.txt)
4、 生成的txt内容

源文件:D:\Program Files\Sublime Text\sublime_text.exe.old
大小: 7.74 MB

比较文件:D:\Program Files\Sublime Text\sublime_text.exe
大小: 7.74 MB

不同之处: 6 字节
详细如下:
地址       源文件    比较文件
--------------------------
0x0001A4E8  80        C6
0x0001A4E9  79        41

0x0001A4EB  00        01
0x0001A4EC  0F        B2
0x0001A4ED  94        00
0x0001A4EE  C2        90

5、结束语

        自用编制注册机时候的帮助工具,不用装什么软件工具。大文件,大差异没试验过,加了缓存参数,有兴趣自己试试吧,全源码公开。

http://www.lryc.cn/news/464939.html

相关文章:

  • 学习 UE5 的一些前置操作总结
  • C#/.NET/.NET Core技术前沿周刊 | 第 10 期(2024年10.14-10.20)
  • Git 基本配置
  • 理工科考研想考计算机,湖南大学、重大、哈工大威海、山东大学,该如何选择?
  • 使用langchain和大模型API提取QA的实战教程
  • Java面试场景题(1)---如何使用redis记录上亿用户连续登陆天数
  • Element UI
  • ②PROFINET转ModbusTCP, EtherCAT/Ethernet/IP/Profinet/ModbusTCP协议互转工业串口网关
  • python+Mosh网课笔记04
  • 【微服务】全面构建微服务监控体系:确保系统稳定与性能优化的关键
  • Gin框架操作指南08:日志与安全
  • 鸿蒙系统 VS 安卓系统,谁将引领未来移动操作系统?
  • PyTorch 中 functional.py 文件介绍
  • SQL Injection | SQL 注入 —— 报错盲注
  • 网络通信与并发编程(四)操作系统、进程理论、开启进程的两种方式
  • Java--集合(三)之vectorlinkedlisthashset结构
  • upload-labs Pass-04
  • 如何修改jupyter notebook的工作目录
  • 23种设计模式具体实现方法
  • cisco网络安全技术第3章测试及考试
  • 数据结构练习题5(链表和栈)
  • 计算机网络408真题解析(湖科大教书匠)
  • uniapp+vue3+uview-plus修改默认样式
  • 数控机械制造工厂ERP适用范围有哪些
  • 华为配置 之 Console线路配置
  • 小米等手机彻底关闭快应用
  • 【每日一题】24.10.14 - 24.10.20
  • CMake与Qt4/Qt5的结合使用指南
  • TwinCAT3添加PLC轴,并建立PLC轴与NC轴的链接
  • Linux操作系统如何制作U盘启动盘