Page 1 of 1

getPathComponent(): Returns specified path component

Posted: 17 Aug 2012 21:35
by Stefan
Surprise :appl:

Code: Select all

v11.40.0202 - 2012-08-17 20:58
  + Scripting got a new function.
      Name:   getPathComponent
      Action: Returns the specified component of a path.
      Syntax: getpathcomponent([path], [component])


Handy. Thank you.

Example use:

Code: Select all

$A = "C:\Documents and Settings\Administrador\Meus documentos\XYplorer\Contas a receber.docx.bak";

  msg "Orig: $A<crlf>path: "      . getpathcomponent($A, path);
  msg "Orig: $A<crlf>parent: "    . getpathcomponent($A, parent);
  msg "Orig: $A<crlf>file name: " . getpathcomponent($A, file);
  msg "Orig: $A<crlf>file base: " . getpathcomponent($A, base);
  msg "Orig: $A<crlf>file ext: "  . getpathcomponent($A, ext);

I wonder if you want to hear improvement ideas? :whistle:

getpathcomponent([path] [, component] [, level] )

Component: The component to return; can be any of the following:
        folder: one folder name from the path, specified by level

         Level: only used for component "folder". Returns the n'th folder name from path.
                   Values are: 1 for the top folder, 2 for the second from top and so on.
                   Negative value will return folder name from the right. E.g. -1 returns parent folder name.


Return an empty string if the level in question is not present.

Would help in case of such > issues (click)

Re: getPathComponent(): Returns specified path component

Posted: 17 Aug 2012 21:40
by highend
getpathcomponent([path] [, component] [, level] )

Component: The component to return; can be any of the following:
folder: one folder name from the path, specified by level

Level: only used for component "folder". Returns the n'th folder name from path.
Values are: 1 for the top folder, 2 for the second from top and so on.
Negative value will return folder name from the right. E.g. -1 returns parent folder name.
+5^^

No really, would be incredible useful!

Re: getPathComponent(): Returns specified path component

Posted: 18 Aug 2012 15:10
by admin
Stefan wrote:I wonder if you want to hear improvement ideas? :whistle:

getpathcomponent([path] [, component] [, level] )

Component: The component to return; can be any of the following:
        folder: one folder name from the path, specified by level

         Level: only used for component "folder". Returns the n'th folder name from path.
                   Values are: 1 for the top folder, 2 for the second from top and so on.
                   Negative value will return folder name from the right. E.g. -1 returns parent folder name.


Return an empty string if the level in question is not present.
Isn't that identical to

Code: Select all

component = gettoken(path, level, "\")
?

Re: getPathComponent(): Returns specified path component

Posted: 18 Aug 2012 17:33
by Stefan
admin wrote:
Stefan wrote:
getpathcomponent([path] [, component] [, level] )


Isn't that identical to

Code: Select all

component = gettoken(path, level, "\")
?
Yes exactly, it is.

Code: Select all

$A = "C:\Documents and Settings\Administrador\Meus documentos\XYplorer\Contas a receber.docx.bak";
 //gettoken(string, [index=1], [separator=" "], [format])
  msg gettoken( $A,  2, "\");
  msg gettoken( $A, -2, "\");
I just though that that would belong to getpathcomponent() too, and there the user would want to lockup it.
Whereas to use gettoken() is not that obvious.


.

Re: getPathComponent(): Returns specified path component

Posted: 18 Aug 2012 17:44
by admin
Yep, good thinking, I'll add it. :)

I'd rather call it "component" than "folder" because also drives and files can be returned.

Question: what should be returned when level resp. index = 0?

Re: getPathComponent(): Returns specified path component

Posted: 18 Aug 2012 22:42
by Stefan
admin wrote:Yep, good thinking, I'll add it. :)
Thanks.
And a another plus:
getpathcomponent() works for both "\" and "/"
whereas with gettoken() i would have to take care myself that it works for both cases.

admin wrote:I'd rather call it "component" than "folder" because also drives and files can be returned.
I didn't said anything against it :lol:
See, "folder" is ONE of the "component".
Analog to "path", "drive", "parent", "file", "base" and "ext"

- - -

BTW, getpathcomponent(path, drive);
will return the drive letter including the colon.

I think we want to get the drive letter without the colon.

It is easier to add an ':' ourself then to use an another .replace() command.

- - -
admin wrote:Question: what should be returned when level resp. index = 0?
Hmm, brain storming...
Since this is for users and not for programmers...
and since i meant that for "folders" / hierarchy levels ...
and since there is already "drive"...
i have no real use for '0' :wink:

But i think '0' could still return the drive letter, since i have no better use for it.
And '1' should return the top folder. It's not the first level. But the first folder.
'2' the second top-level folder.
And '-1' returns the parent folder.
'-2' the grand parent folder name.
...


- - -

And how to handle the filename if present in path?

$A = "C:\Documents and Settings\Administrador\Meus documentos\XYplorer\Contas a receber.docx.bak";

Normally '-1' for an ARRAY would return blindly the filename "Contas a receber.docx.bak"
But now, since i talk about an "component" named 'FOLDER', should not '-1' return "XYplorer" ?
Hmm, hard. But i would tend to Yes, return folder names only.


$A = "C:\Documents and Settings\Administrador\Meus documentos\XYplorer\Contas a receber.docx.bak";

getpathcomponent([path] [, component] [, level] )
getpathcomponent( $A, "folder" ,  0 ) // C
getpathcomponent( $A, "folder" ,  1 ) // Documents and Settings
getpathcomponent( $A, "folder" ,  2 ) // Administrador
getpathcomponent( $A, "folder" ,  3 ) // Meus documentos
getpathcomponent( $A, "folder" , 14 ) // ' '
getpathcomponent( $A, "folder" , -2 ) // Meus documentos
getpathcomponent( $A, "folder" , -1 ) // XYplorer

getpathcomponent( $A, "drive" )        // C
getpathcomponent( $A, "path" )        // "C:\Documents and Settings\Administrador\Meus documentos\XYplorer
getpathcomponent( $A, "parent" )     // XYplorer
getpathcomponent( $A, "file" )          // Contas a receber.docx.bak
getpathcomponent( $A, "base" )       // Contas a receber.docx
getpathcomponent( $A, "ext" )         // bak



- - -

BTW, just tested: on an path without an file name
"C:\Documents and Settings\Administrador\Meus documentos\XYplorer\

getpathcomponent(path, path); will still drop blindly the last level, so here the parent folder.
getpathcomponent(path, parent); We will get here the grand parent name instead.
The same for 'file' and 'base' which returns both blindly the parent folder name.
And 'ext' will be empty (if not the folder has an dot by change)

I know it's by design, i just mention it here for reference.

Or you may want to check if the last item has the folder attribute or not?
And then return more the expected result.


- - -

Just my cogitations. HTH? And sorry for much text.


More opinions?


.

Re: getPathComponent(): Returns specified path component

Posted: 19 Aug 2012 10:42
by admin
Stefan wrote:I think we want to get the drive letter without the colon.
Really? Other opinions?

Re: getPathComponent(): Returns specified path component

Posted: 19 Aug 2012 12:40
by highend
admin wrote:
Stefan wrote:I think we want to get the drive letter without the colon.
Really? Other opinions?
In my (special) case: Yes

I have to deal with linux formatted paths (I use rsync (in Windows) to store backups in the cloud) almost daily and assembling the paths is smarter when I only get the drive letter without the colon.

Re: getPathComponent(): Returns specified path component

Posted: 19 Aug 2012 12:45
by admin
OK, I see.

Re: getPathComponent(): Returns specified path component

Posted: 21 Aug 2012 14:46
by Stefan
Stefan wrote:
admin wrote:I'd rather call it "component" than "folder" because also drives and files can be returned.
I didn't said anything against it :lol:
See, "folder" is ONE of the "component".
Analog to "path", "drive", "parent", "file", "base" and "ext"
v11.40.0204 - 2012-08-18 21:11
+ SC getPathComponent enhanced: Now you can return components by
their position index. Negative indices can be used to refer to the
position from the right end.
Syntax: getpathcomponent([path], [component], [index=1])
component: The component to return:
--> "component": the component referred to by index
Ahh, now i understand what you had talked about.
Only if you had chosen an better name for component "component"




- - -

Just for reference, further modifications on getPathComponent()
v11.40.0205 - 2012-08-19 12:52
* SC getPathComponent: Now any trailing colon (:) is removed from the
returns, so "drive" will now return the drive letter alone: "E" instead of "E:".
v11.40.0206 - 2012-08-20 12:07
+ SC getPathComponent enhanced: Added two further values for the "component" parameter.
count: The count of components.
server: The server in case of an UNC path, else the drive.