No announcement yet.

Setting environment variable in mixed environment

  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting environment variable in mixed environment

    I'm using PBCC 6.04 on Win 10 Pro 64bit.

    I have a desktop shortcut that starts .BAT file, this bat then calls a PBCC pgm.

    I want to set an environment variable in the PBCC pgm, that is then used in the calling bat.

    I don't seem able to do this, I've tried:
    1. ENVIRON
    2. using SHELL to issue a SET cmd
    3. using SHELL to issue a SETX cmd
    none of these seem to work.

    If I start CMD manually, run the bat manually, option 2. works. But it doesn't work when if I use a shortcut.

    Any help would be greatly appreciated.

    Currently I write a one line text file and extract the info in the bat file
    using ... for /f "tokens=1" %%a in (%xoutput%) do (set xinfo=%%a)
    or ... set /p xinfo=<%xfile%

  • #2
    What I do is create a temporary batch file in the PB app, and then call the batch when the program exits, something like:
     :: run my program that will set the environment variable JIMBO_INFO
     CALL ~TMP_JIMBO.bat
     echo Jimbo_Info is %JIMBO_INFO%
     del ~TMP_JIMBO.bat
    I started doing this years ago after not finding any consistent way of setting environment variables in a batch via PB programs.

    I'd be happy if someone has a better solution to share. But, like someone here has said before, "Sometimes, good enough is good enough."
    ... .... . ... . . ... ... .... . .. ... .... .. ... .... .. .... ..

    n6jah @


    • #3
      To make a long story short, batch file where designed this way...

        OS Open batch file
        OS Read line
        OS Compute and memorize new pointer offset
        OS Close batch file
        OS Execute content of the line
      WHILE pointer offset < file length 'No more to read
      You can take advantage of this, it mean that a batch file can be modified while running, the rule is that internal pointer offset must be respected.

      In pb...
      #COMPILE EXE "SetEnvTime.exe"
      #DIM ALL
      #INCLUDE ""
      '*  Batch file named "OnTime.bat"        *
      '*                                       *
      '*  @Echo off                            *
      '*  SetEnvTime.exe                       *
      '*  Set EnvTime=                         *
      '*  @Echo EnvTime was set to %EnvTime%   *
      '*  @Pause                               *
      '*                                       *
       LOCAL sFileText      AS STRING
       LOCAL EnvironmentPos AS DWORD
       OPEN "OnTime.bat" FOR BINARY AS #1
       GET$ #1, LOF(#1), sFileText                       'Read whole file
       EnvironmentPos = INSTR(sFileText, "Set EnvTime=") 'Search for sEnvironment
       IF EnvironmentPos THEN _
         sFileText = LEFT$(sFileText, EnvironmentPos + 11) & TIME$ & _ 'Update the EnvTime  line 
                     MID$(sFileText, INSTR(EnvironmentPos, sFileText, $CRLF))
       SEEK #1, 1         'ReSet pointer to the start of file
       PUT$ #1, sFileText 'Write updated file
       CLOSE #1           'Tell the OS where done
      Last edited by Pierre Bellisle; 14 Feb 2019, 08:19 AM.


      • #4
        Jim, Pierre,

        Many thanks for the suggestions, I will try them shortly, when I have some time to spare.

        Again, thanks,


        • #5
          Many years ago I did a lot of experimenting with environments. IIRC, there were at least 3: the ENV for the current process, the ENV of its caller/parent, and the "master" or "root" ENV...
          DOS maintained a "chain" of them, whereby you could walk the chain up to the root. I wrote a handful of little utilities that enabled reporting on each level, as well as setting environment variables in whichever level was desired.

          I have two ZIP files that contain all the files I could identify that pertained to this effort, source and executables. If you're interested, I can email them to you. Just send me a PM on this forum.

          I used Spontaneous Assembler for some x86 source code, and I remember that I was very new to naming files for later recognition and retrieval, so I may have to help you trace which sources generated which EXEs...

          The PB sources are all for PBDOS, and contain snippets from all over the internet, books, etc. (Attributed fairly well.) Again, I was new to it all, and my code is pretty ugly. However, the programs worked well on (16-bit) Windows!

          Let me know if there's any interest.



          • #6
            If you always start your program inside of a batch file then you could make a shortcut to it and if the program is a console program, then you would not have to worry about having a console on the screen while a GUI program was running and even then, you can do as I do and hide the console screen.
            With all that said and your starting your program by a batch process. You could have Call a batch file after your program ran to set the environmental variable.
            Your code in the batch file might go something like this.
            if exist setvatable.bat del setvarisbke.bat
            if exist setvarisble.bat call setvariable.bat
            if exist setvariable.bat del setvariable.bat

            p purvis


            • #7
              In the starting batch file.
              You can set the Called batch file name to an environmental variable as well to something unique and have your program read the environmental variable containing the file and location for your program to create before it exits
              p purvis


              • #8
                Many thanks to everyone who has responded and suggested solutions to my issue.

                After much experimentation I've decided on the following solution, as it fits the simplistic way I think.

                1. I have a very small .INC subroutine that accepts a single string.
                The string has 1-n environment variable values, semi-colon delimited, in the format:
                This string is written to a single line file.

                2. The BAT file calls the program that calls the .INC passing the string to it, which writes the file.
                Example for three (3) variables to be set: value 1;value no 2;third value
                Written to file: C:\Temp\xxx.txt

                3. In the BAT file I would use something like:
                for /f "tokens=1-3 delims=;" %%a in (C:\Temp\xxx.txt) do (
                set xenv1=%%a && set xenv2=%%b && set xenv3=%%c
                xenv1 would be set to: value 1
                xenv2 would be set to: value no 2
                xenv3 would be set to: third value

                N.B. 1) I use ";" as the delimiter this can be changed if needed
                2) The number of variables can be changed
                3) I believe the limit is 31 tokens can be extracted in the FOR