Page 1 of 1

Getting item name in a foreach loop?

Posted: 20 Dec 2014 16:07
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;}

Re: Getting item name in a foreach loop?

Posted: 20 Dec 2014 16:32
by highend
$item refers to the current one in the loop...

Re: Getting item name in a foreach loop?

Posted: 20 Dec 2014 17:01
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.

Re: Getting item name in a foreach loop?

Posted: 20 Dec 2014 17:35
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)...

Re: Getting item name in a foreach loop?

Posted: 20 Dec 2014 22:50
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?

Re: Getting item name in a foreach loop?

Posted: 21 Dec 2014 00:26
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.