#32491 - 2002-11-14 09:14 PM
Repeat INSTR
|
Anonymous
Anonymous
Unregistered
|
I am using Kix 3.60. I'm sure this is already provided in a mroe recent version, but can't take the time right now to upgrade and risk having to debug anything that may result from the change. So, being fully aware that I am doing this the hard way, here is what I am trying to do... I need to identify the path that the user profiles are kept in, regardless of NT or 2K operating system. Here is what I tried:
do $temp = instr ($UProfile,"\") if $temp > 0 $position = $temp endif until $temp = 0 $ProfilesPath = substr($UProfile,1,$position) "Profiles path = " $profilesPath sleep 10
It went into an endless look though, as you might imagine. My goal was to find the last \ and then pull the part of the string that preceeds it. BTW, my $UProfile is the path to the profile directory of the current user... c:\winnt\profiles\username or c:\documents and settings\username I thought about just searching the $UProfile value for the username, but there are often times where the path is part of the username ending in ~1, so this wouldn't work. Anyway, I guess what I am after is a way to look for the \ character repeatedly until it isn't found again. Any suggestions?
|
|
Top
|
|
|
|
#32493 - 2002-11-14 09:26 PM
Re: Repeat INSTR
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
If I understand you correctly you want to return the last directory name of the path. $path returns "c:\acf\swert\asd".
code:
Break ON $Uprofile = "c:\acf\swert\asd\dir1"
$temp = $Uprofile $x = instr($Uprofile, "\") while ($x) $path = $path + "\" + substr($temp,1,$x-1) $temp = substr($temp,$x+1, len($temp)) $x = instr($temp, "\") Loop $path = Substr($path,2,len($path)) ? $path ? "Account is " + Substr($Uprofile,len($path)+2,len($uprofile))
[ 14. November 2002, 21:38: Message edited by: Howard Bullock ]
|
|
Top
|
|
|
|
#32495 - 2002-11-14 09:34 PM
Re: Repeat INSTR
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Updated code in my post above.
|
|
Top
|
|
|
|
#32496 - 2002-11-14 09:36 PM
Re: Repeat INSTR
|
Jochen
KiX Supporter
   
Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
|
If Howard guesses correctly (And see that this is the case after re-reading) I have one good reason (amongst several others) to upgrade for you :
quote: Split( )
Action: Returns a zero-based, one-dimensional array containing a specified number of substrings. Syntax: Split (“string”, “delimiter”, count) Parameters: String
Required. String expression containing substrings and delimiters. If expression is a zero-length string, Split returns an empty array, that is, an array with no elements and no data.
Delimiter
Optional. String character(s) used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
Count
Optional. Number of substrings to be returned; -1 indicates that all substrings are returned. Remarks: The Split function doesn't actually create a variable -- all it does is create an array, and the parser copies that back into whatever variable the result is being assigned to. Therefore, if you want Split to populate an array and have that array's contents available outside the current scope, you must make the variable/array global prior to populating it with the split function (Example 2). Returns: An array containing the substrings found in the input string. See Also: Join( ), UBound( )
[ 14. November 2002, 21:40: Message edited by: jpols ]
_________________________
|
|
Top
|
|
|
|
#32497 - 2002-11-14 09:41 PM
Re: Repeat INSTR
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I think I would use: quote: InStrRev( )
Action: Searches a string for the presence of a second string. The search is started from the end of the source string. Note that the offset returned is counted from the beginning of the source string. Syntax: INSTRREV ("string1", "string2") Parameters: String1
The string to search in.
String2
The string to search for. Returns: ? Offset of the first character of string2 found in string1, counted from the beginning of string1 0 String2 not present in string1 See Also: InStr( ), Left( ), Right( ), SubStr( ) Example: ; find last backslash in @CURDIR $x = INSTRREV(@CURDIR, "\")
|
|
Top
|
|
|
|
#32498 - 2002-11-14 09:42 PM
Re: Repeat INSTR
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
kind of Right()
code:
$path="c:\df\adsfvad\vwed"
$l=len($path) $index=$l $folder=""
while $index>0 and $folder="" if substr($path,$index,1)="\" $folder=substr($path,$index+1,$l-$index) endif $index=$index-1 loop ? $folder
[ 14. November 2002, 21:46: Message edited by: Radimus ]
|
|
Top
|
|
|
|
#32499 - 2002-11-14 09:45 PM
Re: Repeat INSTR
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
|
|
Top
|
|
|
|
#32501 - 2002-11-14 10:00 PM
Re: Repeat INSTR
|
Anonymous
Anonymous
Unregistered
|
WOW! So much help! You guys are GREAT!
While waiting for all these great suggestions, I kept chipping away at it myself. Yes, I will definitely make a priority of upgrading as soon as I can.
I guess I never stated my goal very well. For clarification, I wanted to take C:\winnt\profiles\username and get back C:\winnt\profiles\
or...
c:\documents and settings\username and get back c:\documents and settings\
I looked at all those suggestions. Honestly, I got lost in some of the string manipulating code. Not enough coffee yet I guess. I'm gonna tear apart your stuff later to understand it better. I ended up finding a solution that is not as clean as I sure yours is, but this is what I came up with. It seems to work, but if anyone sees any potential flaws, I'd sure love to hear what you have to say. Here's my code:
;$ProfilePath reading code $temp = 0 $position = 0 $tempprof=$UProfile do $temp = instr ($tempprof,"\") if $temp > 0 $position = $position + $temp $tempprof = substr($tempprof,$temp+1,50) endif until $temp = 0 $ProfilesPath = substr($UProfile,1,$position)
|
|
Top
|
|
|
|
#32502 - 2002-11-14 10:05 PM
Re: Repeat INSTR
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
in that case...
code:
$path="c:\df\adsfvad\vwed"
$l=len($path) $index=$l $folder=""
while $index>0 and $folder="" if substr($path,$index,1)="\" $folder=substr($path,1,$index-1) endif $index=$index-1 loop ? $folder
|
|
Top
|
|
|
|
#32504 - 2002-11-14 10:13 PM
Re: Repeat INSTR
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I had it Right and Wrong since my code returned both the path and the last directory.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1636 anonymous users online.
|
|
|