There are two approaches that would work:
- pass the values on the command-line (requires the .CMD script to parse the command line—tedious, IMHO)
- pass the values via the environment (requires the .CMD script to see if the vars are defined before asking for them)
A sample implementation of the latter could be:
from subprocess import Popen
import os
command = [
'mybatch.cmd'
]
params_by_env = {}
# "inherit" the current environment
for k,v in os.environ.items(): params_by_env[k] = v
# Specify the envvars for mybatch.cmd
params_by_env['USER_NAME'] = 'billg'
params_by_env['PASSWORD'] = 'killfree'
params_by_env['AGE'] = '60'
params_by_env['PASSEWORD'] = 'pa$$ew0rd'
proc = Popen(command,env=params_by_env)
proc.wait()
print "Finished"
A modified version of your .CMD script could look like this:
@SETLOCAL
@ECHO OFF
:Start
IF "%USER_NAME%"=="" SET /P USER_NAME=Enter user name:
IF "%PASSWORD%"=="" SET /P PASSWORD=Enter Password:
IF "%AGE%"=="" SET /P AGE=Enter age:
IF "%PASSEWORD%"=="" SET /p PASSEWORD=Enter Passeword:
@ECHO Username: %USER_NAME%
@ECHO Password: %PASSWORD%
@ECHO Age: %AGE%
@ECHO Passeword: %PASSEWORD%
Note the alterations to the SET /P commands, only executing them if the environment variable isn't set and assigning the value to the environment variable. Also note that USERNAME is a variable that already exists in Windows environments, so you wouldn't want to use it for this purpose. That's why I chose USER_NAME instead.
Security Caveat: It is generally considered to be a Bad Thing™ to put passwords on the command line (or in the environment—both of which are easily accessible to other programs running on the machine). Sometimes it must be done, but it should be avoided if possible. In the case of this program, you could have the Python script send input to the cmd script (proc.communicate(), I think, and SET /P in the .CMD script) to provide the password. There are other, even better, ways, but they are sometimes cumbersome to implement and not perfectly secure either.