在递归文件夹中运行exe

时间:2015-11-21 17:43:11

标签: powershell windows-7

Set-ExecutionPolicy Unrestricted
$start_path ="D:\VST\"
$start_path> Get-ChildItem-Recurse |
foreach { cd $_.DirectoryName; "VST_Screenshot_Tool"; cd ..; }

这应该在VST_Screenshot_Tool.exe的根和所有子文件夹中运行$start_path。我收到这个错误:

Expressions are only allowed as the first element of a pipeline.
At C:\Users\pithy\Desktop\screenshotter.ps1:2 char:13
+ $start_path  <<<< ="D:\ZZ_AUDIO\VST etc\__ARCHIVE\*" |
    + CategoryInfo          : ParserError: (:) [],       ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

任何指针都会非常感激。

1 个答案:

答案 0 :(得分:2)

$start_path> Get-ChildItem-Recurse会将字符串D:\VST\写入当前目录中的文件Get-ChildItem-Recurse。此外,您需要调用操作符(&)来执行命令字符串,如果要运行外部命令,则应包括扩展名。如果没有运算符,PowerShell将只是回显字符串。

将您的代码更改为:

$start_path = 'D:\VST'

Get-ChildItem $start_path -Recurse -Directory | ForEach-Object {
  Set-Location $_.FullName
  & "VST_Screenshot_Tool.exe"
}

在PowerShell v2及更早版本中,您需要替换-Directory参数,如下所示:

Get-ChildItem $start_path -Recurse | Where-Object {
  $_.PSIsContainer
} | ForEach-Object {
  Set-Location $_.FullName
  & "VST_Screenshot_Tool.exe"
}