[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理比较两个文本的内容

按行进行比较,如果有不同打印出不同位置的行号
和内容,批处理应该怎么写?

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=1,* delims=:" %%i in ('type 1.txt ^| findstr /n ".*"') do (
  4. set var1=%%i
  5. set str1=%%j
  6. for /f "tokens=1,* delims=:" %%a in ('type 2.txt ^| findstr /n ".*"') do (
  7. set var2=%%a
  8. set str2=%%b
  9. if "!var1!" equ "!var2!" (
  10. if "!str1!" neq "!str2!" (
  11. echo 1.txt中的 第!var1!行---!str1! 与2.txt中的 第!var2!行---!str2! 不同>>temp.log))
  12. )
  13. )
复制代码
自己改改就可以了

TOP

本帖最后由 qixiaobin0715 于 2021-1-18 16:22 编辑
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /a n=0,m=0
  4. for /f "delims=" %%a in (a.txt) do (
  5. set /a n+=1&set "_!n!=%%a"
  6. )
  7. for /f "delims=" %%i in (b.txt) do (
  8. set /a m+=1
  9. for %%x in (_!m!) do if not "%%i"=="!%%x!" echo !m!  a.txt:!%%x! b.txt:%%i&&set k=false
  10. )
  11. if not defined k echo 两个文本相同
  12. pause
复制代码

TOP

回复 3# qixiaobin0715

文件要是完全相同能否给个提示,两个文件完全相等。谢谢!

TOP

本帖最后由 netdzb 于 2021-1-18 16:05 编辑

回复 3# qixiaobin0715


    set "_!n!=%%a" 表示什么含义,出了括号变量不是过期了吗?
代码没有完全看懂啊。

第一个for是一个一重循环,第二个for是两重循环的意思吗?

TOP

回复 4# netdzb
这样行吗

TOP

本帖最后由 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

回复 5# netdzb
你在哪儿看到的

TOP

回复 5# netdzb
运行下面代码试试:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /l %%a in (1,1,3) do (
  4. set /a n+=1
  5. set str!n!=%%a
  6. )
  7. echo !str1!#!str2!#!str3!
  8. pause
复制代码

TOP

返回列表