#59416 - 2001-09-29 02:12 AM
How do you extract an EXACT string from a string or text file?
|
NTDOC
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
|
|
|
|
#59418 - 2001-09-29 02:43 AM
Re: How do you extract an EXACT string from a string or text file?
|
NTDOC
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 endifselect 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
|
|
|
|
#59425 - 2001-10-02 03:39 AM
Re: How do you extract an EXACT string from a string or text file?
|
NTDOC
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.kixNAV 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
   
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 $NavServiceStatusSELECT 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
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
|
|
|
|
#59429 - 2001-10-02 04:32 AM
Re: How do you extract an EXACT string from a string or text file?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
scary ...
|
Top
|
|
|
|
#59431 - 2001-10-02 07:00 AM
Re: How do you extract an EXACT string from a string or text file?
|
NTDOC
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
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 ONIf @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
|
|
|
|
#59434 - 2001-10-02 03:50 PM
Re: How do you extract an EXACT string from a string or text file?
|
Les
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
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 2141 anonymous users online.
|
|
|