PDA

View Full Version : parse$ or parse direction



kryton9
26-04-2007, 12:11
Right now you can parse from left to right, is there a way to parse from right to left?

There might be an easier way, so I will put more detail of what I am trying to do:
I have text using dir_listarray, I would like to extract the filename which is at the very end of the path for each record.
So if I could parse backwards to the "\", it would give me the filename in an easy way.

Thanks.

ErosOlmi
26-04-2007, 12:26
For this specific purpose use FILE_PathSplit function:
http://www.thinbasic.com/public/products/thinBasic/help/html/file_pathsplit.htm

Here for example
http://www.thinbasic.com/public/products/thinBasic/help/html/fileequates.htm

You can get
drive name
directory path
file name
file extension



%Path_RootPathProgExt
%Path_RootPathProg
%Path_RootPath
%Path_Root
%Path_FileExt
%Path_File
%Path_Ext

Standard Paths
_______________Path____________
| |
Root | Prog_ Ext
| | || | | |
d:\program files\folder1\folder2\program.exe

UNC Paths
_______________Path__________
|____Root_____ | Prog_ Ext
| | || | | |
\\server\share\folder1\folder2\program.exe


I will check PARSE functions to see what I can do with right to left.

Eros

ErosOlmi
26-04-2007, 12:30
Example:


uses "file"

Dim MyPath as string = "d:\program files\folder1\folder2\program.exe"

msgbox 0, "File name and extension: " & file_pathsplit(MyPath, %Path_FileExt)
msgbox 0, "File name only: " & file_pathsplit(MyPath, %Path_File)
msgbox 0, "Extension only: " & file_pathsplit(MyPath, %Path_Ext)
msgbox 0, "full Path: " & file_pathsplit(MyPath, %Path_RootPath)


Not bad, isn't it? Sorry I'm just joking. FILE_PathSplit was developed because we faced this problem a lot of time and we was bored about parsing every time again. It also works on UNC path type so also on network shared resources.

Windows API have some equivalent or very close functionalities but some of the needed API are only available from W2K on. See shlwapi.dll DLL.

Ciao
Eros

kryton9
26-04-2007, 12:44
FANTASTIC Eros, thanks so much, I figured there might be an easier way, couldn't be better than that, woo hoo, horray!!!

kryton9
26-04-2007, 13:03
I just tried it with dir_listarray and it doesn't quite work right. It won't see the filename or extention. I am reading m15 files, so that could be the reason.

But I can still use it and it will be very useful. I can use rootpath, then subrtact from the whole string and easily pull out the filename.
I just wanted to mention it in case it is a minor bug.

Petr Schreiber
26-04-2007, 14:40
Hi,

just quick though, I think PARSE$ with negative index can do the job too for right-to-left scan:


msgbox 0, PARSE$("C:\foo\bar\model.m15", "\", -1)



Bye,
Petr

ErosOlmi
26-04-2007, 15:02
I just tried it with dir_listarray and it doesn't quite work right. It won't see the filename or extention. I am reading m15 files, so that could be the reason.

But I can still use it and it will be very useful. I can use rootpath, then subrtact from the whole string and easily pull out the filename.
I just wanted to mention it in case it is a minor bug.

Ken I do not understand what is not working.
Can you please make an example not working fine? I will have a look immediatelly.

Yes, Petr is right, I forgot about it and also I didn't mention in help.
If index is negative PARSE$ with parse from right to left.

kryton9
26-04-2007, 20:55
I will make an example later tonigth Eros, need to run now.

Petr, thanks for the tip, will try that one later too, sounds great!

kryton9
27-04-2007, 01:58
Attached is an example, on my computer it shows the models folder and never the files themselves.
Hope it proves useful.

ErosOlmi
27-04-2007, 06:49
Please substitute line:


...
m(i) = file_pathsplit(s, %Path_FileExt)
...


with the following:


...
m(i) = file_pathsplit(mylist(i), %Path_FileExt)
...


and all shold be ok.
s variable is not containing any file name but the directory where to search.

Ciao
Eros

kryton9
27-04-2007, 08:29
Glad it was a stupid error on my part, thanks Eros. Even when you explained it, it took me awhile to catch it in my main program as more code there and that "s" just looks so good there, as if it belongs :)