我有一个保存在变量$command
中的命令,类似这样$command = path\to\.exe
$command
具有参数-f
,它表示文件的路径。可以在同一行中多次重复此参数,以在多个文件上执行命令,而不必每次在每个文件上执行命令时都重新加载必要的模型。
示例:如果我有3个文件,则需要在其上运行命令,然后可以像这样执行它:
& $command -f 'file1' -f 'file2' -f 'file3' -other_params
[我想知道如果我有100个文件,那么在PowerShell中是否可以执行任何操作,因为我显然不能尝试手动传递100个参数。
如果我理解您的问题,这是一种方法:
$fileList = @(
"File 1"
"File 2"
"File 3"
"File 4"
)
$argList = New-Object Collections.Generic.List[String]
$fileList | ForEach-Object {
$argList.Add("-f")
$argList.Add("{0}" -f $_)
}
$OFS = " "
& $command $argList
在此示例中,传递给$command
的命令行参数将为:
-f "File 1" -f "File 2" -f "File 3" -f "File 4"
# Open-ended array of input file names.
$files = 'file1', 'file2', 'file3'
& $command ($files.ForEach({ '-f', $_ })) -other_params