Page 1 of 1

evaluate a program's output as a command

Posted: 26 Oct 2007 09:47
by zackyh
what i want to do it write a program that can dynamically load and unload a command prompts(cmd.exe) environment variables( these environment variables are not system or user wide variables, is a process environment variable.see more about environment variables), so that each time user can open a customized cmd.exe and run my program to dynamically load and unload specific environment varialbes, when the command prompt is closed all the environment variables will be lose, so no matter what user did in the command prompt(cmd.exe) session, it will not affect system/user wide environment variable.

when user open the command prompt(cmd.exe), they can use set command to set process wide environment variables which will lose when the cmd.exe is close( this is what i need), however i can't tell user to use millions of set command to set environment variable themself, so i want to write a program that can do something like: envload.exe load perl-5.8(this will set the PATH to perl5.8 directory), and when they want to switch to use perl5.7, i will need something like: envload.exe unload perl-5.8, then envload.exe load perl-5.7 (these commands will take perl5.8 of path environment variable, and add the path toperl5.7 to the PATH environment variable).

the problem is when my program is execute in a command prompt(cmd.exe), the system will say my program is a child of command prompt(cmd.exe), because for some security reason, a child process can not modify its parents environment variables. and my method of get around with this problem is let my program write a batch file and output its path, and then use script to take the output and execute it in the current command prompt session. so i like to do in my program is:

run some code with user's command like arguments
make a new batch file mainly with SET command in it ( something like: SET PATH=%PATH%;NEW/PATH; etc)
output the path of this batch file
execute this file in the current command prompt(cmd.exe)

my program now can generate a batch file, and i need some kind of script to glue all these steps together, because i don't want the users to execute the my program first and then run the batch file. i just want something that glue this 2 part together?


i hope someone can understand me.

Posted: 29 Oct 2007 03:24
by zackyh
is it no one understand what the **** am i talking about?

basically i want to port module command to windows
http://modules.sourceforge.net/

but the way module work in linux is by output the environment variable to a shell-script, and then source to current shell.
in the shell script there are mostly set environment commands. because in linux it have eval command in the bash shell, this command allow the shell to evaluate the output of a program and then run the outputs as a command.


what want to do the similar thing in windows, output a batch script(contain set command) and then use cmd.exe to execute it, what i need is a batch script to glue these two things together?

Posted: 29 Oct 2007 05:01
by bsod
If I understand it correctly, You want to be using 'START' , call the batch file that sets the environment, use /k to remain in command prompt,
JOB DONE
The environment variables will be lost when the window is closed.

Posted: 29 Oct 2007 07:42
by bsod
Sorry, Should have said, something like this,

start cmd /k yourbatch.bat

which sets environment variables then executes whatever it is you want to run.

Posted: 30 Oct 2007 03:50
by zackyh
thanks bsod, that's exactly what i want. doskey also used to execute one and command than another one,
something like
doskey myprogram.exe = myprogram.exe$touput_batch.bat
and then
cmd /k startupbatch.bat

the startupbatch which will give user to customized their sessions, so they can load some necessary module when they startup.