getpathcomponent($path, "base") wrong when folder has dot

Things you’d like to miss in the future...
Post Reply
binocular222
Posts: 1416
Joined: 04 Nov 2008 05:35
Location: Hanoi, Vietnam

getpathcomponent($path, "base") wrong when folder has dot

Post by binocular222 »

with a folder with dot in folder name such as C:\first.second
getpathcomponent($path, "base")
would return "first" instead of "first.second"
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

klownboy
Posts: 4109
Joined: 28 Feb 2012 19:27

Re: getpathcomponent($path, "base") wrong when folder has do

Post by klownboy »

Per the help file, "base" is base "filename" without extension and doesn't apply to folders. Something like this...

Code: Select all

::text getpathcomponent("G:\House\Damas.cus\Home_inspection.pdf", "base");
does properly return Home_inspection. So since it's suppose to apply to the file, XY is probably treating your folder like a file and splitting it at the extension.
Windows 11, 22H2 Build 22621.1555 at 100% 2560x1440

bdeshi
Posts: 4249
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: getpathcomponent($path, "base") wrong when folder has do

Post by bdeshi »

gpc() ( :whistle: ) doesn't check if given string is a folder etc, so it can be said to be working as intended with b222's string.
But gpc() by default processes everything as filepaths, simply stripping trailing/extra /:\ and tokenizing the string. Some odd results:

Code: Select all

::text getpathcomponent("D:\", "file");
::text getpathcomponent("D:\Docs.Proj\", "file");
::text getpathcomponent("G:\", "base");
::text getpathcomponent("G:\", "file");
::text getpathcomponent("Impossible^&?*:!],=..\file.txt", "parent"); 
I only wish the sc would consider strings ending in either slash as folder.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: getpathcomponent($path, "base") wrong when folder has do

Post by admin »

SammaySarkar wrote:gpc() ( :whistle: ) doesn't check if given string is a folder etc, so it can be said to be working as intended with b222's string.
But gpc() by default processes everything as filepaths, simply stripping trailing/extra /:\ and tokenizing the string. Some odd results:

Code: Select all

::text getpathcomponent("D:\", "file");
::text getpathcomponent("D:\Docs.Proj\", "file");
::text getpathcomponent("G:\", "base");
::text getpathcomponent("G:\", "file");
::text getpathcomponent("Impossible^&?*:!],=..\file.txt", "parent"); 
I only wish the sc would consider strings ending in either slash as folder.
I don't find these so odd. To me the results are expected.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: getpathcomponent($path, "base") wrong when folder has do

Post by highend »

In general it would be nice if getpathcomponent could check if an item is a folder and return the correct elements :)
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: getpathcomponent($path, "base") wrong when folder has do

Post by admin »

What are the correct elements in the above examples? Apart from Base everything is correct now IMO.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: getpathcomponent($path, "base") wrong when folder has do

Post by highend »

Imho:
text getpathcomponent("R:\abc.def\ghi.jkl", "path");
Current : R:\abc.def
Expected: R:\abc.def\ghi.jkl
Comment : Help file states: path: [default] The path without any file.
There is no file at all so returning "R:\abc.def"
is actually not the (full path), but the path of the parent folder

text getpathcomponent("R:\abc.def\ghi.jkl", "parent");
OK

text getpathcomponent("R:\abc.def\ghi.jkl", "file");
Current : ghi.jkl
Expected: <empty>
Comment : There isn't any file!

text getpathcomponent("R:\abc.def\ghi.jkl", "base");
Current : ghi
Expected: ghi.jkl
Comment: Folders don't have any extensions at all, so the base is the folder name without path

text getpathcomponent("R:\abc.def\ghi.jkl", "ext");
Current : jkl
Expected: <empty>
Comment : Folders don't have any extensions at all...

text getpathcomponent("R:\abc.def\ghi.jkl", "count");
OK

text getpathcomponent("R:\abc.def\ghi.jkl", "component", -x);
OK
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: getpathcomponent($path, "base") wrong when folder has do

Post by admin »

OK, I have a different understanding of "file" and "path" in this context. To me "file" is the last component, and path is all other components.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: getpathcomponent($path, "base") wrong when folder has do

Post by highend »

But this is not what you expect when you use gpc() on a folder with dots in it...
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: getpathcomponent($path, "base") wrong when folder has do

Post by admin »

Yes, the "base" case could be improved. At least when the path ends with a slash (I don't want to test for folder vs file because this takes time and can be troublesome in case of network paths). So I suggest this change:

text getpathcomponent("R:\abc.def\ghi.jkl", "base"); //ghi
text getpathcomponent("R:\abc.def\ghi.jkl\", "base"); //ghi.jkl = NEW BEHAVIOR

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: getpathcomponent($path, "base") wrong when folder has do

Post by TheQwerty »

I have mixed feelings about this...

For GPC to determine that 'R:\abc.def\ghi.jkl' is a folder and not a file it has to check if it exists.
That has the potential to make it slower, but that also means working with paths you know don't exist could become more difficult.

Perhaps this should be under a flag parameter?
flag
0 = Default (as it is now)
1 = Check if path is a folder or file first.


Frankly I'm more agitated by the treatment of drive letters & servers because it can become difficult to determine if you got back a drive letter or folder name:

Code: Select all

echo GetPathComponent('C:\abc', 'path'); // 'C' - expected 'C:'
echo GetPathComponent('\\server', 'path'); // '\' - expected '\\server
echo GetPathComponent('C:\abc', 'parent'); // 'C' - would prefer 'C:'
echo GetPathComponent('C:\C\abc', 'parent'); // 'C' - okay

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: getpathcomponent($path, "base") wrong when folder has do

Post by highend »

The last time I used gpc to create a script and relied on the result it failed (http://www.xyplorer.com/xyfc/viewtopic.php?f=3&t=12733).

Maybe make it optional if we want it to check for files / folders and take the risk that it slows down (but delivers correct results) like TheQwerty suggests.
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: getpathcomponent($path, "base") wrong when folder has do

Post by admin »

TheQwerty wrote:I have mixed feelings about this...

For GPC to determine that 'R:\abc.def\ghi.jkl' is a folder and not a file it has to check if it exists.
That has the potential to make it slower, but that also means working with paths you know don't exist could become more difficult.

Perhaps this should be under a flag parameter?
flag
0 = Default (as it is now)
1 = Check if path is a folder or file first.


Frankly I'm more agitated by the treatment of drive letters & servers because it can become difficult to determine if you got back a drive letter or folder name:

Code: Select all

echo GetPathComponent('C:\abc', 'path'); // 'C' - expected 'C:'
echo GetPathComponent('\\server', 'path'); // '\' - expected '\\server
echo GetPathComponent('C:\abc', 'parent'); // 'C' - would prefer 'C:'
echo GetPathComponent('C:\C\abc', 'parent'); // 'C' - okay
Yes, a flag would be an option. Maybe later.

I admit I agree with your expectations listed above. The current behavior looks buggy to me, so I think it's okay to change it (which may break some scripts...).


BTW, although I document everything I currently cannot find the reason why I decided to remove the colon after the drive letter. :?

Code: Select all

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:".
The resulting issue had been discussed already before: http://www.xyplorer.com/xyfc/viewtopic.php?f=5&t=12219
Probably my intention had been to do it just for "drive", but buggily I implemented it for all modes.

neil9090
Posts: 64
Joined: 28 Jun 2014 00:09

Re: getpathcomponent($path, "base") wrong when folder has do

Post by neil9090 »

Just wondering if the logic should be to add an additional component fullpath, change behaviour of path just to return the path without server/drive would break many existing scripts so maybe pathonly component?
component
pathonly = folderpath
fullpath = C:\folderpath
path = folderpath

Code: Select all

echo GetPathComponent('C:\abc', 'path'); // 'C' - expected '' (nothing) <<change in behaviour>>
echo GetPathComponent('\\server', 'path'); // '\' - expected '\\server

<<new option fullpath>>
echo GetPathComponent('C:\abc\abc', 'fullpath'); // 'C:\abc\abc' (if a directory) else C:\abc
echo GetPathComponent('C:\abc', 'fullpath'); // 'C:\abc' (if a directory) else C:\
echo GetPathComponent('C:\file.txt', 'fullpath'); // 'C:\'  

echo GetPathComponent('C:\abc', 'parent'); // 'C' - would prefer 'C:'
echo GetPathComponent('C:\C\abc', 'parent'); // 'C' - okay
I have just discovered this issue when using "C:\dir\file.txt" the gpc path resolved to "C:\dir" but when a call to gpc is done in the root it resolved to be "C" not "C:\"
thus read this item and the other item http://www.xyplorer.com/xyfc/viewtopic.php?f=5&t=12219
e.g. C:\file.txt returns C not C: but in a subdirectory C:\dir\file.txt return C:\dir

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: getpathcomponent($path, "base") wrong when folder has do

Post by admin »

As I said I think the previous behavior was buggy and never intended like this by me. It was a mistake that I have corrected now.

Post Reply