1

I've two files

file 1:

PROCESS_NAME

wf_1

wf_2

wf_3

file 2:

wf_1 - [Running]

wf_2 - [Succeeded]

wf_2 - [Succeeded]

So now I need to compare the above two files and have to remove the names in file1 for which the status in file2 is [Succeeded]. Struggling for hte past 3 days.

Result should be

file 1:

wf_1

Appreciate any help.

4
  • actually I am new to batch scripting, if any one provide sample script that would be helpful, so the requirement is like we've to use file2 to remove data in file1 for which the status in file2 is Succeeded Commented Jan 22, 2013 at 11:59
  • If there was a line in file1 without any match in file2, would that line need to be displayed? Commented Jan 22, 2013 at 12:42
  • Also, what is PROCESS_NAME? Is it the first file's name or a header line or something else? Commented Jan 22, 2013 at 12:43
  • All the data in file1 needs to retain, except when there is [Succeeded] in file2 that particular entry in file1 should be eliminated Commented Jan 23, 2013 at 4:43

2 Answers 2

1

Here you go

@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

Just replace file1.txt and file2.txt with your actual file names.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.