Page 1 of 2 12>
Topic Options
#20890 - 2002-04-30 04:15 PM Get characters before file extension
Darren_W Offline
Hey THIS is FUN
*****

Registered: 2001-10-10
Posts: 208
Loc: Bristol, England
Hi,

I'm sure this is easy

I would like to take a file name like 'access.log' which is in a string like $filename

and take off the extension to get this

$filename = access

How is this done?

Darren
_________________________
I want to share something with you - the three sentences that will get you through life.
Number 1, 'cover for me.'
Number 2, 'oh, good idea, boss.'
Number 3, 'it was like that when I got here'.

Top
#20891 - 2002-04-30 04:19 PM Re: Get characters before file extension
Jochen Administrator Offline
KiX Supporter
*****

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

how about this :

code:
break on

$fn = "Access.log"

$fn = substr($fn,1,instr($fn,'.')-1)

$fn ?

get $

Ok, some more dots in filename can be fatal ...
_________________________



Top
#20892 - 2002-04-30 04:20 PM Re: Get characters before file extension
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Try this:
code:
substr($filename,1,instr($filename + ".",".")-1)



[ 30 April 2002, 16:21: Message edited by: Richard Howarth ]

Top
#20893 - 2002-04-30 04:23 PM Re: Get characters before file extension
Jochen Administrator Offline
KiX Supporter
*****

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

missing closing brackets ; Still a dot in result ...
Back to the olde drawing board [Razz]

[Big Grin]

Jochen
_________________________



Top
#20894 - 2002-04-30 04:36 PM Re: Get characters before file extension
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Maybe:
code:
$filename = "aaa.aaa.cc.log"
$basename = substr($filename, 1, InStrRev($filename, ".")-1)
? $basename

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

Top
#20895 - 2002-04-30 04:45 PM Re: Get characters before file extension
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
... most adequate solution for KiX v. 4.02 and higher , not useable in KiX 3.x

J.
_________________________



Top
#20896 - 2002-04-30 04:57 PM Re: Get characters before file extension
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Ok, fairly bullet proof solution for pre-4.02:
code:
$sFilename="foo..bar.exe"
$sExtension=""

$iLen=Len($sFilename)
While $iLen
If "."=SubStr($sFilename,$ILen,1)
$sExtension=SubStr($sFilename,$iLen+1,32768)
$sFilename=SubStr($sFilename,1,$iLen-1)
$iLen=0
Else
$iLen=$iLen-1
EndIf
Loop
"File = " $sFilename ?
"Extension = " $sExtension ?


Top
#20897 - 2002-04-30 04:58 PM Re: Get characters before file extension
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Darren did not specifically ask for a 3.x solution. If he did, I would have suggested an upgrade. [Smile]

But anyway Kix 3.x code:
code:
While instr($filename, ".") > 0
$x = instr($filename, ".")
If $x = 0
$x = len($filename)
Endif
$y = "" + $y + substr($filename, 1, $x)
$filename = substr($filename, $x+1, len($filename))
Loop
$y = substr($y, 1, len($y)-1)
? "y = " + $y



[ 30 April 2002, 16:58: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#20898 - 2002-04-30 05:01 PM Re: Get characters before file extension
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Richard, your too fast for me. It must the fact that your hours in front of me. [Smile]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#20899 - 2002-04-30 05:04 PM Re: Get characters before file extension
Darren_W Offline
Hey THIS is FUN
*****

Registered: 2001-10-10
Posts: 208
Loc: Bristol, England
Thanks

Howard's solution seems to work fine...

Darren
_________________________
I want to share something with you - the three sentences that will get you through life.
Number 1, 'cover for me.'
Number 2, 'oh, good idea, boss.'
Number 3, 'it was like that when I got here'.

Top
#20900 - 2002-04-30 05:06 PM Re: Get characters before file extension
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Heh, who'd ever have thought that "Kix speed scripting" would take off as a sport. [Wink]
Top
#20901 - 2002-04-30 05:07 PM Re: Get characters before file extension
Jochen Administrator Offline
KiX Supporter
*****

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

however ... running yours against $filename = "f.u.b.a.r.exe"

returns on 3.63 'y = 0f.u.b.a.r'

(correct on 4.x )

[Wink]
_________________________



Top
#20902 - 2002-04-30 05:13 PM Re: Get characters before file extension
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Apparently, Kix 3.x does not like $y = "" + $y ...

This code works on 3.62 & 3.63.
code:
$y = ""
While instr($filename, ".") > 0
$x = instr($filename, ".")
If $x = 0
$x = len($filename)
Endif
$y = $y + substr($filename, 1, $x)
$filename = substr($filename, $x+1, len($filename))
Loop
$y = substr($y, 1, len($y)-1)
? "y = " + $y

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

Top
#20903 - 2002-04-30 06:43 PM Re: Get characters before file extension
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Can i play!!

code:
$file =  "f.u.b.a.r.exe.txt"

$file = split($file,".")
for $i = 0 to ubound($file)-1
if $i = ubound($file)-1
$Name = $Name + $file[$i]
else
$Name = $Name + $file[$i] + "."
endif

next

? $Name

Bryce

Top
#20904 - 2002-04-30 07:00 PM Re: Get characters before file extension
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
ME TOO, ME TOO!

Break on
$file = "f.u.b.a.r.exe.txt"
$file = Left($file,InStrRev($file,".")-1)
$file ?
get $_

OK, so it's real close to Howard's... does it still count?

HB, I like mine better [Razz]

[ 30 April 2002, 19:13: 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
#20905 - 2002-04-30 11:11 PM Re: Get characters before file extension
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
DOH!!! curse you and your LEFT() thinking!!!! [Big Grin]
Top
#20906 - 2002-04-30 11:15 PM Re: Get characters before file extension
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Les, I can only get one 4.02 command into my code at a time. Maybe someday I will master all the functions including LEFT(). [Embarrassed]

I like your code better too. [Frown]

[ 30 April 2002, 23:16: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#20907 - 2002-05-01 01:58 AM Re: Get characters before file extension
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
My code using substr is even slower than Les' LEFT function. [Frown] You win Les. [Wink]

Time for a 10,000 iteration loop using each.
code:
Break ON
$file = "f.u.b.a.r.exe.txt"

$a = @ticks
for $i=1 to 10000
$basename = substr($file, 1, InStrRev($file, ".")-1)
next
$b=@ticks
? "HB = " + ($b-$a) + " ticks"

$a = @ticks
for $i=1 to 10000
$basename = Left($file,InStrRev($file,".")-1)
next
$b=@ticks
? "Les = " + ($b-$a) + " ticks"

HB = 591 ticks
Les = 550 ticks
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#20908 - 2002-05-01 05:45 AM Re: Get characters before file extension
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
hmmm weird....

i ran this.

code:
$a = val(@ticks)
$file = "f.u.b.a.r.exe.txt"
$file = split($file,".")
for $i = 0 to ubound($file)-1
if $i = ubound($file)-1
$Name = $Name + $file[$i]
else
$Name = $Name + $file[$i] + "."
endif

next
$b = val(@ticks)
? $Name
? "Time spent = " $b-$a


$a = val(@ticks)
$file = "f.u.b.a.r.exe.txt"
$file = Left($file,InStrRev($file,".")-1)
$b = val(@ticks)
? $file
? "Time spent = " $b-$a

Mine and Les's code to measure any speed dif, and on my computer, both are

Time = 0

I had to put a sleep(1) statement in there just to make sure it was working!!

Bryce

Top
#20909 - 2002-05-01 06:03 AM Re: Get characters before file extension
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Bryce, recheck your code and the code I posted. I have the code executing in a 10,000 iteration loop to get ~550 ticks.

Try:
code:
break on
$file = "f.u.b.a.r.exe.txt"
$a = @ticks
for $j=1 to 10000
$Name = ""
$filearray = split($file,".")
for $i = 0 to ubound($filearray)-1
if $i = ubound($filearray)-1
$Name = $Name + $filearray[$i]
else
$Name = $Name + $filearray[$i] + "."
endif
next
next
$b = @ticks
? $Name
? "Time spent = " $b-$a


$a = @ticks
for $j=1 to 10000
$file = "f.u.b.a.r.exe.txt"
$file = Left($file,InStrRev($file,".")-1)
next
$b = @ticks
? $file
? "Time spent = " $b-$a


f.u.b.a.r.exe
Time spent = 5238
f.u.b.a.r.exe
Time spent = 601
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 941 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.073 seconds in which 0.025 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