how to get SetEnvironmentVariable in batch script

Windows specific forum
plouf
Enthusiast
Enthusiast
Posts: 255
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

how to get SetEnvironmentVariable in batch script

Post 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
Christos
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: how to get SetEnvironmentVariable in batch script

Post 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.
bye,
Daniel
plouf
Enthusiast
Enthusiast
Posts: 255
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Re: how to get SetEnvironmentVariable in batch script

Post 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%
Christos
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: how to get SetEnvironmentVariable in batch script

Post 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.
bye,
Daniel
Axolotl
Enthusiast
Enthusiast
Posts: 446
Joined: Wed Dec 31, 2008 3:36 pm

Re: how to get SetEnvironmentVariable in batch script

Post 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 
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
Post Reply