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.2k Views Asked by shagul At
1
There are 1 best solutions below
Related Questions in BATCH-FILE
- Get Maximum Log Size
- Running batch fine onclick delay
- Automating Telnet Scripts from .bat with a teamspeak instance
- Variable setting exhibits unexpected behavior when set inside for loop
- Execute a command over multiple files
- How to run multiple "mvn test"-commands from batch file?
- Referencing a Schema's table batch/perl
- Can I use \ and / to be directory delimiter in CLASSPATH of .bat
- SSIS exit code error with batch file
- CMD/BATCH - Had to run batch script 3 times to get full result
- Trying to create a Bath file that will copy a specific directory path (no lower) and zip the directories in that path
- curl command don't work in some cases
- Set environment in current cmd using batch script
- What is the correct way to pass the password to OpenSSL
- Batch file to process files in sub sub directory
Related Questions in GO
- How do I get all the attributes of an XML element using Go?
- Type cast custom types to base types
- Why are Revel optional func parameters in controller not working? CRUD code redundancy
- Streaming commands output progress
- single ampersand between 2 expressions
- golang goroutine use SSHAgent auth doesn't work well and throw some unexpect panic
- How do I do a literal *int64 in Go?
- Emulating `docker run` using the golang docker API
- How to print contents of channel without changing it
- Golang time zone parsing not returning the correct zone on ubuntu server
- Is os.File's Write() threadsafe?
- How to get the pointer of return value from function call?
- How do I represent an Optional String in Go?
- Fibonacci in Go using channels
- Boltdb-key-Value Data Store purely in Go
Related Questions in ENVIRONMENT-VARIABLES
- Best way to pass an environment variable to several config files
- How to expand environment variables in python as bash does?
- How to set environment variables with a forward slash in the key
- What is the meaning of environment variable !::=::\
- iOS launch app with environment variable
- How to set Environment variables permanently in C#
- Blank path in environment variable causing elevated command prompt to not respond
- Rails: ENV variable failure outputs double string
- ActiveMQ: Error while loading shared libraries
- setenv, and getenv documentation
- Trying to set and use environment variables with PHP on Ubuntu 14.04
- SECRET_KEY error with Django using uWSGI
- Import linux environment variable from shell file
- Use Dokku Environment variables in DockerFile
- Changing JS variable with Grunt for different environments
Related Questions in WINDOWS-SCRIPTING
- PsExec fails to open the stdin, stdout and stderr named pipes
- JScript is crashing with a very poor reason given
- Undo the actions performed by a .bat file?
- How can I write "exit" word to txt from bat file?
- Use environment variable set by a batch script in the next batch script run
- How to pipe bash script output to a file on windows
- need to check the version value in text file using vbscript
- Why does this jenkins job never complete?
- Is it possible to assign a logon script to a user in a windows machine which is not a part of domain?
- How to call/ execute .WSF (windows script file) from browser
- Visual Studio Build Agent scripted setup
- When to choose development of a PowerShell Module over PowerShell Script
- Getting Working Processes within IIS App Pools
- Implementing a Windows script
- parse the source safe history output and run the diff command
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)
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?
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.exeprocess 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.exeis 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.exewill inherit the environment variables set by the first script and will hence "see" them.Note that the Windows batch command language supports the
callscommand 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.exeto 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,USERPROFILEetc, 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.exeis 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.exewhile connecting an instance ofio.Pipeto itsStdin.Open the first script yourself and shovel it at the running
cmd.exevia 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.Readerto 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.