Read TXT file and compare strings

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gunsslash
Posts: 1
Joined: 14 Sep 2010 09:49

Read TXT file and compare strings

#1 Post by gunsslash » 14 Sep 2010 10:11

Hi,

I am not an expert in batch but i know you guys are experts so I come to you guys for help.

I am trying to manipulate a TXT file to catch a string and compare with other one but i was not able to do it.

I have the folow TXT:

"Executable version : 5.2.4.0
DLL version : 5.2.4.1

>>> Connecting to "database"
>>> Logging on to the database as "*****"
>>> Logon successful
>>> Beginning export..


Connection result:

ResultCode = 0x00000000
ResultDescription = A operação foi concluída com êxito.


Command result:

Command = ExportMachine
ResultCode = 0xdb000004
ResultDescription = O nome não foi encontrado no banco de dados"

I Need to compare the text marked in red with another similar string and check if the two marked in red are equal to the string comparison, both must be equals to the string comparison, and if the result is negative display an error message.

Someone please can help me with this? I'll be very grateful.

Reggards,

Rafael.

aGerman
Expert
Posts: 4693
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Read TXT file and compare strings

#2 Post by aGerman » 14 Sep 2010 13:27

Lets say the name of your text file is "xyz.txt" and it contains never more than two ResultCodes:

Code: Select all

@echo off &setlocal

for /f "tokens=3" %%a in ('findstr /c:"ResultCode" "xyz.txt"') do (
  if not defined ConnectionResultCode set "ConnectionResultCode=%%a"
  if defined ConnectionResultCode set "CommandResultCode=%%a"
)

if "%ConnectionResultCode%"=="0x00000000" (
  echo Connection Result Code is 0x00000000.
) else (
  echo Connection Result Code is NOT 0x00000000.
)

if "%CommandResultCode%"=="0xdb000004" (
  echo Command Result Code is 0xdb000004.
) else (
  echo Command Result Code is NOT 0xdb000004.
)

pause


Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Read TXT file and compare strings

#3 Post by ghostmachine4 » 14 Sep 2010 19:22

download gawk for windows,

Code: Select all

C:\test>gawk -F"[ \t]*=[ \t]*" "/^ResultCode/{a[++c]=$2}END{if (a[1]!=a[2]) print \"Not equal\"}" file
Not equal


I am assuming you are comparing the 2 red colors against EACH OTHER. If not, show example of the string you are comparing the red colored code against.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Read TXT file and compare strings

#4 Post by amel27 » 16 Sep 2010 00:09

Code: Select all

@echo off

set file=1.txt
set true=ResultCode = 0x00000000

for /f %%i in ('find /i /c "%true%" ^<"%file%"') do set cnt=%%i
if %cnt% neq 2 echo some errors occured

Post Reply