Page 1 of 1

how to get SetEnvironmentVariable in batch script

Posted: Sat Apr 06, 2024 6:22 pm
by plouf
HI

wondering how does SetEnvironmentVariable works

i try SetEnvironmentVariable("filename","abc123.txt")

then in a batch used echo %filename% but i get no results out !?

thanx

Re: how to get SetEnvironmentVariable in batch script

Posted: Sat Apr 06, 2024 6:45 pm
by DarkDragon
plouf wrote: Sat Apr 06, 2024 6:22 pm HI

wondering how does SetEnvironmentVariable works

i try SetEnvironmentVariable("filename","abc123.txt")

then in a batch used echo %filename% but i get no results out !?

thanx
Did you start the batch script via RunProgram? Processes inherit environment variables throughout their run hierarchy, so the variables of the parent process are given to the child processes at the beginning.

Re: how to get SetEnvironmentVariable in batch script

Posted: Sat Apr 06, 2024 6:51 pm
by plouf
no i start batch via command line (CMD)
my point is to pass a variable back to a script

Pb progran is just (console mode selected in compiler)

Code: Select all

SetEnvironmentVariable("filename","abc123.txt")
batch is

Code: Select all

@purebasicprog.exe
@echo %filename%

Re: how to get SetEnvironmentVariable in batch script

Posted: Sat Apr 06, 2024 7:39 pm
by DarkDragon
plouf wrote: Sat Apr 06, 2024 6:51 pm no i start batch via command line (CMD)
my point is to pass a variable back to a script

Pb progran is just (console mode selected in compiler)

Code: Select all

SetEnvironmentVariable("filename","abc123.txt")
batch is

Code: Select all

@purebasicprog.exe
@echo %filename%
You cannot pass it back to the parent process. Use the exit code or stdout to communicate to the batch script then.

Re: how to get SetEnvironmentVariable in batch script

Posted: Sat Apr 06, 2024 10:40 pm
by Axolotl
Imho the explanation is correct and there are to possible ways.
1. Make the variable permanent by registry or a tool called setx
2. Create a batch file with @set varname=value and call this batch in your batch file

Maybe this will get you started. (I know, this is no PB code)
The registry keys you should work with are

Code: Select all

HKCU\Environment
or

Code: Select all

HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment


This is where setx stores the definition permanently. Unfortunately you cannot remove a variable with setx, so you have to use the registry like this:
Assuming we want have a variable DEBUG

Code: Select all

; console commands here 
; reg query HKCU\Environment /v DEBUG 
; reg add HKCU\Environment /v DEBUG /d 
; reg delete  HKCU\Environment /v DEBUG /f  
; The operation completed successfully. 
; /f ... forces the deletion