Page 1 of 1

SC getpathcomponent problem

Posted: 10 Dec 2018 21:08
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!

Re: SC getpathcomponent

Posted: 10 Dec 2018 21:20
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...

Re: SC getpathcomponent problem

Posted: 11 Dec 2018 08:18
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