Actually i want to run 2 bat scripts , first script will set a system variable set NEWPATH="E:/Some"
and second script will show the path of that variable: echo %NEWPATH%
. this is not working with the same server at first time , when i restart server that will show the path otherwise it'll show nothing. so can anyone help me on that?
Use environment variable set by a batch script in the next batch script run
5.1k views Asked by shagul At
1
There are 1 answers
Related Questions in BATCH-FILE
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
Related Questions in GO
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
Related Questions in ENVIRONMENT-VARIABLES
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
Related Questions in WINDOWS-SCRIPTING
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
I don't fully grasp your problem, but here's a couple of observations.
Some theory
Environment vaiables set in a batch file (which is being executed by a shell process,
cmd.exe
)—or any other kind of process—can only be set for that very process.That is, each process has a special block with environment variables made available to it by the OS when that process is created.
This also means that when the process which set an environment variable is finished, it's environment block is gone.
An environment variable can be inherited by a process which was started by the process which has set that environment variable.
What happens in your case
What this means is that if you run two batch scripts in a sequence, this means
The first
cmd.exe
process is strated, the batch script it executed set an environment variable; then the process is gone, and its environment block is gone, too.Then another
cmd.exe
is started, it inherits the environment block of your host process (written Go), but as you can see, whatever is set by a first batch script is not available.What you can do about it
There are two possible ways to solve the problem:
Make the first batch script call the second one by itself.
In this case the second
cmd.exe
will inherit the environment variables set by the first script and will hence "see" them.Note that the Windows batch command language supports the
calls
command to call out to other batch scripts by their pathnames.Make the first script communicate the value of that variable to your host process and then arrange in the host process for the second
cmd.exe
to have the indicated variable with the indicated value in its environment.Say, the first script could just do something like
to have
printed to its stdout.
Your Go process could parse that output, tear it apart on the
=
character, sanitize to not affect "interesting" variables likePATH
,USERPROFILE
etc, and then the host process could doThe second case can be done a bit differently: the host process can call
os.Setenv("VARNAME=value")
to make this variable appear in its own environment block; it then will be inherited automatically by whatever processes it starts after that.Update to address the OP's comment
There's another approach which might work in your case.
The idea is that
cmd.exe
is just a shell: when it's started non-interactively (that's what is done byexec.Command("cmd.exe")
) it reads commands from its standard input stream and executes them one by one—until the stream is closed (by the sender).Hence you can do the following:
Start
cmd.exe
while connecting an instance ofio.Pipe
to itsStdin
.Open the first script yourself and shovel it at the running
cmd.exe
via the pipe set up on the first step.Don't forget to
Flush()
the pipe.You can use the
io.Copy()
function which sends all the data from an openedio.Reader
to someio.Writer
.Leave the shell instance running.
When it's time, read the second script and shovel it at the same shell.
Since the shell is the same, the second script would run as if it were physically appended to the first one, and will see all the variables set by the first script.