如何与 Powershell 分开启动 .bat 文件到 运行(2pt 问题的第 1 部分)

How to launch a .bat file to run separately from Powershell (pt1 of a 2pt problem)

我有一个 .bat 文件,该文件将位于一个文件夹中,并在双击时打开特定扩展名的所有文件(在本例中为 3dsMax)。

我想为 powershell(或可能 python)创建一个 GUI,当打开时,我可以插入文件的位置并有一个启动按钮该位置的 .bat 文件。

我已经尝试了很多不同的代码,我在这里和其他网站上找到了,例如:

import subprocess
subprocess.call.......

import subprocess
subprocess.Popen.......

os.system.....

Start-Process....

甚至只是 .bat 文件的位置。

我最接近的是制作 .bat 文件 运行,但它会在 Power shell 中的所有加载过程中打开 3dsmax 文件 运行或命令。 然后 3dsmax 尝试打开 UI,我最终得到 400 多个版本的 max 试图打开并且它崩溃了。 (编辑 - 它试图打开的一个文件的 400 多个版本)。

我不想更改 .bat 文件,因为它运行良好,而且不仅仅是打开 .max 文件。 我只需要它在 CMD 或任何 shell.

之外正常打开文件

提前致谢

(编辑)下面的 .bat 文件代码..

for /r %%v in (*.max) do (
  start "" "C:\Program Files\Autodeskds Max 2018dsmax.exe" -u MAXScript Wire_colorizer_0.ms %%v 
)

我不知道你为什么想要 A 和 B 字段的解决方案,但是你去吧:

function startBAT() {
    $STARTButton.Enabled = $false #disable START button, so u won't accidentally run it again while executing
    Set-Location "C:\something$($TextBox.text)$($TextBox2.text)" #textbox.text = field A, textbox2.text = field B
    Start-Process "C:\something$($TextBox.text)$($TextBox2.text)\name.bat"
    $STARTButton.Enabled = $true #enable START button after done
    $form.Close() #close GUI immediately after execution, delete this line if you dont want to
}

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '500,230'
$Form.text = "Lunch .bat file from location"
$Form.TopMost = $false
$Form.StartPosition = 'CenterScreen'
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false

$Label = New-Object system.Windows.Forms.Label
$Label.text = "Field A:"
$Label.AutoSize = $true
$Label.location = New-Object System.Drawing.Point(50,30)
$Label.size = New-Object System.Drawing.Size(50,50)
$Label.Font = 'Microsoft Sans Serif,10'

$TextBox = New-Object system.Windows.Forms.TextBox
$TextBox.multiline = $false
$TextBox.location = New-Object System.Drawing.Point(50,50)
$TextBox.size = New-Object System.Drawing.Size(400,50)
$TextBox.Font = 'Microsoft Sans Serif,10'

$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Field B:"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(50,90)
$Label2.size = New-Object System.Drawing.Size(50,80)
$Label2.Font = 'Microsoft Sans Serif,10'

$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline = $false
$TextBox2.location = New-Object System.Drawing.Point(50,110)
$TextBox2.size = New-Object System.Drawing.Size(400,50)
$TextBox2.Font = 'Microsoft Sans Serif,10'

$STARTButton = New-Object System.Windows.Forms.Button
$STARTButton.Location = New-Object System.Drawing.Point(200,150)
$STARTButton.Size = New-Object System.Drawing.Size(100,50)
$STARTButton.Text = 'START'
$STARTButton.Add_Click({startBAT}) 

$Form.controls.AddRange(@($Label, $TextBox,$Label2, $TextBox2, $STARTButton))

$Form.ShowDialog()

同样在您的批处理脚本中,您使用了 /r 参数,因此它将 运行 输入路径及其子文件夹中的每个文件。如果你想 运行 它只用于输入的文件夹路径中的文件(没有子文件夹),只需从你的 .bat 脚本中删除 /r