Windows Shell Environment

I know it’s rare I actually ask a question, but Windows is harassing me, so I could use a little help.

I’ve got a Shell object that I need to execute some commands which contain environmental variables. The first few commands I issue setup the variables, the subsequent commands use those variables. Problem is, each command seems to be executed inside its own environment, even though they’re all executing on the same Shell object. I’ve tried all three execution modes, but it makes no difference. I’ve also tried packing everything into a single Execute statement separated by &, but still no luck.

Is there anything I can do about this? My last resort is to have my app build out a batch file and run that, but I’d rather use that only as a last resort.

what about executing a batch file with all the command??

Per my last paragraph, that’s my backup plan. I’d rather figure out what is actually going on though.

When you use Mode 2, you should Execute “cmd.exe /q”, and then Write each command.

Note that you can set the environment variables before you start the shell with

system.EnvironmentVariable("variable") = "value"

The values will be accessible in the shell via %variable%.

And if each command is self-contained, and just needs the environment variables, you can use Mode 0 on the shell.

Awesome, I’ll try those tomorrow morning. I read about cmd.exe, but didn’t know about the /q switch.

/q puts it into “quiet” mode - it won’t echo the command, you just get the results. You do still get the command prompt after every command, though.

So you execute cmd.exe and use Read/Write to interact with it?

Yes, for interactive mode. However, from your original description, I suspect you can execute each command individually and not need the complexity of interactive mode. Use System.EnvironmentVariable to set up the environment and fire away.

I’ll try that first.

Ok, I opted for the batch file. I really need the commands to be part of one session, so mode 0 just won’t work. I began implementing in interactive mode, but I don’t feel confident enough that I can detect the incoming prompt signifying that it is okay to move on.

From the Better-Late-Than-Never department. Permanently setting a Windows environment variable. Code by other people ( I don’t have the brains for this sort of thing). It seems to work.

[code]’ You have to run this as administrator since you are writing to the registry
’ You probably need to check if the key already exists too
’ This survives reboots so you only need to ever run it once
Dim r as new RegistryItem(“HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment”,True)
Dim results as Integer
Dim c as new shell

r = new RegistryItem(“HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment”)
r.value(“MYPATH”) =“C:\MyFolder”

’ This declare by Aaron Ballman
Declare Sub SendMessageTimeoutA Lib “user32” ( hwnd as Integer, _
msg as Integer, wParam as Integer, lParam as CString, _
flags as Integer, timeout as Integer, ByRef retVal as Integer )

Const HWND_BROADCAST = &hffff
Const WM_SETTINGCHANGE = &h001A
Const SMTO_ABORTIFHUNG = &h0002

SendMessageTimeoutA( HWND_BROADCAST, WM_SETTINGCHANGE, 0, “Environment”, SMTO_ABORTIFHUNG, 5000, results )

c.Execute “SET MYPATH”
TextArea1.text=c.result ’ You obviously need a textarea or something
[/code]