使用批处理脚本比较两个文件

时间:2013-01-22 08:25:28

标签: batch-file

我有两个文件

文件1:

  

PROCESS_NAME

     

wf_1

     

wf_2

     

wf_3

文件2:

  

wf_1 - [正在运行]

     

wf_2 - [成功]

     

wf_2 - [成功]

所以现在我需要比较上面两个文件,并且必须删除file1中的名称,file2中的状态为[Succeeded]。过了3天苦苦挣扎。

结果应为

文件1:

  

wf_1

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你去吧

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (file1.txt) do (
for /f "skip=1 tokens=*" %%b in ('find "[Succeeded]" file2.txt') do (
set check=%%b
set check=!check: - [Succeeded]=!
if "%%a"=="!check!" set bool=true
)
if not "!bool!"=="true" echo %%a >>new.txt
)
del file1.txt /f /q
ren new.txt file1.txt

只需将file1.txtfile2.txt替换为您的实际文件名。

答案 1 :(得分:0)

Const ForReading = 1
Const TextCompare = 1

Dim File1, File2, OutputFile

File1 = "D:\1t\test1\ddir11.txt"
File2 = "D:\1t\test1\ddir12.txt"
OutputFile = "D:\1t\test1\outfile.txt"

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
If ObjFSO.FileExists(File1) Then
  Dim objFile1 : Set objFile1 = objFSO.OpenTextFile(File1, ForReading)
Else
  WScript.Quit
End If

' Dictionary object for reference file.
Dim RefDict : Set RefDict = CreateObject("Scripting.Dictionary")
RefDict.CompareMode = TextCompare

Dim StrLine, SearchLine, strNotFound

' Read reference file into dictionary object.
Do Until objFile1.AtEndOfStream
  StrLine = Trim(objFile1.ReadLine)
  'MsgBox (StrLine)
  if Not RefDict.Exists(StrLine) Then
    RefDict.Add StrLine, "1"
  End If
Loop

Dim a,s,i
a = RefDict.Keys
'read dictionary....
'For i = 0 To RefDict.Count -1 ' Iterate the array.
'   s = s & a(i) & "<BR>" ' Create return string.
'Next
objFile1.Close

' File that may have more or less lines.
If ObjFSO.FileExists(File2) Then
  Dim objFile2 : Set objFile2 = objFSO.OpenTextFile(File2, ForReading)
Else
  WScript.Quit
End If

' Search 2nd file with reference file.
Do Until objFile2.AtEndOfStream
  SearchLine = Trim(objFile2.ReadLine)
  If Not RefDict.Exists(SearchLine) Then
    If IsEmpty(strNotFound) Then
      strNotFound = SearchLine
    Else
      strNotFound = strNotFound & vbCrlf & SearchLine
    End If
  End If
Loop

objFile2.Close

If IsEmpty(strNotFound) or strNotFound = "" Then

End If

Dim objFile3 : Set objFile3 = objFSO.CreateTextFile(OutputFile, True)
MsgBox ("str:" & strNotFound)
objFile3.WriteLine strNotFound
objFile3.Close