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

Batch file 使用批处理脚本比较两个文件,batch-file,Batch File,我有两个文件 文件1: 进程名称 wf_1 wf_2 wf_3 文件2: wf_1-[正在运行] wf_2-[成功] wf_2-[成功] 因此,现在我需要比较上述两个文件,并且必须删除file1中的名称,file2中的状态为[successed]。在过去的三天里苦苦挣扎 结果应该是 文件1: wf_1 感谢您的帮助。给您 @echo off setlocal enabledelayedexpansion for /f "tokens=*" %%a in (file1.txt) do ( for

我有两个文件

文件1:

进程名称 wf_1

wf_2

wf_3

文件2:

wf_1-[正在运行]

wf_2-[成功]

wf_2-[成功]

因此,现在我需要比较上述两个文件,并且必须删除file1中的名称,file2中的状态为[successed]。在过去的三天里苦苦挣扎

结果应该是

文件1:

wf_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.txt
file2.txt

给你

@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
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
只需用实际文件名替换
file1.txt
file2.txt

Const ForReading=1
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
常量TextCompare=1 Dim文件1、文件2、输出文件 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”) 如果ObjFSO.FileExists(File1),则 Dim objFile1:设置objFile1=objFSO.OpenTextFile(文件1,用于读取) 其他的 WScript.Quit 如果结束 '引用文件的字典对象。 Dim RefDict:Set RefDict=CreateObject(“Scripting.Dictionary”) RefDict.CompareMode=TextCompare 暗淡的StrLine、搜索线、strNotFound '将引用文件读入字典对象。 直到objFile1.AtEndOfStream StrLine=Trim(objFile1.ReadLine) “MsgBox(StrLine) 如果不存在引用(StrLine),则 参考。添加斯特林,“1” 如果结束 环 暗淡的a、s、i a=参考键 “读字典。。。。 'For i=0 To RefDict.Count-1'迭代数组。 's=s&a(i)&“
”创建返回字符串。 ”“接着呢 objFile1.Close '文件,该文件可能有多行或少行。 如果ObjFSO.FileExists(File2),那么 Dim objFile2:设置objFile2=objFSO.OpenTextFile(文件2,用于读取) 其他的 WScript.Quit 如果结束 '使用参考文件搜索第二个文件。 直到objFile2.AtEndOfStream SearchLine=Trim(objFile2.ReadLine) 如果RefDict.不存在(搜索行),则 如果是空的(strNotFound),则 strNotFound=SearchLine 其他的 strNotFound=strNotFound&vbCrlf&SearchLine 如果结束 如果结束 环 objFile2.Close 如果IsEmpty(strNotFound)或strNotFound=“”,则 如果结束 Dim objFile3:Set objFile3=objFSO.CreateTextFile(OutputFile,True) MsgBox(“str:&strNotFound”) objFile3.WriteLine strNotFound objFile3.Close
常数ForReading=1
常量TextCompare=1
Dim文件1、文件2、输出文件
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”)
如果ObjFSO.FileExists(File1),则
Dim objFile1:设置objFile1=objFSO.OpenTextFile(文件1,用于读取)
其他的
WScript.Quit
如果结束
'引用文件的字典对象。
Dim RefDict:Set RefDict=CreateObject(“Scripting.Dictionary”)
RefDict.CompareMode=TextCompare
暗淡的StrLine、搜索线、strNotFound
'将引用文件读入字典对象。
直到objFile1.AtEndOfStream
StrLine=Trim(objFile1.ReadLine)
“MsgBox(StrLine)
如果不存在引用(StrLine),则
参考。添加斯特林,“1”
如果结束
环
暗淡的a、s、i
a=参考键
“读字典。。。。
'For i=0 To RefDict.Count-1'迭代数组。
's=s&a(i)&“
”创建返回字符串。 ”“接着呢 objFile1.Close '文件,该文件可能有多行或少行。 如果ObjFSO.FileExists(File2),那么 Dim objFile2:设置objFile2=objFSO.OpenTextFile(文件2,用于读取) 其他的 WScript.Quit 如果结束 '使用参考文件搜索第二个文件。 直到objFile2.AtEndOfStream SearchLine=Trim(objFile2.ReadLine) 如果RefDict.不存在(搜索行),则 如果是空的(strNotFound),则 strNotFound=SearchLine 其他的 strNotFound=strNotFound&vbCrlf&SearchLine 如果结束 如果结束 环 objFile2.Close 如果IsEmpty(strNotFound)或strNotFound=“”,则 如果结束 Dim objFile3:Set objFile3=objFSO.CreateTextFile(OutputFile,True) MsgBox(“str:&strNotFound”) objFile3.WriteLine strNotFound objFile3.Close
事实上,我是批处理脚本新手,如果有人提供了有帮助的示例脚本,那么要求就像我们必须使用file2删除file1中的数据,其中file2中的状态是成功的。如果
file1
中有一行在
file2
中没有任何匹配项,是否需要显示该行,什么是
过程名称
?是第一个文件的名称、标题行还是其他内容?file1中的所有数据都需要保留,除非file2中有[Successed]项,否则file1中的特定条目应该被删除。实际上,我是批处理脚本的新手,如果有人提供有帮助的示例脚本,因此,要求就像我们必须使用file2删除file1中的数据,file2中的状态是成功的。如果
file1
中有一行在
file2
中没有任何匹配项,那么需要显示该行吗?另外,什么是
进程名称
?是第一个文件的名称、标题行还是其他内容?file1中的所有数据都需要保留,除非file2中有[Successed]项,否则file1中的特定条目应该被删除