SC getpathcomponent problem

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Huidong
Posts: 213
Joined: 18 May 2011 21:55

SC getpathcomponent problem

Post by Huidong »

I'm trying this command to get the file extension, but somehow, when I use it inside a for loop via <allitems>, I get an extra right-quote in the result.

Code: Select all

foreach( $item, <allitems>, " " ) {
    $ext = getpathcomponent($item, "ext");
    echo $ext;
}
I get something like

Code: Select all

MP4"
But
getpathcomponent($item, "base");
works fine.

[EDIT]
I just noticed that, instead of using <allitems>, if I use get("ItemsPathNames"), then getpathcomponent($item, "ext"); worked!

highend
Posts: 14571
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: SC getpathcomponent

Post by highend »

This is not a bug of gpc().

It's a problem caused by using the wrong delimiter for foreach() / having a space in a path / item name

Unquoting the items would work but you would still be in trouble if you have space inside one of them...

Code: Select all

    foreach($item, <allitems>, " ") {
        $ext = getpathcomponent(trim($item, '"'), "ext");
        echo $ext;
    }
So the correct solution would be:

Code: Select all

    foreach($item, <allitems>, '" "') {
        $ext = getpathcomponent(trim($item, '"'), "ext");
        echo $ext;
    }
Moving this topic now...
One of my scripts helped you out? Please donate via Paypal

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: SC getpathcomponent problem

Post by Huidong »

Thanks a lot, highend:) Now I understand what <allitems> actually is -- it includes *literal* quotes around each individual path, and thus if it's split using only a space, each item will not be a valid path, as paths should not include those literal quotes!

And thanks for taking the time to offer the nice workaround to fix the issue. But I guess in general, I've learned that for iteration, using the get function is way better!

Best regards,
Huidong

Post Reply