#20892 - 2002-04-30 04:20 PM
Re: Get characters before file extension
|
Richard H.
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
KiX Supporter
   
Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
|
|
|
Top
|
|
|
|
#20894 - 2002-04-30 04:36 PM
Re: Get characters before file extension
|
Howard Bullock
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
|
|
Top
|
|
|
|
#20896 - 2002-04-30 04:57 PM
Re: Get characters before file extension
|
Richard H.
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
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.
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 ]
|
|
Top
|
|
|
|
#20898 - 2002-04-30 05:01 PM
Re: Get characters before file extension
|
Howard Bullock
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.
|
|
Top
|
|
|
|
#20900 - 2002-04-30 05:06 PM
Re: Get characters before file extension
|
Richard H.
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.
|
|
Top
|
|
|
|
#20902 - 2002-04-30 05:13 PM
Re: Get characters before file extension
|
Howard Bullock
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
|
|
Top
|
|
|
|
#20903 - 2002-04-30 06:43 PM
Re: Get characters before file extension
|
Bryce
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
|
|
|
|
#20905 - 2002-04-30 11:11 PM
Re: Get characters before file extension
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
DOH!!! curse you and your LEFT() thinking!!!!
|
|
Top
|
|
|
|
#20906 - 2002-04-30 11:15 PM
Re: Get characters before file extension
|
Howard Bullock
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().
I like your code better too. ![[Frown]](images/icons/frown.gif) [ 30 April 2002, 23:16: Message edited by: Howard Bullock ]
|
|
Top
|
|
|
|
#20907 - 2002-05-01 01:58 AM
Re: Get characters before file extension
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
My code using substr is even slower than Les' LEFT function. You win Les.
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
|
|
Top
|
|
|
|
#20908 - 2002-05-01 05:45 AM
Re: Get characters before file extension
|
Bryce
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
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
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 847 anonymous users online.
|
|
|