Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a program in a ForEach loop

I'm trying to get this simple PowerShell script working, but I think something is fundamentally wrong. ;-)

ls | ForEach { "C:\Working\tools\custom-tool.exe" $_ } 

I basically want to get files in a directory, and pass them one by one as arguments to the custom tool.

like image 306
Luke Quinane Avatar asked Oct 08 '08 01:10

Luke Quinane


People also ask

How do you run a foreach loop?

How foreach loop works? The in keyword used along with foreach loop is used to iterate over the iterable-item . The in keyword selects an item from the iterable-item on each iteration and store it in the variable element . On first iteration, the first item of iterable-item is stored in element.

Is there a foreach loop in C++?

There is no foreach loop in C, but both C++ and Java have support for foreach type of loop. In C++, it was introduced in C++ 11 and Java in JDK 1.5. 0 The keyword used for foreach loop is “for” in both C++ and Java.

What is foreach loop with example?

Prerequisite: Loops in C# The foreach loop is used to iterate over the elements of the collection. The collection may be an array or a list. It executes for each element present in the array. It is necessary to enclose the statements of foreach loop in curly braces {}.


1 Answers

If you still need quotes around the command path (say, if you've got spaces), just do it like this:

ls | % { &"C:\Working\tools\custom-tool.exe" $_.FullName } 

Notice the use of & before the string to force PowerShell to interpret it as a command and not a string.

like image 54
tomasr Avatar answered Sep 21 '22 12:09

tomasr