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
- .bat file - How can I return the value of a variable whose name depends on another variable concatenated with a string in a batch file?
- Discordbot(Python) who should start bat file(Minecraft server) can't find user_jvm_args.txt file
- Set req query output to a variable
- bat file creates a "corrupt" zip
- How to list several items in the dialog box for execution?
- "if contains" with forbidden special characters
- Overlaying frame number with ffmpeg
- Batch Script-Powershell MessageBox | How do I set TopMost within PS command line of Batch?
- Batch file no longer works correctly in Windows 11
- Trying to launch batch file from powershell, and immediately closes
- How to automate an SSH login with a batch file?
- Having trouble executing my program from a jar, using Jinput
- How can I unload Visual Studio projects via batch file/developer command prompt?
- How to use goto in nested loop in .bat script window
- How can I run this Powershell function from a batch file on windows?
Related Questions in GO
- Go Fiber and HTMX - HX-Trigger header is changed to Hx-Trigger, which is not what HTMX is listening for
- Golang == Error: OCI runtime create failed: unable to start container process: exec: "./bin": stat ./bin: no such file or directory: unknown
- Handling both JSON and form values in POST request body with unknown values in Golang
- invalid transaction: Transaction failed to sanitize accounts offsets correctly
- Golang lambda upload image into s3 static website
- Is there a way to get a list of selected module versions, but only for modules within the pruned graph?
- Save Interface in DB golang
- ERROR: column "country" is of type text[] but expression is of type record (SQLSTATE 42804)
- Trying to update the version.go file with the release tag from GitHub actions but its failing
- How can I optimize this transposition table for connect 4 AI?
- const declaration - How to evaluate expressions at compile time?
- How add array of authors for unique user in database in Goland IDE?
- Why is the main goroutine not blocked after write in unbuffered channel?
- Insert & Retrieve from a channel in same main function throws "goroutine 1 [chan receive]: main.main() /path exit status 2" error
- Gob error when decoding array of structs: decoding into local type but received remote type
Related Questions in ENVIRONMENT-VARIABLES
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Windows environment variables at appsettings.json
- API key 401 error in .env.development file
- Vite TypeError: Cannot read properties of undefined (reading 'VITE_YOUTUBE_API_KEY')
- Set environment variable during push for GitHub Actions
- Is there a way to read .csproj PropertyGroup variable in c#
- My environment variables are not recognized in Azure - ASP.NET Core MVC
- Is it best to declare global variables in a header file or environment file in a C project
- NextJS public environment variables are not recognized in my elastic beanstalk
- Shell script for copying environment variables into config.json?
- Locally testing OS environmental variables in Ballerina
- Is using NEXT_PUBLIC for my secret key is a dangerous way to use environment variables when I deploy on vercel?
- can't add another folder path in environment variables
- How to use ProxyAgent with http_proxy and no_proxy environment variables
- New values not being loaded for .env for SvelteKit app
Related Questions in WINDOWS-SCRIPTING
- Powershell script to close specific user share drive session
- PowerShell Script to Replace non-HTML Tags
- search a folder recursively to list the file names which contains the string windows
- Logon scripts are running with admin powershell by default can this be changed?
- Microsoft Excel cannot paste the data. FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
- How to automatically set the Application Pool Identity, User Name & Password
- Printing borderless with PowerShell script
- Remove folder contained in different folders
- How can I create backup script for selected tables in Oracle
- "Object required" - path string unrecognized in windows scripting file
- which script dont need any software setup or config
- Create Directory and Copy files from different directory (shared location) in production using Windows batch scripting
- Getting PowerShell Script Location at Run Time in PowerShell Command line
- Rename all file in a folder with .BAT code
- Needing to read data from Excel and output it into EMS 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.