本帖最后由 flashercs 于 2021-1-18 17:57 编辑
- <#*,:&cls
- @echo off
- pushd "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- ::pause
- exit /b
- #>
- # 比较两个文本
- $file1 = "gbk.txt"
- $file2 = "gbk2.txt"
- $syncWindow = 0
- $includeEqual = $false
- function New-ComparableLines {
- param (
- [string]$FilePath,
- [string]$strEncoding
- )
- $lineCtr = 0
- if ([string]::IsNullOrEmpty($strEncoding)) {
- $content = @(Get-Content -Path $FilePath)
- } else {
- $content = @(Get-Content -Path $FilePath -Encoding $strEncoding)
- }
- foreach ($item in $content) {
- $lineCtr++
- $item | Add-Member -MemberType NoteProperty -Name "Line" -Value $lineCtr
- }
- $content
- }
- $lines1 = @(New-ComparableLines -FilePath $file1)
- $lines2 = @(New-ComparableLines -FilePath $file2)
- $objComp = Compare-Object -ReferenceObject $lines1 -DifferenceObject $lines2 -SyncWindow $syncWindow -IncludeEqual:$includeEqual
- $result = @($objComp | Group-Object -Property @{
- Expression={$_.InputObject.Line}
- } | ForEach-Object {
- $pso = $_ | Select-Object -Property @{
- Name = "Line"
- Expression = { $_.Name }
- }, Reference, Difference, @{
- Name = "IsEqual"
- Expression = { $_.Group[0].SideIndicator -eq "==" }
- }
- $pso.Line = $_.Name
- foreach ($item in $_.Group) {
- switch ($item.SideIndicator) {
- "<=" { $pso.Reference = $item.InputObject }
- "=>" { $pso.Difference = $item.InputObject }
- "==" {
- $pso.Reference = $item.InputObject
- $pso.Difference = $item.InputObject
- }
- }
- }
- $pso
- })
-
- $result | Out-GridView -Title "Compare result"
- Read-Host -Prompt "按Enter退出..."
复制代码
|