views:

86

answers:

4

How do I achieve this in Windows using either Command-prompt or PowerShell?

myprog *

EDIT: I want to call myprog with each file in the current directory as an argument.

A: 

Do you mean the following construct in command shell (cmd.exe):

for %f IN (*.*) DO echo %f
olegz
I want to call myprog with each file in the current directory as an argument.
manu1001
+2  A: 

If your goal is to call "myprog.exe" for each file in a directory, assuming that "myprog.exe" does not handle wildcards by itselef, it should be something like that in powershell :

dir | % {prog $_.FullName} 

Edit : taking your comment into account, try this :

prog (get-childItem -name)

Hope it helps...

Cédric Rup
I want to run only one instance of myprog with multiple arguments, each argument corresponding to a file in the current directory.eg: in unix 'myprog *' would expand to 'myprog a.txt g.txt p.txt' ..etc
manu1001
+1  A: 

I wrapped Cédric Rup's solution in a function ea (ExpandAll?). Not as clean as the Unix approach though.

function ea ($p="*") { dir $p | foreach { '"{0}" ' -f $_.Name } }

.\Myprog.exe (ea)
.\Myprog.exe (ea *)
.\Myprog.exe (ea *.txt)
Doug Finke
In fact, there's no need to protect against space in file names... so a simple get-childItem -name does the trick
Cédric Rup
+1  A: 

In a .CMD or .BAT file:

SETLOCAL EnableDelayedExpansion
FOR %%f in (*) DO SET params=!params! "%%f"
CALL myprog %params%

The SETLOCAL EnableDelayedExpansion and !variable! syntax allow you to accumulate the results. (See this page for explanation and examples.)

Edit: Added quotes around the %%f, and changed the listing to be '*'... see comments below.

ewall
+1 but you should wrap `!params!` in double quotes to protect from spaces in file names
Frank Bollack
@Frank: Not at all. You should put `%%f` into quotes. A file list of `""""1.txt" 2.txt" 3.txt" 4.txt" 5.txt` would be a bit strange. ewall: It's enough to use `*` as a wildcard.
Joey
@Johannes I left in the *.* as a simple way of avoiding listing subdirectories... but I should've mentioned the assumption that filenames have . extentions and subfolders don't. Thanks!
ewall
@Johannes: Thanks, thats what I meant, but sometimes things get twisted around in my head
Frank Bollack