Page 1 of 1 1
Topic Options
#32491 - 2002-11-14 09:14 PM Repeat INSTR
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
#32492 - 2002-11-14 09:22 PM Re: Repeat INSTR
Jochen Administrator Offline
KiX Supporter
*****

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

If I understood that correctly you try to strip the trailing backslash from a path variable ?

So my approach is :

code:
break on

$path = "c:\winnt\profiles\user\desktop\"

if substr($path,len($path),1) = '\'
$path = substr($path,1,len($path)-1)
endif

$path

get $

hth
Jochen
_________________________



Top
#32493 - 2002-11-14 09:26 PM Re: Repeat INSTR
Howard Bullock Offline
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 ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#32494 - 2002-11-14 09:27 PM Re: Repeat INSTR
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
In other words...
Are you trying to return the index of a substring in another string, parsing from right to left?
{Edit}The answer appears to be yes and the question is a description of instrrev(){/Edit}

[ 14. November 2002, 21:50: Message edited by: Waltz ]
_________________________
We all live in a Yellow Subroutine...

Top
#32495 - 2002-11-14 09:34 PM Re: Repeat INSTR
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Updated code in my post above.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#32496 - 2002-11-14 09:36 PM Re: Repeat INSTR
Jochen Administrator Offline
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 Offline
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, "\")


_________________________
Home page: http://www.kixhelp.com/hb/

Top
#32498 - 2002-11-14 09:42 PM Re: Repeat INSTR
Radimus Moderator Offline
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 ]
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#32499 - 2002-11-14 09:45 PM Re: Repeat INSTR
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Very nice Radimus
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#32500 - 2002-11-14 09:56 PM Re: Repeat INSTR
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
If it came to a golf course I would prefer this :

code:
$path = "c:\df\adsfvad\vwed"

$a = split($path,'\')
$folder = $a[ubound($a)]

$folder ?

_________________________



Top
#32501 - 2002-11-14 10:00 PM Re: Repeat INSTR
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 Offline
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

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#32503 - 2002-11-14 10:07 PM Re: Repeat INSTR
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
So we were all wrong [Big Grin]

Your code works fine for me !

By the way If you post Code snippets use the Code tags wrapping it (There are Instant UBB Code buttons while replying) ... Much more readable

J.
_________________________



Top
#32504 - 2002-11-14 10:13 PM Re: Repeat INSTR
Howard Bullock Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#32505 - 2002-11-14 10:19 PM Re: Repeat INSTR
Waltz Offline
Seasoned Scripter

Registered: 2002-08-01
Posts: 485
Loc: Waterloo, Ontario, Canada
code:
$G = "c:\df\adsfvad\vwed"
$jpols = split($G,'\')
$L = $jpols[ubound($jpols)]
$G'O'$L'F'?

[Big Grin]
_________________________
We all live in a Yellow Subroutine...

Top
#32506 - 2002-11-14 10:28 PM Re: Repeat INSTR
Jochen Administrator Offline
KiX Supporter
*****

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

must participate one day !
When will be the next course ?
_________________________



Top
Page 1 of 1 1


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

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.068 seconds in which 0.028 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