0

I am working with an older proprietary program where the only way to load the ability to run that program's proprietary commands in the /bin directory is to called a .bat file in cmd.exe, which sets a a variety of variables to then run the commands inside of the /bin directory.

I'm importing various file formats to a list full of components to run inside those proprietary commands, however, the .bat file must be called first and the proprietary commands run directly after in the same cmd.exe session, then close out.

I'm attempting to use subprocess.run to pass a list of commands, but I believe it only allows one command at a time, and can't find a way to load this .bat file and then pass commands into cmd.exe directly afterwards. This is a representative example of what I'm trying:

import subprocess

start_bat_env = 'C:\\Temp\\env_start.bat'

list1 = ['component1A', 'component1B']
list2 = ['component2A', 'component2B', 'component2C']

args = []
args.append(start_bat_env)

for component1 in list1:
    args.append('proprietary code ' + component1)

for component2 in list2:
    args.append('proprietary code ' + component2)

subprocess.run(args)

Is there a proper way to do this using the subprocess or os libraries to keep one cmd session open loading the .bat and passing everything in directly after?

  • You can take a look at https://stackoverflow.com/questions/39721924/how-to-run-multiple-commands-synchronously-from-one-subprocess-popen-command Here they feed the input commands one by one, starting up `cmd.exe` as a subprocess itself. – Icebreaker454 Feb 01 '21 at 16:44
  • I'm not understanding exactly what your issue is, so here's my best guess. Are you wanting to open cmd.exe, and then `Call` your batch file, then run additional commands, also in that same cmd.exe session. If so, the methodology is to use those steps, something like `import os` followed by `os.system("cmd /C \"Call C:\\Temp\\env_start.bat && NextCommandHere\"")`. – Compo Feb 01 '21 at 16:56
  • Sorry for the confusion. It's not just the environment variables in the bat, it's the fact that it calls another bat file, which calls .xml files, and so on. It would be fairly difficult to decompose where each lead, and how many. And yes, running cmd.exe, passing the .bat, and then a varying amount of list components each time, and closing the cmd.exe is the process I'm looking for. 1 - cmd.exe 2 - bat file - keep cmd.exe open 3 - For each list object, run proprietary command 4 - Close cmd.exe once finished with all commands – falconmin20 Feb 01 '21 at 17:01
  • I've attempted appending with the ampersands at the start of the proprietary command to break the commands up as listed in my statement above as well, so args list would equal ['C:\\Temp\\env_start.bat', '&& command1'], for example, however the interpreter gives a '&& was unexpected at this time' statement. I'm not really sure what I'm missing to add into the for-each statement to allow each command to run successfully in a list after the .bat. – falconmin20 Feb 01 '21 at 17:33

0 Answers0