Jump to content
Sign in to follow this  
Achilles

Most efficient way to get a file name?

Recommended Posts

Achilles

Hello, I was wondering if you're given a full path, what is the fastest/most efficient way to get the file name. I came up with this:

$path = @ScriptFullPath 

Msgbox(0, '', _GetFileName($path))

Func _GetFileName($path) 
    $temp = StringSplit($path, '\') 
    
    Return $temp[$temp[0]]
EndFunc

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites
gumbitha

$PathArray = StringSplit($InputPath, "\")
$Filename = $PathArray[$PathArray[0]]

muttley

Share this post


Link to post
Share on other sites
crzftx

I ran the following to find out the bottom method is about 25% faster.

The top method takes about 12.79µs and the bottom method takes about 9.53µs; neither is going to hurt your execution time muttley

My method (I'm calling the bottom one mine...) takes up half the lines though.

Global Const $path = "C:\Windows\System32\Boot\en-US\winload.exe.mui", $amount = 200000
Global $timer, $array

$timer = TimerInit()
For $z = 0 To $amount
    $array = StringSplit($path, "\")
    $filename = $array[$array[0]]
Next
ConsoleWrite(Round(TimerDiff($timer)/$amount*1000,2) & "µs average" & @CRLF)

$timer = TimerInit()
For $z = 0 To $amount
    $filename = StringTrimLeft($path,StringInStr($path,"\",0,-1))
Next
ConsoleWrite(Round(TimerDiff($timer)/$amount*1000,2) & "µs average" & @CRLF)
Edited by crzftx

Share this post


Link to post
Share on other sites
weaponx

Global Const $path = "C:\Windows\System32\Boot\en-US\winload.exe.mui", $amount = 200000
Global $timer, $array

;STRINGSPLIT
$Timer1 = TimerInit()
For $z = 0 To $amount
    $array = StringSplit($path, "\")
    $filename = $array[$array[0]]
Next
ConsoleWrite(Round(TimerDiff($Timer1)/$amount*1000,2) & "µs average" & @CRLF)

;STRINGTRIMLEFT + STRINGINSTR
$Timer2 = TimerInit()
For $z = 0 To $amount
    $filename = StringTrimLeft($path,StringInStr($path,"\",0,-1))
Next
ConsoleWrite(Round(TimerDiff($Timer2)/$amount*1000,2) & "µs average" & @CRLF)

;STRINGREGEXPREPLACE
$Timer3 = TimerInit()
For $z = 0 To $amount
    $filename = StringRegExpReplace($path, "^.*\\", "")
Next
ConsoleWrite(Round(TimerDiff($Timer3)/$amount*1000,2) & "µs average" & @CRLF)

11.94µs average

6.74µs average

8.45µs average

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×