Getting item name in a foreach loop?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
sinilill
Posts: 111
Joined: 02 Dec 2013 18:37

Getting item name in a foreach loop?

Post by sinilill »

Hi, tried to search the forum, but got a little overwhelmed.
How to get the selected item name in the foreach loop?

Example with selected item: D:\DOWNLOAD\MOVIES
foreach($item, "<get SelectedItemsPathNames |>") {run """program.exe"" -option1 ""MOVIES"" -option2 ""D:\DOWNLOAD\MOVIES"""; wait 50;}

Code: Select all

foreach($item, "<get SelectedItemsPathNames |>") {run """program.exe"" -option1 ""ITEM NAME"" -option2 ""$item"""; wait 50;}

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

Re: Getting item name in a foreach loop?

Post by highend »

$item refers to the current one in the loop...
One of my scripts helped you out? Please donate via Paypal

sinilill
Posts: 111
Joined: 02 Dec 2013 18:37

Re: Getting item name in a foreach loop?

Post by sinilill »

yeah, thats how I ment it :oops: sorry

In the script I need to use the base name and full path from the current item in the loop.
Option 1 is base name and option2 is full path.

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

Re: Getting item name in a foreach loop?

Post by highend »

By base name you mean: the name of the file (incl. the extension) without it's path?

Code: Select all

    foreach($item, "<get SelectedItemsPathNames |>") {
        $baseName = getpathcomponent($item, "file");
        run """program.exe"" -option1 ""$baseName"" -option2 ""$item""";
        wait 50;
    }
Otherwise you have to replace "file" with "base" if it's really the base name (without extension and path)...
One of my scripts helped you out? Please donate via Paypal

sinilill
Posts: 111
Joined: 02 Dec 2013 18:37

Re: Getting item name in a foreach loop?

Post by sinilill »

this worked flawlessly, thanks :appl:

Is there a easy way to remove the 2 first charachters (drive letter and colon) from the -option2 path?
Desired output: \DOWNLOAD\MOVIES

RegularExpression? ^(.{2})(.*) > $2 removes first 2 character, but how to use in a script?

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

Re: Getting item name in a foreach loop?

Post by highend »

Code: Select all

    $test = "D:\DOWNLOAD\MOVIES";
    text regexreplace($test, "^.{2}");
    text substr($test, 2);
substr() is the easier option but ofc regexreplace works fine as well.
One of my scripts helped you out? Please donate via Paypal

Post Reply