I'm creating a script that is to create a variable called http_proxy. The script does a bit more than just set the proxy, it has a few statements in there as well as a prompt for user password on load.
I have set up a shortcut to cmd.exe with an extra parameter /k ".set_http_proxy.bat" to run on startup, which sets this variable.
Once the script exits, the command prompt remains open for the user to run their scripts. My problem is, the variable http_proxy has vanished now and no trace that it was set in the script that just ran.
Is there a way to set a variable that will remain in use for that session until the command prompt window is closed? I think in bash we just use export which is great!
current code is simply...
set http_proxy=http://proxy.address
If that's all, then it should work exactly as you expect, and in fact it did so for me when I tried it out.
Unless you use setlocal or launch another process for running a batch file, then environment variables persist even after the batch file finishes.
Related
I'm trying to start a powershell instance, that loads a script and remains open so I can still call methods loaded by that script manually.
I'm trying to dot source a script and pipe it to powershell like below, from a cmd instance/batchfile:
echo . .\script.ps1 | powershell
The result in this case is that powershell starts, loads my script, executes it and exits. I've tried running with -noexit argument, it has no effect.
I'm thinking of another option, to start a powershell process and pipe my dot source command to its stdin - but this probably won't allow me to interact with the process anymore because its stdin is opened by the host process.
If you need to run a script file so that window stays open and variables are accessible after the execution.
Try dot sourcing the script file like this:
powershell -noexit ". .\script.ps1"
Once the script is done, you can access any internal variable the script defined. Assuming the variables are at the script level scope.
I have two scripts which are to be executed and i am using Windows batch script for automating of running the scripts.
I have to read 10 user input values and then run the two scripts using those parameters.
I am successful in executing the first script and failing with the second script.
Issue is that the cmd prompt exits after completing the first script.
How to make cmd prompt run the second script as well using the input parameters.
Any Help on this??
Thanks in Advance.
You have two options to execute a batch script from you code. It's either START or CALL:
START will execute your code in it's own variable scope, means the variables you've set in the first script won't be available. Further both scripts will be executed in parallel and not one after another (unless you use START /WAIT).
CALL on the other hand will do exactly what you need. It will start the first script in the same scope (previously set variables are variable), execute it and afterwards it will run the second sript (also in the same scope).
TL;DR this will work:
...
CALL BatchScript1.bat
CALL BatchScript2.bat
...
If you call scripts from another script you have to use the call command:
#echo off
call first.cmd
call second.bat
echo Here I'm back again !
I want to setup a system wide environment variable within my batch script script running within the CMD prompt, I have been able to achieve this by calling:
setx MyEnvVar "C:\<Some Path>" /M
However when I do:
echo "MyEnvVar is %MyEnvVar%"
afterwards the statement that outputs at prompt is "MyEnvVar is" although the variable has been setup with setx and I can observe it through looking at the Windows - system properties - environment variables GUI.
I know this is because the CMD prompt has to be restarted for it to pick up the new environment variables however I don't want to stop the execution of my batch script and tell the user to manually restart the CMD prompt window and re-run my script so the environment variables are picked up. Is there some other way of getting around this?
It would be better if I could get around this without utilizing the "call" method and breaking the script to two segments
batchfileA - Code up until and including the call to change the environment variables thereafter utilize call method to call batchfileB
batchfileB- The rest of the original code placed inside batchfileB and called with call method within batchfileA
I had tried using set after the setx and echoed the result and the variable was null so I assumed that the value was not taking because I had to restart the CMD prompt what I forgot was I had enabled DelayedExpansion and had to use ! (exclamation marks) instead of % (percent) signs around my variable names
Is it possible to use Install[] to start a MathLink program with a custom PATH environment variable?
I am trying to use mEngine to connect Mathematica to MATLAB on Windows. It only works if mEngine.exe is launched when the PATH environment variable includes the path to the MATLAB libraries. Is it possible to modify the PATH for launching this program only, without needing to modify the system path? Or is there another way to launch mEngine.exe?
#acl's solution to wrap mEngine.exe in a batch file, and temporarily modify the PATH from there, works correctly:
I used this as the contents of mEngine.bat:
set PATH=c:\path\to\matlab\bin\win32;%PATH%
start mEngine.exe %*
*% ensures that all command line arguments are passed on to mEngine.exe
start is necessary to prevent the command window from staying open until mEngine.exe terminates
It can be started using Install["mEngine.bat"].
Since all the information that is needed for the kernel to communicate with mEngine.exe is passed by Install[] as command line arguments, all we need to do is launch mEngine.exe with these arguments. It is not necessary for Install[] to know the location of mEngine.exe, the important thing is that the process gets launched with the correct command line arguments, which is ensured by %*.
For example I can directly call myscript.cmd or in other script I can put a line to myscript.
The reason is that if a script is run on it's own it dissapears as soon as it stop executing, so I can't see the result, so at the end I must add #pause but when I run it from another shell this causes annoyance since console window wouldn't exit that way.
So I look for some kind of 'if' condition to address this issue.
To get your script paused when double-clicked (or by dropping files on it), but terminating the usual way when invoked from console:
#echo off
echo Hello World!
:: ...your ScriptCode...
(((echo.%CMDCMDLINE%)|find /I "%~0")>NUL)&&pause
Unless you create an environment variable like Stu suggested, you're not going to find any that do what you want. You're going to need to write a small program that queries the parent process programmatically and returns a value your script can check. If you're being run from Start->run your parent will be explorer.exe. Otherwise it will be cmd.exe or some other exe.
Sample code to find the parent process can be found here.
Why not set it yourself?
SET RUNNINGFROMOTHERSHELL=YES
CALL MYSCRIPT.CMD
SET RUNNINGFROMOTHERSHELL=
In MyScript.Cmd:
IF "%RUNNINGFROMOTHERSHELL%"=="" GOTO NOPAUSE
PAUSE
:NOPAUSE