Jump to content

Parse Functions for FileNames and URL's

Recommended Posts

Parse Functions for FileNames and URL's

Development Guidance requested:

I have found it useful to have a function that returns appropriate parts of

file names and URL's. I couldn't find anything in the forums so I went back and

upsized my old DOS 'C' programs to 'AU3'

I appreciate there is a UDF _Splitpath function in the forum but it is cumbersome

in that you need to set up variables which will then be loaded with each part.

I have developed 4 inline functions which will parse both filenames and URL's.

There are however different choices on where to delimit the parse.

Below is what I return now and the options as I see them compared with the results from

_SplitPath($str, $drive, $dir, $file, $ext) as posted in these forums.

Note: that _Splitpath splits my _ParseRoot result into $Drive and $Dir.

I did not do this since it is easy to strip off the first two chars of Root if required.

Also SplitPath was not designed for URL's and their stubs.

***I'd appreciate your feedback on my choices before I post the functions as URL's.***


It would be even better if they could be included in the next distribution's

"include library" since I think they are useful enough. How do I submit them for this?

I have followed the format of the existing "include"d functions and would be happy to do

user guide pages.

Some Examples:

"C:" & _ParseName($filename) & ".txt" = To change type of file like in Html2txt!!

If _ParseRoot($filename) = "D:" Then .... = To check Source drive

"C:\dir\" & "My New File" & _ParseExt($filename) = To create new file with orig .ext

_ParseRoot($URL) & _ParseName($URL) & _ParseExt($URL) = strip stubs to get home page.

Ignore the `s. There is no Tab or Table func in this editor ! :ph34r:


If Input= "C:\root\sub.c\name.qual.ext"

Result=``````My Current`````` Option```````SplitPath

````````````````````````````````````````$Drive $Dir



_ParseExt ````.ext `````````.qual.ext`````````.ext

_ParseStub ```nul `````````` nul


If Input= "http(s)://www.site.qual/stub/stub:/etc"

Result=`````My Current`````Option``````SplitPath

`````````````````````````````````````$Drive $Dir






If Input= "www.site.qual/stub/stub:/etc"

Result=``````My Current`````Option````````SplitPath

````````````````````````````````````````$Drive $Dir






Edited by jhbell
Link to post
Share on other sites

The functions I wrote are an emulation of the C-library functions with the only differences being mine accept either Unix or Windows slashes (Or mixed) and they support UNC paths (Also, I return the data in some form or another instead of a success/failure return, which I think is what the C versions do, we have @error for that).

Also, it isn't necessary to set up 4 variables.

Local $drive, $dir, $NULL
_SplitPath("D:\The\Path\To\Split.exe", $drive, $dir, $NULL, $NULL)

Mix and match as you please, or declare a single variable and just use the array return value.

Local $NULL
Local $array = _SplitPath("D:\The\Path\To\Split.exe", $NULL, $NULL, $NULL, $NULL)
Link to post
Share on other sites

Good info. I learn more every day. I was not trying to critique yours just comparing the delimiter approach. In fact if a script needs a lot of this info yours would be faster to execute and easier to use.

But even with this I think it has to be set up and from what I can see in your code it can't just be used as an inline function for a single value since you populate the return array with all four elements. Am I right?

In fact when I look at it, I wonder why you bothered with all the byref variables at all. In fact I stripped them all out of yours and just called $file = _PathSplit (Filnametobesplit) then used $file[n] to get the piece(s) I wanted. But its still not an inline function.

So do mine have value in YHO and are they worth pursuing as included UDF's?


Link to post
Share on other sites

Again, mine were emulations of the C-library functions. The C-library takes those parameters and sets them in the same manner. 2 of the 3 C versions return void and the other (fullpath) returns the aboslute path. My return values were more an after-thought than anything else (Except for _FullPath, of course, which, like fullpath, should return something).

The URL part of your functions could be useful. I don't know that I would ever need it, but I'm sure others might. I'm sure others would prefer your approach for Windows/Unix paths (You do support Unix, right? And what about UNC shares?). I, however, would just wrap _SplitPath() if I wanted a quick one-line usable function for conditional comarison.

Link to post
Share on other sites

Appreciate your feedback..helping me learn...

1) No I made no effort to address UNIX paths or UNC Shares.. My focus is on the local environment and I assumed you would use different functions targeted at any other remote environment.

I think if one had to address UNIX then we would not be able to use it on URL's since it is the / vs \ that signals the difference.

2) I'm still not clear on if you can use your _splitPath as an in-line function.. what do you mean when you say "I, however, would just wrap _SplitPath() if I wanted a quick one-line usable function for conditional comarison" Would you explain what you mean by "wrap" and give me an example? Tks.

Link to post
Share on other sites

The wrapper:

Func GetDrive($path)
    Local $drive, $NULL
     _SplitPath($path, $drive, $NULL, $NULL, $NULL)
    Return $drive

And called as:

If GetDrive("C:\Path\To\Somewhere") <> "C:" Then MsgBox(4096, "Error", "Error")

Edit: Fixed typo.

Edited by Valik
Link to post
Share on other sites

Guess that means you would write my SplitRoot on the fly. I think thats clever and appropriate for an adept programmer but for casual types like me I'll stick with my predefined easy to use function. Also I will stick with my current delimiter definitions in the original post when I submit these functions.

Edited by jhbell
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...