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, 09:19 AM.


      • #4
        Jim, Pierre,

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

        Again, thanks,