循环中具有流程优先级的启动流程无法识别变量 [英] Start-Process with Process Priority in Loop doesn't recognize variable

查看:68
本文介绍了循环中具有流程优先级的启动流程无法识别变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PowerShell中使用 FFmpeg .

I'm using FFmpeg with PowerShell.

我有一个循环,遍历一个mpg文件的文件夹,并将名称捕获到变量$inputName.

I have a loop that goes through a folder of mpg files and grabs the names to a variable $inputName.

FFmpeg然后将每个转换为mp4.

FFmpeg then converts each one to an mp4.

批处理

$files = Get-ChildItem "C:\Path\" -Filter *.mpg; 

foreach ($f in $files) {

    $inputName = $f.Name; #name + extension
    $outputName = (Get-Item $inputName).Basename; #name only

    ffmpeg -y -i "C:\Users\Matt\Videos\$inputName" -c:v libx264 -crf 25 "C:\Users\Matt\Videos\$outputName.mp4"
}


不起作用

具有流程优先级的批处理


Not Working

Batch Processing with Process Priority

$files = Get-ChildItem "C:\Path\" -Filter *.mpg; 

foreach ($f in $files) {

    $inputName = $f.Name; #name + extension
    $outputName = (Get-Item $inputName).Basename; #name only

    ($Process = Start-Process ffmpeg -NoNewWindow -ArgumentList '-y -i "C:\Users\Matt\Videos\$inputName" -c:v libx264 -crf 25 "C:\Users\Matt\Videos\$outputName.mp4"' -PassThru).PriorityClass = [System.Diagnostics.ProcessPriorityClass]::AboveNormal;
    Wait-Process -Id $Process.id 

}

如果我使用Start-Process PriorityClass设置了进程优先级",则$inputName变量将不再被识别.

If I set the Process Priority using Start-Process PriorityClass, the $inputName variable is no longer recognized.

Error:
C:\Users\Matt\Videos\$inputName: No such file or directory

推荐答案

让我们讨论一些基本的东西.

Lets go over a few basic things.

在powershell中,我们喜欢管道|,它允许用户将信息从一个命令传递到另一个命令.

In powershell we love piping |, It allows use to pass the information from one command to another command.

一个很好的例子就是您拥有的ForEach.

A good example of this is the ForEach you have.

您可以代替Foreach($F in $Files),将|用管道传输到foreach-object

Instead of Foreach($F in $Files) you can pipe | into a foreach-object

Get-ChildItem "C:\Path\" -Filter *.mpg | Foreach-Object{
    $_
}

管道|时,命令powershell将自动创建变量$_,该变量是在管道|中传递的对象

When Piping | a command powershell automatically creates the variable $_ which is the object that is passed in the pipe |

接下来的事情是报价"'有两种类型.

The next thing is there are 2 types of quotes " and '.

如果使用',则一切都是字面意义.例子

If you use ' then everthing is taken literally. Example

$FirstName = "TestName"
'Hey There $FirstName'

会回来

Hey There $FirstName

"允许您在其中使用变量.例子

While " allows you to use Variables in it. Example

$FirstName = "TestName"
'Hey There $FirstName'

会回来

Hey There TestName

现在,在解决此问题之前,还有最后一件事.在powershell中,我们有一个转义字符,也称为"tick".它位于带代字号的键盘上数字1旁边.您可以使用它来允许使用char,否则这些char会超出qoutes的范围.例子

Now one last thing before we fix this. In powershell we have a escape char ` aka a tick. Its located beside the number 1 on the keyboard with the tilde. You use it to allow the use of char that would otherwise break out of the qoutes. Example

"`"Hey There`""

会退货

"Hey There"

好的,现在我们已经涵盖了基础知识,可以修复脚本

OK so now that we covered the basics lets fix up the script

Get-ChildItem "C:\Users\Matt\Videos\" -Filter *.mpg -File | Foreach-Object{
    ($Process = Start-Process ffmpeg -NoNewWindow -ArgumentList "-y -i `"$($_.FullName)`" -c:v libx264 -crf 25 `"C:\Users\Matt\Videos\$($_.Name)`"" -PassThru).PriorityClass = [System.Diagnostics.ProcessPriorityClass]::AboveNormal;
    Try{
        Wait-Process -Id $Process.id
    }catch{
    }
}

在上述情况下,我改变了

In the case above I changed

Get-ChildItem中添加-File以表示只希望返回文件而不是文件夹

Add -File to the Get-ChildItem to designate that you only want Files returned not folders

|插入Foreach-Object

-ArgumentList中的外部括号更改为双引号"而不是文字引号'

Changed the Outside Brackets in the -ArgumentList to be double quotes " instead of literal quotes '

删除了$InputName$OutputName,取而代之的是Foreach-Object变量$_

Removed the $InputName and $OutputName in favor of the Foreach-Object variable $_

这篇关于循环中具有流程优先级的启动流程无法识别变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆