Search code examples
powershellbatch-filecmd3dsmax

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


I have a .bat file that will sit in a folder and open all files of a certain extension when double clicked on (in this case 3dsMax).

I want to create a GUI for powershell (or possibly python) that when opened, I can insert the location of the file and have a button that launches the .bat file in that location.

I have tried many different codes I have found on here and other site such as:

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

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

os.system.....

Start-Process....

Even just the location of the .bat file.

The closest I have got is to make the .bat file run, but it opens the 3dsmax file running through all the load process in Powershell or CMD. Then 3dsmax tries to open the UI and I end up with 400+ versions of max trying to open and it crashes. (Edit - 400+ versions of just the one file it's trying to open).

I don't want to change the .bat file as it works nicely and does more than just open the .max file. I just need it to open the file normally outside of CMD or any shell.

Thanks in advance

(Edit) .bat file code below..

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

Solution

  • I don't know why you want solution with A and B field, but there you go:

    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()
    

    Also in your batch scritpt, you are using /r parameter, so it will run every file in entered path and its subfolders. If you want to run it only for files in entered folder path (without subfolders), just delete /r from your .bat script.