Page 1 of 2 12>
Topic Options
#59416 - 2001-09-29 02:12 AM How do you extract an EXACT string from a string or text file?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
Okay, I have looked at a lot of string manipulation samples and have looked in the manual, but I appear to either be missing it or not understanding it. I know it can be done, I just can't seem to figure it out.

How can I get an EXACT string from a text file? ie.. If I shell out and use FIND, it will even come back with a partial match (don't see any switch to say exact match), If I use PV.EXE it does the same thing with a partial match.

Here is an example of what I want to do.

SHELL '%comspec% /c pv.exe -q >c:\rc.txt'

This should give me a list of all of the currently running processing on the system.
I then want to read this file and find
RTVSCAN.EXE or RTVSCN95.EXE not RTVS.EXE or RTVSCN9.EXE or anything similar to these file names.
Meaning I ONLY want to find (not case sensitive though) RTVSCAN.EXE or RTVSCN95.EXE based upon which OS it is running on. I don't want to find a partial match like these other utilities do.

If the exact file name is found then I want to take one type of action, and if it is not found then I want to a different action.

I can manage the other code pieces, I would like some help though from someone on how to read into a variable this exact match.

Please note:
I am using KiXtart v3.63 so I can not use any UDF or such from KiXtart 2001 and it needs to work on all Microsoft platforms.

Thanks in advance to anyone for help with this.

Top
#59417 - 2001-09-29 02:23 AM Re: How do you extract an EXACT string from a string or text file?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Doc,

hows it goin mate !

try something (very basic) like :

code:

$chk = ""
$ = open(1,$logfile,2)
if @error = 0
$x = readline(1)
if $x
do
select
case instr($x,"rtvscan.exe")
$chk = "NT"
case instr($x,"rtvscn95.exe")
$chk = "95"
$x = readline(1)
until @error
endif
endif

select
case $chk = "NT"
"Windows NT" ?
case $chk = "95"
"Windows 95" ?
case 1
"Wassup here ?"
endselect



Jochen

_________________________



Top
#59418 - 2001-09-29 02:43 AM Re: How do you extract an EXACT string from a string or text file?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
Hi Jochen,

Things are going pretty good. Were just having problems with our Internet connections that we have not been able to pin down yet. VERY slow past couple of days.

Thanks for the help guy. Getting an error though with the script.
Script error : expected expression !.

I assume that maybe there is a missing . " () or something like that, but not sure where as it looks okay.

Here is what I did to get the file. Your code works if the file is not there, so I assume something else is required for the parsing to be happy.

code:
$ChkNavService = '%comspec% /c pv.exe -q >C:\RC.TXT'
shell $ChkNavService
$logfile = "C:\RC.TXT"
$chk = ""
$ = open(1,$logfile,2)
if @error = 0
$x = readline(1)
if $x
do
select
case instr($x,"rtvscan.exe")
$chk = "NT"
case instr($x,"rtvscn95.exe")
$chk = "95"
$x = readline(1)
until @error
endif
endif

select
case $chk = "NT"
"Windows NT" ?
case $chk = "95"
"Windows 95" ?
case 1
"Wassup here ?"
endselect


You don't need PV.EXE to test, you can just add some line to a file called RC.TXT in the root of C: to simulate the test error.

Thanks again Jochen, I appreciate the quick response and help.

Top
#59419 - 2001-09-29 03:02 AM Re: How do you extract an EXACT string from a string or text file?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Sorry DOC,

it's a missing endselect in the loop !
I wrote it on the fly while replying and it's 2:05 am

J.

_________________________



Top
#59420 - 2001-09-29 03:27 AM Re: How do you extract an EXACT string from a string or text file?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
Got it... easy one too. Gee, now I feel bad.

Thanks again Jochen, time for you to go nighty bye now

Hopefully I can finish up the rest of the code now and start testing it on a larger scale.

Top
#59421 - 2001-09-29 03:39 AM Re: How do you extract an EXACT string from a string or text file?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Naah,

No rest for the wicked !!!

That's my normal shift time ( 23:00 - 09:00 ) for the
rest of the (Euro Conversion - ) weekend ..

J.

btw. : NICE Photo in your profile ... Is that you ?

_________________________



Top
#59422 - 2001-09-29 04:16 AM Re: How do you extract an EXACT string from a string or text file?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
No, not me I don't really like the taste of beer that much, just thought it was a cool photo.

Oh!... I added a close to the file statement. We were leaving it open.

Here is more of what I was wanting to accomplish, but not the whole thing. Hopefully next week I will be done and I'll post it then.

code:
$ChkNavService = '%comspec% /c pv.exe -q >C:\RC.TXT'
$StopNavService = 'NET STOP "Norton AntiVirus Client"'
$StartNavService = 'NET START "Norton AntiVirus Client"'

shell $ChkNavService
$logfile = "C:\RC.TXT"
$chk = ""
$ = open(1,$logfile,2)
if @error = 0
$x = readline(1)
if $x
do
select
case instr($x,"rtvscan.exe")
$chk = "Running"
case instr($x,"rtvscn95.exe")
$chk = "Running"
endselect
$x = readline(1)
until @error
endif
endif
$ = close (1)
select
case $chk = "Running"
;do nothing
case $chk = ""
shell $StartNavService
case 1
;unknown problem
endselect
IF EXIST("C:\RC.TXT")
DEL ("C:\RC.TXT")
ENDIF
RETURN


Thanks again... Oh, do you do ICQ or AOL IM? I wanted to chat with Shawn about this because our Internet is like being on a 2400 baud modem at the moment. He missed that I guess or maybe he left already.

I use magicmkrdl for AOL IM

Top
#59423 - 2001-09-29 04:29 AM Re: How do you extract an EXACT string from a string or text file?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Yeah,

the close() thingie could be a shot in the knee if the script's getting longer and calling subscripts ... been there , done that ... ok, it was On-the-fly .

ICQ , AIM ?
Not really yet, ... we use icq for internal communication here @work and I'm too lazy to expand it to internet chat ... let's see what happens in a month or so

J.

_________________________



Top
#59424 - 2001-10-01 04:20 PM Re: How do you extract an EXACT string from a string or text file?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
NTDOC,
Pardon me for jumping in, but you're confusing me. I have never encountered a case where FIND was not selective enough. If you passed a subset as the search string, then I'd understand, but for it to return a subset???

Please post the line that produces this behaviour along with the rc.txt for me to test.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#59425 - 2001-10-02 03:39 AM Re: How do you extract an EXACT string from a string or text file?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
Well, after a little more playing around, I found what I was doing wrong with FIND.

I was missing the spaces that were in the file. This code seems to work now.

code:
@ECHO OFF 
:START
SRVINFO >C:\RC.TXT
FIND /I " [Running] Norton AntiVirus Client" C:\RC.TXT
IF ERRORLEVEL = 1 GOTO BAD
IF ERRORLEVEL = 0 GOTO GOOD
GOTO DONE

:GOOD
ECHO The Service is running
GOTO DONE

:BAD
ECHO The Service is not running
GOTO DONE

:DONE


However, I am having a problem now with the code from Jochen.

If you run the code with NAV Service running you get the correct outcome. If you stop the NAV Service you get "0" and not a blank variable or a "NotRunning" variable which you would expect. I'm not sure why this is happening.

code:
; *** Variables ***
$ChkNavService = '%comspec% /c %windir%\pv.exe -q >C:\RC.TXT'
$StopNavService = 'NET STOP "Norton AntiVirus Client"'
$StartNavService = 'NET START "Norton AntiVirus Client"'

SHELL $ChkNavService
$logfile = "C:\RC.TXT"
$ = OPEN(1,$logfile,2)
IF @error = 0
$x = READLINE(1)
IF $x
DO
SELECT
CASE instr($x,"rtvscan.exe")
$CHK = "NAVNT"
CASE instr($x,"rtvscn95.exe")
$CHK = "NAV9X"
ENDSELECT
$x = READLINE(1)
UNTIL @error
ENDIF
ENDIF
$ = CLOSE (1)

SELECT
CASE $CHK = "NAVNT"
? "NAV is "$CHK
? "This should say NAVNT"
GOTO END
case $CHK = "NAV9X"
? "NAV is "$CHK
? "This should say NAV9X"
GOTO END
case 1
;unknown problem
? "NAV is "$CHK
? "This should say nothing. File entry was not found, that is why this is displaying."
GOTO END
ENDSELECT

:END


Here is what is said in the command prompt.

code:
C:\SCRIPTS>kix32 96.kix

NAV is NAVNT
This should say NAVNT
C:\SCRIPTS>net stop "Norton AntiVirus Client"
The Norton AntiVirus Client service is stopping..
The Norton AntiVirus Client service was stopped successfully.


C:\SCRIPTS>kix32 96.kix

NAV is 0
This should say NAVNT


Now back to the drawing board for FIND and PV and/or maybe if someone can explain what is going wrong in the code by Jochen.

[ 02 October 2001: Message edited by: NTDOC ]

Top
#59426 - 2001-10-02 04:07 AM Re: How do you extract an EXACT string from a string or text file?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
UPDATE...
This code works, but I would like to know what is wrong or what I'm doing wrong with the code Jochen gave me, it should work also.

code:
; *** Variables ***
$ChkNavService = '%comspec% /c %windir%\pv.exe -q >C:\RC.TXT'
$NavServiceStatus = '%comspec% /c %windir%\system32\find.exe /I "rtvscan.exe" C:\RC.TXT'
$StopNavService = 'NET STOP "Norton AntiVirus Client"'
$StartNavService = 'NET START "Norton AntiVirus Client"'

IF EXIST("C:\RC.TXT")
DEL "C:\RC.TXT"
ENDIF
SHELL $ChkNavService
SHELL $NavServiceStatus

IF @ERROR = 1 GOTO BAD
ENDIF
IF @ERROR = 0 GOTO GOOD
ENDIF
GOTO DONE

:GOOD
?"The Service is running"
GOTO DONE

:BAD
? "The Service is not running"
GOTO DONE

:DONE


This code also works

code:
IF EXIST("C:\RC.TXT")
DEL "C:\RC.TXT"
ENDIF
SHELL $ChkNavService
SHELL $NavServiceStatus

SELECT
CASE @ERROR = 0
GOTO GOOD
CASE @ERROR = 1
GOTO BAD
ENDSELECT
GOTO DONE


[ 02 October 2001: Message edited by: NTDOC ]

Top
#59427 - 2001-10-02 04:20 AM Re: How do you extract an EXACT string from a string or text file?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Hey DOC,
You must be running 3.62 'cause when I test with 3.62 I get identical results but with RC3 $chk is nul.

The interesting thing under 3.62 is if you init the variable at the beginning, as with:

$chk = ""

then it's null at the end.

If you change J's code at the end as follows:

? "NAV is " + $CHK

then it doesn't return a "0"

The real problem in J's code is with his first Select - Case group. He forgot the third possibility, that it is neither. One should always include a dummy Case, well, just in case (pardon the pun) none of the anticipated return true.

So, with Jochen's permission, I would like to amend a section of code as follows:

code:

IF @error = 0
$x = READLINE(1)
IF $x
DO
SELECT
CASE instr($x,"rtvscan.exe")
$CHK = "NAVNT"
CASE instr($x,"rtvscn95.exe")
$CHK = "NAV9X"
CASE 1
$CHK = "Neither"
ENDSELECT
$x = READLINE(1)
UNTIL @error
ENDIF
ENDIF


<edit>
I take this back. Read on...
</edit>

[ 02 October 2001: Message edited by: LLigetfa ]

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#59428 - 2001-10-02 04:20 AM Re: How do you extract an EXACT string from a string or text file?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Hey DOC, working the graveyard shift ?

Took a quick boo ... in order to get a "blank" variable, would you not have to first "initalize" $CHK to blank before you entered the do-until loop.

Since NAV isn't running, $CHK never gets "assigned" to anything, therefore it's uninitialized and equal to zero ... ?

-Shawn

[ 02 October 2001: Message edited by: Shawn ]

Top
#59429 - 2001-10-02 04:32 AM Re: How do you extract an EXACT string from a string or text file?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
scary ...
Top
#59430 - 2001-10-02 04:36 AM Re: How do you extract an EXACT string from a string or text file?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Oops! Here I go, shoot'n from the lip again! Well I was half right, sort of. It was the case of the missing Case (pun intended) by my solution, was not! The real solution is to not use Case at all, but to use only multiple If Instr() constructs instead.

It is easy to critique someone else's work. I am humbled once again.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#59431 - 2001-10-02 07:00 AM Re: How do you extract an EXACT string from a string or text file?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
Shawn and Les,

I have tried both ideas. Setting $chk to "" and to "test" at the beginning of the code.
I have added the CASE 1 and $chk = "" and "test"

Neither of these items worked. That is why I'm lost as to what is happening.

I am running v3.63 for sure and have been now for at least a year.

I have manipulated this code back and forth and this way and that way and I still get inaccurate results. I can probably make a work-around now, but I am puzzled and curious as to what is wrong with the code in the first place.

Les, what do you mean use multiple IF statements? A CASE "should" be able to do the same thing. If CASE is at fault, then is this an internal bug in KiXtart v3.63? or what is going on.

Jochen, what do you think? Any suggestions...?

MCA are you lurking out there yet back from vacation?

Top
#59432 - 2001-10-02 07:47 AM Re: How do you extract an EXACT string from a string or text file?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
NTDOC,
Sorry for the verbal diarrhea. That's what happens when you burn the midnight oil. I was trying to explain two different situations.

1. The generation of the zero where none was expected. That was resolved by defining $chk = "" or by changing the to ? "NAV is " + $CHK


2. The third Case that wasn't coded for. If neither "rtvscan.exe" nor "rtvscn95.exe" were found, there was not a Case for it. Unfortunately my sloppy coding example didn't cut it, hence the hasty rebuttal.

What I propose is as follows:

code:

Break ON

If @INWIN = 1 ;NT/2K
SHELL '%comspec% /c %windir%\pv.exe -q | find /i "rtvscan.exe"'
If @error = 0
$CHK = "NAVNT"
? "NAV is "$CHK
? "This should say NAVNT"
Else
;unknown problem
? "NAV is "$CHK
? "This should say nothing. File entry was not found, that is why this is displaying."
EndIf
EndIf
GoTo END
; Else must be 9X
SHELL '%comspec% /c %windir%\pv.exe -q | find /i "rtvscn95.exe"'
If @error = 0
$CHK = "NAV9X"
? "NAV is "$CHK
? "This should say NAV9X"
GOTO END
Else
;unknown problem
? "NAV is "$CHK
? "This should say nothing. File entry was not found, that is why this is displaying."
EndIf
:END

Get $


_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#59433 - 2001-10-03 12:16 AM Re: How do you extract an EXACT string from a string or text file?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Les,

permission granted ... back to 'find' .. much better to not open files if no need to do so ...

btw., where have y'all been over the weekend ... ?
No posts on Saturday/Sunday night (bored beyond belief ) and after a free Monday there's a blast of new topics

J.

_________________________



Top
#59434 - 2001-10-02 03:50 PM Re: How do you extract an EXACT string from a string or text file?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I thought I'd take another look at this in the sober light of dawn, and now it's starting to look like a bad dream. Yesterday, I ripped the code to my home machine and had weird results. Today at work, I can't get it to mess up. Go figure!

I have found that sometimes something weird ends up somewhere in the white space that can't be seen but trips up KiX. You can stare at the code till you're bule in the face and can't find anything wrong with it. Retype it, word for word, and it works! Well, this is my theory and I'm sticking to it!

Here's what works for me today:

code:

Break on

; *** Variables ***
$CHK = "Neither"
$ChkNavService = '%comspec% /c %windir%\pv.exe -q >C:\RC.TXT'
$StopNavService = 'NET STOP "Norton AntiVirus Client"'
$StartNavService = 'NET START "Norton AntiVirus Client"'
SHELL $ChkNavService
$logfile = "C:\RC.TXT"
$ = OPEN(1,$logfile,2)
IF @error = 0
$x = READLINE(1)
IF $x
DO
SELECT
CASE instr($x,"rtvscan.exe")
$CHK = "NAVNT"
CASE instr($x,"rtvscn95.exe")
$CHK = "NAV9X"
CASE 1
ENDSELECT
$x = READLINE(1)
UNTIL @error
ENDIF
ENDIF
$ = CLOSE (1)
SELECT
CASE $CHK = "NAVNT"
? "NAV is "$CHK
? "This should say NAVNT"
GOTO END
case $CHK = "NAV9X"
? "NAV is "$CHK
? "This should say NAV9X"
GOTO END
case 1
;unknown problem
? "NAV is "$CHK
? "This should say nothing. File entry was not found, that is why this is displaying."
GOTO END
ENDSELECT
:END

get $


p.s. I read somewhere that when using CASE, you always should have a 'true' CASE at the bottom JIC all others are false.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#59435 - 2001-10-02 04:00 PM Re: How do you extract an EXACT string from a string or text file?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
hmmm,

can't test as I have no NAV installed

For the Case :

You don't have to use case 1 , it's just useful sometimes though !

If you don't want to have have a default action , just specify explicit cases ...

Jochen

_________________________



Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2141 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.086 seconds in which 0.038 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org