【问题标题】:Powershell Script, 2x variable in foreach loopPowershell 脚本,foreach 循环中的 2x 变量
【发布时间】:2022-07-14 22:00:03
【问题描述】:

我快疯了。我不得不承认我是一个典型的复制粘贴非脚本人员,站在这里有一些我无法解决的新问题。我想使用 ocrmypdf.exe,我必须在其中读取 PDF 的网络文件夹并将其放在子文件夹中。

ocrmypdf 工作简单:ocrmypdf.exe

我有 3 个变量,例如:

$source = @(Get-ChildItem -Path 'X:\OCR\*.pdf') # <-- here are my files, filtered for pdfs
$destname = "X:\ocr\done" #destination-folder where the pdf-files should be written in
$destfiles = foreach ($file in $source) {"$destname\$($file.name)"} # <--- destination path + the same source-file-name

当我必须在 Powershell 中运行 command-exe 时,我应该像这样运行它

Foreach ($a in $source)
{

& $command $param

}

其中 $command 和 $param 是(不是)这样的:

$command = 'ocrmypdf.exe'
$param = '$source', '$destfiles'

但我已经知道这是行不通的,因为 foreachloop 不能与我的变量一起使用。

有人可以帮我解决这个问题吗?是的,我现在懒惰地阅读一本 powershell 书,但我还是试试运气:)

提前谢谢你

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以为命令调用提供任意数量的参数:

    $source = @(Get-ChildItem -Path 'X:\OCR\*.pdf')
    $destname = "X:\ocr\done" 
    $command = 'ocrmypdf.exe'
    
    foreach ($file in $source) {
        $sourcePath = $file.FullName
        $destPath   = Join-Path $destname $file.name
    
        # pass both arguments to command
        & $command $sourcePath $destPath
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-17
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      相关资源
      最近更新 更多