Make your own free website on Tripod.com
 
 
 Tips & Trix
Extract the Filename from a Full Path

Home
PUBz
Grafx Pax
Tips & Trix
Neo-Linx
Neo-Talk
Neo-Filez
Search NeoDezign
 
 
 

 


If you are a user of one of the fine Addons available for NeoBook you might have noticed that they all have one thing in common. The Open dialogs always return a Full Path Statement to the target file. This is not a bad thing really. More often than not you would need the full path to exactly locate the selected file.

However, what if for some reason you need only the filename of the target file? Obviously you would need to strip away the path and be left with nothing but the actual filename.

I noticed that some Addons have a "feature" that strips strings in this manner. It seems to me that this would only add more code to the AddOn bloating it with functions that are already built into NeoBook anyway! (But what do I know?)

Anyway, when we look at any and all path statements on planet earth there is one thing that will ALWAYS be in EVERY path statement... Backslashes! (duh??) The backslash can be used as a marker to determine where the path ends and the filename begins. If you SearchStr "C:\Autoexec.bat" the backslash is the #3 position. Easy enough. But, what if you have a long path like... 

"C:\Program Files\Company Name\Application Name\Filename.exe". 

First of all, you have to scan that whole long string. Secondly you would have to pick out each backslash postion. 

So, I tackled this backwards. Starting from the last character of the path statement scanning backwards. The very first backslash encountered (from right to left) is where the path ends. Viola! Use SubStr to extract a new variable starting from 1 to the marker position and you have your path. Use SubStr again from the marker position +1 to the StrLen of the path and you have your filename. (If you understood what I just said, congrats! You're as big a neo-geek as I am. ;)  Read the remarks in the Working Example code below to follow the flow. It might make more sense!)


 
Working Example:
(replace addon.exe and parameters with your preffered addon filename)
 

. /////// Fire up the AddOn and grab a file
ExecuteAddOn "[PubDir]addon.exe" "openfile 'EXE File' '*.exe'"

. /////// Initiate a VAR with the full PATH and the FILE
SetVar "[pathfile]" "[addon_openfile]"

. for this example let's assume this is the [pathfile]
. C:\Program Files\Company Name\Application Name\Filename.exe
. it is 59 characters long
. "\" is at position 47

. /////// BEGIN - Extract the filename from the path /////////////////////

. determine the length of the string  (59)
StrLen "[pathfile]" "[pathfile_len]"

. set a var at the end of the string (59)
SetVar "[charpos]" "[pathfile_len]"

. initiate a loop
SetVar "[loop]" "1"
While "[loop]" "=" "1"

. begin looking at the full path from the last character (59)
SubStr "[pathfile]" "[charpos]" "1" "[char]"

. determine if the character is a "\" backslash
If "[char]" "=" "\"

. if the "\" is found extract from one position more than the 
. "\" to the end of the full path. This is the [filename]!
. "Filename.exe"
SubStr "[pathfile]" "[charpos]+1" "[pathfile_len]" "[filename]"

. extract just the path starting from the 1st character up to
. the position where the "\" was found. This is the [pathonly]!
. "C:\Program Files\Company Name\Application Name\"
SubStr "[pathfile]" "1" "[charpos]" "[pathonly]"

. when everything is done break out of the while loop with a zero
SetVar "[loop]" "0"
EndIf

. here we are moving backwards from character position 59
. 59-1, 58-1, 57-1, etc...
Math "[charpos]-1" "" "[charpos]"
EndWhile

. /////// END - Extract the filename from the path /////////////////////

. a couple debugger dialogs
.AlertBox "Path" "[pathonly]"
.AlertBox "Filename" "[filename]"