[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 flashercs 于 2021-1-18 17:57 编辑
  1. <#*,:&cls
  2. @echo off
  3. pushd "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. popd
  6. ::pause
  7. exit /b
  8. #>
  9. # 比较两个文本
  10. $file1 = "gbk.txt"
  11. $file2 = "gbk2.txt"
  12. $syncWindow = 0
  13. $includeEqual = $false
  14. function New-ComparableLines {
  15.   param (
  16.     [string]$FilePath,
  17.     [string]$strEncoding
  18.   )
  19.   $lineCtr = 0
  20.   if ([string]::IsNullOrEmpty($strEncoding)) {
  21.     $content = @(Get-Content -Path $FilePath)
  22.   } else {
  23.     $content = @(Get-Content -Path $FilePath -Encoding $strEncoding)
  24.   }
  25.   foreach ($item in $content) {
  26.     $lineCtr++
  27.     $item | Add-Member -MemberType NoteProperty -Name "Line" -Value $lineCtr
  28.   }
  29.   $content
  30. }
  31. $lines1 = @(New-ComparableLines -FilePath $file1)
  32. $lines2 = @(New-ComparableLines -FilePath $file2)
  33. $objComp = Compare-Object -ReferenceObject $lines1 -DifferenceObject $lines2 -SyncWindow $syncWindow -IncludeEqual:$includeEqual
  34. $result = @($objComp | Group-Object -Property @{
  35.   Expression={$_.InputObject.Line}
  36. } | ForEach-Object {
  37.     $pso = $_ | Select-Object -Property @{
  38.       Name       = "Line"
  39.       Expression = { $_.Name }
  40.     }, Reference, Difference, @{
  41.       Name       = "IsEqual"
  42.       Expression = { $_.Group[0].SideIndicator -eq "==" }
  43.     }
  44.     $pso.Line = $_.Name
  45.     foreach ($item in $_.Group) {
  46.       switch ($item.SideIndicator) {
  47.         "<=" { $pso.Reference = $item.InputObject }
  48.         "=>" { $pso.Difference = $item.InputObject }
  49.         "==" {
  50.           $pso.Reference = $item.InputObject
  51.           $pso.Difference = $item.InputObject
  52.         }
  53.       }
  54.     }
  55.     $pso
  56.   })
  57. $result | Out-GridView -Title "Compare result"
  58. Read-Host -Prompt "按Enter退出..."
复制代码
微信:flashercs
QQ:49908356

TOP

返回列表