How to use Powershell to run a program against all files in a directory

I am using windows 8 pro and want to do what I hope is very simple. I just want to execute one program on all files of a certain type in a directory. no trees, only in a flat catalog. On Linux, I would:

find . -name 'exec c:\user\local\bin\myprog {} \; 

I literally spend a couple of hours struggling with the power shell, encountering policy issues, permissions, etc. Is there any easy way to do this?

+7
source share
2 answers

This is easy, but different than using find for example:

 Get-ChildItem -File | Foreach {c:\user\local\bin\myprog $_.fullname} 

To execute commands on the command line, aliases can make this a bit more concise:

 ls -file | % {c:\user\local\bin\myprog $_.fullname} 

PowerShell prefers teams that are narrow in focus but that can be piped together to provide many features. In addition, PowerShell binds .NET objects, for example. Get-ChildItem uses System.IO.FileInfo objects. You can then use commands such as Foreach, Where, Select, Sort, Group, Format to control objects piped. If you have the time, I recommend that you check out my free Effective PowerShell e-book.

+15
source

Try it. Change dir to this directory:

 cd \path\to\directory\with\files 

Run the program for all files in the directory:

 c:\user\local\bin\myprog *.type 

I hope I'm asking the right question.

0
source

All Articles