ArchAngel96
(Getting the hang of it)
2004-01-16 11:10 PM
Spliting a String

Ok, I've looked at the board and didn't find my issue, of course there are many ways to describe it, so I'll just do that and hope some one might know where to point me.

I need to strip a string and verify it meets a certain criteria....


Code:
 $node = "L1A3456789"
$sAsset=SubStr($node,2,6)
Select
Case $sAsset >= "000001" AND $sAsset <= "999999"
? $sAsset
EndSelect


I want to make sure the first 6 'characters' are numbers only. Since there is no separator I can't use the split command so I am uncertain how to get this completed. Can someone help?


Sealeopard
(KiX Master)
2004-01-16 11:17 PM
Re: Spliting a String

Code:

IF $sAsset=val($sAsset)
? $sAsset
ENDIF



ArchAngel96
(Getting the hang of it)
2004-01-16 11:20 PM
Re: Spliting a String

I totally over looked that... thanx

Howard Bullock
(KiX Supporter)
2004-01-16 11:58 PM
Re: Spliting a String

Given your example, I do not think that VAL is a suitable solution.

$node = "L1A3456789"
$sAsset=SubStr($node,2,6)

yields "1A3456" which is not 6 consecutive number and yields a value of only 1 (one).

My recommendation would be to use regular expressions, but first you need to clarify your example and your data strings.

See: RFC: Regular Expression UDFs

The code below accepts your input and return the first characters that match 6 consecutive digits.

Result: Match = 345678

Code:
$node = "L1A3456789"

$RegExPattern = '(\d{6})'

$Matches = RegExpFind($RegExPattern, $node, 1, 0)
? "Match = " + $Matches[0][0]

Function RegExpFind($Pattern, $String, $IgnoreCase, $Global)

Dim $regEx, $Match, $Matches, $i, $j, $k ; Create variable.
Dim $Results[0]
$regEx = createobject("VBscript.RegExp") ; Create a regular expression.

$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.
$regEx.Global = val($Global) ; Set global applicability.

$Matches = $regEx.Execute($String) ; Execute search.

$k=0
For Each $Match in $Matches ; Iterate Matches collection.
Dim $MatchValue[0]
ReDim Preserve $Results[$k]

$MatchValue[0] = $Match.Value

if $Match.Submatches.count > 0
for $i=0 to $Match.Submatches.count
ReDim Preserve $MatchValue[$i+1]
$MatchValue[$i+1] = $Match.Submatches($i)
next
endif
$Results[$k] = $MatchValue
$k=$k+1
Next
$RegExpFind = $Results
EndFunction



LonkeroAdministrator
(KiX Master Guru)
2004-01-17 04:52 PM
Re: Spliting a String

hoby, I thought he just wanted to check if it is all numbers or not.
with val returning 1 in this case, he knows that it ain't pure numbers only.


ArchAngel96
(Getting the hang of it)
2004-01-17 05:34 PM
Re: Spliting a String

Here's the scenario, all corp pcs are named (W/L)123456(asset tag). The asset tag is a unique number and therefore duplicates are virtually impossible, if the techs do their jobs correctly. We want the logon script to run on pc's that meet a very specific criteria, in this case to check if the first letter is a w or l and then verify that there are 6 numbers following the w/l. Somewhere along the line we have gotten a 7th number or an a or b following the normal naming convention. So the name must meet the criteria before the logo script continues. This is to help prevent non-company machines to be affected by things we’ve placed in the logon script.

Howard Bullock
(KiX Supporter)
2004-01-17 06:33 PM
Re: Spliting a String

From your last post I would then change the RegEx pattern to "^(W|L)(\d{6})" to match

Quote:

to check if the first letter is a w or l and then verify that there are 6 numbers following the w/l.




If you have other patterns that need to be found, I would be happy in assisting you with the pattern.


Since you are not wanting to extract the pieces of the string like your first post seemed to indicate, I would suggest using a different UDF to just verify that match exists.

Various patterns:
"^(W|L)(\d{6})" matches beginning of string; could have other characters after the 6th digit.
"^(W|L)(\d{6})$" matches explicit 7 character string. "^" beginning of string; "$" end of string.


Code:
break ON

$nodes = "L1A3456789", "L123456789"
$RegExPattern = "^(W|L)(\d{6})"

for each $node in $nodes
if RegExpTest($RegExPattern, $node, 1)
? "-Found match: " + $node
else
? "-No Match found: " + $node
endif
next

Function RegExpTest($Pattern, $String, $IgnoreCase)

Dim $regEx, $Match, $Matches ; Create variable.

$regEx = createobject("VBscript.RegExp") ; Create a regular expression.

$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.

$RegExpTest = $regEx.Test($String) ; Execute test search.
EndFunction



LonkeroAdministrator
(KiX Master Guru)
2004-01-18 01:05 AM
Re: Spliting a String

I must wonder...
do you allow non-company machines to be part of your domain?
and even set their logonscript the same as company machines?

that is so weird...


Les
(KiX Master)
2004-01-18 04:02 AM
Re: Spliting a String

Weird... unless they are Wintendos or you setup trusts or they join the domain, non-company machine cannot logon.

ArchAngel96
(Getting the hang of it)
2004-01-18 07:21 PM
Re: Spliting a String

We do allow other workstations on the domain, mainly employees that do work from home on occasion, but not often enough to issue a laptop. We also have venders that do work regularly within the office, they are normally renamed and joined to the domain. In the past everyone had the same style logon script regardless of ‘owner’, there was some minor name checking but many of the non-company machines were still getting the script.

Machine types that are allowed:
Sxxx(Y)xx
(W/L)xxxxxx

Possible Ys: S, N, K, E, H, M, R, L
All ‘x’: Numbers

What I have so far (still working things out):
Code:
$sType=SubStr(@WKSTA,1,1)

Select
Case $sType = "S"
$sLocation=SubStr(@WKSTA,2,4)
$sFunction=SubStr(@WKSTA,6,1)
$sNode=SubStr(@WKSTA,7,2)
Select
Case $sLocation >= "0002" AND $sLocation <= "0999"
Select
Case $sFunction >= "A" AND $sFunction <= "z"
Select
Case $sNode >= "01" AND $sNode <= "99"
$computername = "Store" ; One of the above
EndSelect
EndSelect
EndSelect
Case $sType = "W" OR $sType = "L"
$sAsset=SubStr(@WKSTA,2,6)
Select
Case $sAsset=Val($sAsset)
If $sAsset >= "000001" AND $sAsset <= "999999"
$computername = "Corp"
EndIf
EndSelect
EndSelect

Return
Exit



I’m just trying to clean this up a little more being specific of the node names. I still am testing so the ‘A – Z’ is about to be changed next, I was just using that for testing to ensure I was in the ‘right’ path.


Howard Bullock
(KiX Supporter)
2004-01-18 08:07 PM
Re: Spliting a String

Just wondering... Have you read my posts? You seem to be doing it the hard way.

ArchAngel96
(Getting the hang of it)
2004-01-18 08:25 PM
Re: Spliting a String

I have been reading your posts, it would appear you've created a patterned matching UDF based on perl's pattern matching. I didn't want to use a UDF because I'm the main person who knows kix and would be the only person capable of understanding the concepts. Most of our other scripts are in perl, which just about everyone in my dept knows. We don't use perl for the logon script because we add new features regularly, not every pc has perl so on and on...
I was trying to keep the kix scripts as simple as possible to allow anyone who looks at it to understand. The rest of the knuckleheads seem to be fearful of kix, therefore I became primary. Because of their fears I have to keep things as simple as possible, incase I'm gone and someone needs to make changes.


Howard Bullock
(KiX Supporter)
2004-01-19 01:56 AM
Re: Spliting a String

The script uses the VBS regular expression engine (WSH 5.5 or better) via COM.

ArchAngel96
(Getting the hang of it)
2004-01-19 05:54 AM
Re: Spliting a String

I do appreciate all the help, thou it may not seem like it by not using your examples or so forth.

But you're assuming these workstations have WHS, especially 5.6... If it requires WSH 5.6, then that would mean someone needs to install it on the target computers, correct? Now Win98 should have ver 1, Win2K should have ver 2. NT 4.0 and Win95 requires someone to manually install WSH of any version. Now if this is all true, I know none of the target machines have 5.6 and a little less than half have any version of WSH installed. That would mean I can't use your example because my target workstations do not meet the requirements.

I didn’t attempt to use your example simply because I have to keep in mind there are other people who, at the very least need to be able to look at the script and understand it without having to spend time examining it. I have to admit Kix is a very simple and yet powerful, but none of us are programmers, especially anywhere close your level. So I have to work within my limitations, right or wrong.



Howard Bullock
(KiX Supporter)
2004-01-19 07:01 AM
Re: Spliting a String

I can understand your position and the issues you have now that you mentioned the older OS's.


ArchAngel96
(Getting the hang of it)
2004-01-19 11:24 PM
Re: Spliting a String

Ok, I think I've got the concept down, but its pretty ugly to look at. There's a couple of way I could go to 'clean' it up a bit, a sub or a function. I don't think the function will work correctly, the sub will most likely work better, but I'd like few opinions first. Keep in mind I'm covering all the OS's starting with Win95 to Win2K. WSH has not been installed unless the OS has it built in. The possible workstation names are "SxxxxYxx" or "(W/L)xxxxxx".

Code:
 $machine = "W094562a154"
$computername = "Failed"

$sType=SubStr($machine,1,1)

Select
Case $sType = "S"
$sLocation=SubStr($machine,2,4)
$sFunction=SubStr($machine,6,1)
$sNode=SubStr($machine,7,2)
$sLocation2=Val($sLocation)
Select
Case Len($sLocation2) = 1
$sLocation3 = SubStr($sLocation,4)
If $sLocation2=$sLocation3
Select
Case $sLocation >= "0002" AND $sLocation <= "0999"
Select
Case $sFunction = "N" OR $sFunction = "L" OR $sFunction = "R" OR $sFunction = "H"
OR $sFunction = "E" OR $sFunction = "K" OR $sFunction = "S" OR $sFunction = "V"
Select
Case $sNode >= "01" AND $sNode <= "99"
$computername = "Store"
EndSelect
EndSelect
EndSelect
EndIf
Case Len($sLocation2) = 2
$sLocation3 = SubStr($sLocation,3)
If $sLocation2=$sLocation3
Select
Case $sLocation >= "0002" AND $sLocation <= "0999"
Select
Case $sFunction = "N" OR $sFunction = "L" OR $sFunction = "R" OR $sFunction = "H"
OR $sFunction = "E" OR $sFunction = "K" OR $sFunction = "S" OR $sFunction = "V"
Select
Case $sNode >= "01" AND $sNode <= "99"
$computername = "Store"
EndSelect
EndSelect
EndSelect
EndIf
Case Len($sLocation2) = 3
$sLocation3 = SubStr($sLocation,2)
If $sLocation2=$sLocation3
Select
Case $sLocation >= "0002" AND $sLocation <= "0999"
Select
Case $sFunction = "N" OR $sFunction = "L" OR $sFunction = "R" OR $sFunction = "H"
OR $sFunction = "E" OR $sFunction = "K" OR $sFunction = "S" OR $sFunction = "V"
Select
Case $sNode >= "01" AND $sNode <= "99"
$computername = "Store"
EndSelect
EndSelect
EndSelect
EndIf
EndSelect
Case $sType = "W" OR $sType = "L"
$sAsset=SubStr($machine,2,6)
$sAsset2=Val($sAsset)
If Len($sAsset2) < 6
$sAsset = SubStr($machine,3,5)
EndIf
Select
Case $sAsset=$sAsset2
If $sAsset >= "000001" AND $sAsset <= "999999"
$computername = "Corp"
EndIf
EndSelect
EndSelect

? $computername

Any one?


Sealeopard
(KiX Master)
2004-01-19 11:52 PM
Re: Spliting a String

This
Code:

Case $sFunction = "N" OR $sFunction = "L" OR $sFunction = "R" OR $sFunction = "H"
OR $sFunction = "E" OR $sFunction = "K" OR $sFunction = "S" OR $sFunction = "V"
;...
Case $sNode >= "01" AND $sNode <= "99"


can be shortened e.g. to
Code:

CASE INSTR('NLRHEKSV',$sFunction)
;...
Case $sNode=VAL($sNode)



ArchAngel96
(Getting the hang of it)
2004-01-20 09:33 PM
Re: Spliting a String

Thanks all for the adivise and minor code corrections. Here is my final result, which appears to work exactly how I need it to.

Code:
 Dim $sType, $sLocation, $sLocation2, $sLocation3, $sFunction, $sNode, $sAsset, $sAsset2, $sAsset2

$machine = "S0002N01"
$computername = "Failed"

$sType=SubStr($machine,1,1)

Select
Case $sType = "S"
$sLocation=SubStr($machine,2,4)
$sFunction=SubStr($machine,6,1)
$sNode=SubStr($machine,7,2)
$sLocation2=Val($sLocation)
Select
Case Len($sLocation2) = 1
$sLocation3 = SubStr($sLocation,4)
If $sLocation2=$sLocation3
Select
Case $sLocation >= "0002" AND $sLocation <= "0999"
Select
Case InStr('NLRHEKSV',$sFunction)
$sNode2=Val($sNode)
Select
Case Len($sNode2) = 1
$sNode3 = SubStr($sNode,2)
If InStr('123456789',$sNode2) AND $sNode2=$sNode3
$computername = "Store"
EndIf
Case Len($sNode2) = 2
If $sNode=$sNode2 AND ($sNode >= "01" AND $sNode <= "99")
$computername = "Store"
EndIf
EndSelect
EndSelect
EndSelect
EndIf
Case Len($sLocation2) = 2
$sLocation3 = SubStr($sLocation,3)
If $sLocation2=$sLocation3
Select
Case $sLocation >= "0002" AND $sLocation <= "0999"
Select
Case InStr('NLRHEKSV',$sFunction)
$sNode2=Val($sNode)
Select
Case Len($sNode2) = 1
$sNode3 = SubStr($sNode,2)
If InStr('123456789',$sNode2) AND $sNode2=$sNode3
$computername = "Store"
EndIf
Case Len($sNode2) = 2
If $sNode=$sNode2 AND ($sNode >= "01" AND $sNode <= "99")
$computername = "Store"
EndIf
EndSelect
EndSelect
EndSelect
EndIf
Case Len($sLocation2) = 3
$sLocation3 = SubStr($sLocation,2)
If $sLocation2=$sLocation3
Select
Case $sLocation >= "0002" AND $sLocation <= "0999"
Select
Case InStr('NLRHEKSV',$sFunction)
$sNode2=Val($sNode)
Select
Case Len($sNode2) = 1
$sNode3 = SubStr($sNode,2)
If InStr('123456789',$sNode2) AND $sNode2=$sNode3
$computername = "Store"
EndIf
Case Len($sNode2) = 2
If $sNode=$sNode2 AND ($sNode >= "01" AND $sNode <= "99")
$computername = "Store"
EndIf
EndSelect
EndSelect
EndSelect
EndIf
EndSelect
Case InStr('WL',$sType)
$sAsset=SubStr($machine,2,6)
$sAsset2=Val($sAsset)
If Len($sAsset2) < 6
$sAsset = SubStr($machine,3,5)
EndIf
Select
Case $sAsset=$sAsset2
If $sAsset >= "000001" AND $sAsset <= "999999"
$computername = "Corp"
EndIf
EndSelect
EndSelect

? $computername

Of Course, if any one has any suggestions they are always welcome.


Les
(KiX Master)
2004-01-20 09:40 PM
Re: Spliting a String

Waddup with all those SELECT CASE constructs where there is only one or two CASEs? An IF ELSE ENDIF would do just as well.

ArchAngel96
(Getting the hang of it)
2004-01-20 10:06 PM
Re: Spliting a String

I had no choice but to go that route, there are at least two 'cases' for every condition. By using an 'else' statement the check may not meet the requirements and therefore I would get the wrong result. There are specific requirements the checks need to meet, there is no true 'else' here. Otherwise I'd have to have an 'If' for almost every condition.
I know its an UGLY thing to look at, how do you think I feel.. I had to write it, test it and potentially use it. My target clients don't all meet 'requirements' for many of the UDF's that would work out SO much better. Sorry to say, if I had enough time and knew enough perl I'd just convert the entire logon script... (Sorry Kix), but again many workstations don't meet the desired requirements. I'm stuck in a hole.


maciep
(Korg Regular)
2004-01-20 10:19 PM
Re: Spliting a String

What about something like this? I think it will work, if i understood your conditions correctly

Code:
 $nums = "1234567890"
$letters = "SNKEHMRL"
$computer = "S123N56"
$computerName = "Failed"
$stillGood = 1
if instr("WLS", left($computer, 1)) and len($computer) = 7
if left($computer, 1) = "S"
for $j = 2 to 4
if not instr($nums, substr($computer, $j, 1))
$stillGood = 0
endif
next

for $j = 6 to 7
if not instr($nums, substr($computer, $j, 1))
$stillGood = 0
endif
next

if instr($letters, substr($computer, 5, 1)) and $stillGood
$computerName = "Store"
endif
else
for $j = 2 to 7
if not instr($nums, substr($computer, $j, 1))
$stillGood = 0
endif
next
if $stillGood
$computerName = "Corp"
endif
endif
endif

? $computerName




Les
(KiX Master)
2004-01-20 10:37 PM
Re: Spliting a String

I dunno... maybe it's just me getting cross-eyed trying to follow the logic... but if your indenting is true to form, the following have only a single CASE:
Case $sLocation >= "0002" AND $sLocation <= "0999"
Case InStr('NLRHEKSV',$sFunction)

I take back the ELSE part.


ArchAngel96
(Getting the hang of it)
2004-01-20 11:06 PM
Re: Spliting a String

Man, I've been staring at this script for four days now. All i know is it works exactly how I need it to... lol

Ok, ok... Maybe there is a spot or two I could use 'If', but I really don't want to chance breaking what's now working...lol

After I look at some other scripts I'll see about cleaning it more... fresh eyes and all

Didn't see maciep message first time around.

I like that one better. But I'm testing it and it doesn't like 4 numbers starting with zero. I have to keep that due to our numbers growing regularly...
Line 8:
Code:
       For $j = 2 to 4 


doesn't appear to like 0123 or any other combination. But it should work based on how I'm reading your code.


ArchAngel96
(Getting the hang of it)
2004-01-20 11:50 PM
Re: Spliting a String

Ok I see where the issue is. I think it still needs to be broken up to WL and S sections.

WL will have 6 munbers following for a correct name. For a total of 7 characters.

S will have 4 numbers, a letter and 2 more numbers a total of 8 characters.

Also on a RARE occation our techs didn't know how to add the computer onto the domain correctly so some W/L machines have and extra 'character' of some kind after.. Still need to check for that. So I'll have to do some modifications...


maciep
(Korg Regular)
2004-01-21 02:25 AM
Re: Spliting a String

So, if 'S' will definitely only be 8 or 9 chars long and 'W/L' will be only 7 or 8 char long, this should work. If they could be longer, just change the $len comparison's to >7 and >6 respectively. Should work.

Code:

$nums = "1234567890"
$letters = "SNKEHMRL"
$computer = "S1253K59A"
$computerName = "Failed"
$stillGood = 1
$len = len($computer)
if instr("WLS", left($computer, 1))
if left($computer, 1) = "S" and ($len = 8 or $len = 9)
for $j = 2 to 5
if not instr($nums, substr($computer, $j, 1))
$stillGood = 0
endif
next

for $j = 7 to 8
if not instr($nums, substr($computer, $j, 1))
$stillGood = 0
endif
next

if instr($letters, substr($computer, 6, 1)) and $stillGood
$computerName = "Store"
endif
else
if $len = 7 or $len = 8
for $j = 2 to 7
if not instr($nums, substr($computer, $j, 1))
$stillGood = 0
endif
next
if $stillGood
$computerName = "Corp"
endif
endif
endif
endif

? $computerName