首页 > 解决方案 > 如何启动 .bat 文件以与 Powershell 分开运行(2pt 问题的 pt1)

问题描述

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

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

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

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

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

os.system.....

Start-Process....

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

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

我不想更改 .bat 文件,因为它工作得很好,而且不仅仅是打开 .max 文件。我只需要它在 CMD 或任何 shell 之外正常打开文件。

提前致谢

(编辑).bat 文件代码如下..

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

标签: powershellbatch-filecmd3dsmax

解决方案


我不知道你为什么想要 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参数,因此它将运行输入路径及其子文件夹中的每个文件。如果您只想为输入的文件夹路径(不包括子文件夹)中的文件运行它,只需/r.bat脚本中删除即可。


推荐阅读