title: How to pass environment variables as parameters by reference to another batch file?
body: <p>I have a question about Windows batch. I have two batch files/scripts, the first one calls the second one, several times with different parameters.</p>
  <p>I want to create several environment variables in the first one and pass them as parameters to the second one. In this second batch script the passed variables should be increased each by a value determined in the second batch file. Then the environment variables in first batch file are passed again on the next call of the second batch file and their values should be incremented once again accordingly by the second batch file.</p>
  <p><strong>First .bat script:</strong></p>
  <pre><code>::Modules find
  set anz_Modules=0
  ::Modules build successful
  set SR_Modules=0
  set CPP_Modules=0
  
  call ./Pearl_p2cpp.bat EXTRA auto anz_Modules SR_Modules CPP_Modules
  rem Here are more calls of Pearl_p2cpp.bat with varying first parameter.
  call ./Pearl_p2cpp.bat FKT auto anz_Modules SR_Modules CPP_Modules
  </code></pre>
  <p><strong>Second.bat script <code>Pearl_p2cpp.bat</code>:</strong></p>
  <pre><code>::Globale Variablen aus *_ALLES.bat bearbeiten
  if NOT[%3]==[] goto noParams
  set /a &quot;%~3=%3%+%numberOfModuls%&quot;
  
  if NOT[%4]==[] goto noParams
  set /a &quot;%~4=%4%+%SRecordSuccess%&quot;
  
  if NOT[%5]==[] goto noParams
  set /a &quot;%~5=%5%+%CppSuccess%&quot;
  
  :noParams
  </code></pre>
  <p><strong>First .bat script again:</strong></p>
  <pre><code>echo Es wurden insgesammt [%anz_Modules%]*.P (PEARL)-Module gefunden
  echo aus diesen wurden [%SR_Modules%]-SREC und [%CPP_Modules%]-C++ Dateien in %TookTime:~0,-2%.%TookTime:~-2% seconds gebuildet! 
  </code></pre>
  <p><strong>Output:</strong></p>
  <blockquote>
  <p>Es wurden insgesammt [0]*.P (PEARL)-Module gefunden<br />
  aus diesen wurden [0]-SREC und [0]-C++ Dateien in 143.41 seconds gebuildet!</p>
  </blockquote>
  <p>Increasing, or editing the variables does not seem to work.</p>
  <p>What is the problem?</p>
  <p><strong>UPDATE 2021-04-09:</strong></p>
  <p>Here are the <em>changed batch scripts</em> according to suggestions by Mofi in finally deleted comments. The second batch file works now with local variables which save the interim values. The second batch file should pass back the new values at the end of <code>endlocal</code> with set <code>&quot;%~3=%interimVAR%&quot;</code>.</p>
  <p>But the output is still wrong as output are the initial values of first batch file.</p>
  <p><strong>Full first .bat script:</strong></p>
  <pre><code>@echo off
  
  :: Store start time
  set StartTIME=%TIME%
  set H=%StartTIME:~0,2%
  if &quot;%H:~0,1%&quot;==&quot; &quot; set H=%H:~1,1%
  if &quot;%H:~0,1%&quot;==&quot;0&quot; set H=%H:~1,1%
  set M=%StartTIME:~3,2%
  if &quot;%M:~0,1%&quot;==&quot;0&quot; set M=%M:~1,1%
  set S=%StartTIME:~6,2%
  if &quot;%S:~0,1%&quot;==&quot;0&quot; set S=%S:~1,1%
  set U=%StartTIME:~9,2%
  if &quot;%U:~0,1%&quot;==&quot;0&quot; set U=%U:~1,1%
  )
  set /a Start100S=%H%*360000+%M%*6000+%S%*100+%U%
  
  ::Modules found
  set /A &quot;anz_Modules = 0&quot;
  ::Modules build successful
  set /A &quot;SR_Modules = 0&quot;
  set /A &quot;CPP_Modules = 0&quot;
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; EXTRA auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; FKT auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; FKTREP auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; FKTZU auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; KALIB auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; LASER auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; MOAB auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; MOENDE auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; MOZU auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; MTERM auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; MTERM\RTOS auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ..
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; PAKZU auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; PE auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; SRS auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; SRTBEG2 auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; STDBY auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; VDE auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  call &quot;%~dp0Pearl_p2cpp.bat&quot; VGL auto %anz_Modules% %SR_Modules% %CPP_Modules%
  call cd ../.batch_scripts
  
  :: Get the end time
  set StopTIME=%TIME%
  set H=%StopTIME:~0,2%
  if &quot;%H:~0,1%&quot;==&quot; &quot; set H=%H:~1,1%
  if &quot;%H:~0,1%&quot;==&quot;0&quot; set H=%H:~1,1%
  set M=%StopTIME:~3,2%
  if &quot;%M:~0,1%&quot;==&quot;0&quot; set M=%M:~1,1%
  set S=%StopTIME:~6,2%
  if &quot;%S:~0,1%&quot;==&quot;0&quot; set S=%S:~1,1%
  set U=%StopTIME:~9,2%
  if &quot;%U:~0,1%&quot;==&quot;0&quot; set U=%U:~1,1%
  )
  
  set /a Stop100S=%H%*360000+%M%*6000+%S%*100+%U%
  :: Test midnight rollover. If so, add 1 day=8640000 1/100ths secs
  if %Stop100S% LSS %Start100S% set /a Stop100S+=8640000
  set /a TookTime=%Stop100S%-%Start100S%
  echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  echo ----------------------------------------------------------------------------------------------------
  ::echo um %StartTime% Uhr gestartet
  ::echo um %StopTime% Uhr beendet
  echo Es wurden insgesammt [%anz_Modules%]*.P (PEARL)-Module gefunden
  echo aus diesen wurden [%SR_Modules%]-SREC und [%CPP_Modules%]-C++ Dateien in %TookTime:~0,-2%.%TookTime:~-2% seconds gebuildet! 
  echo ----------------------------------------------------------------------------------------------------
  echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  </code></pre>
  <p><strong>Full second.bat script full:</strong></p>
  <pre><code>@echo off    
  
  echo compile and port moduls in subfolder &quot;%1&quot;
  cd .././%1
  pwd
  echo *.Px ,*.cpp und *.SR-Dateien werden geloescht.
  
  if /i &quot;%2%&quot;==&quot;auto&quot; GOTO nopause_1
  
  pause
  
  rm *.Px
  rm *.SR
  
  echo Konvertierungen starten:
  
  :nopause_1
  
  if /i &quot;%2%&quot;==&quot;auto&quot; GOTO no_time_rec
  
    :: Store start time
    set StartTIME=%TIME%
    set H=%StartTIME:~0,2%
    if &quot;%H:~0,1%&quot;==&quot; &quot; set H=%H:~1,1%
    if &quot;%H:~0,1%&quot;==&quot;0&quot; set H=%H:~1,1%
    set M=%StartTIME:~3,2%
    if &quot;%M:~0,1%&quot;==&quot;0&quot; set M=%M:~1,1%
    set S=%StartTIME:~6,2%
    if &quot;%S:~0,1%&quot;==&quot;0&quot; set S=%S:~1,1%
    set U=%StartTIME:~9,2%
    if &quot;%U:~0,1%&quot;==&quot;0&quot; set U=%U:~1,1%
    )
    set /a Start100S=%H%*360000+%M%*6000+%S%*100+%U%
  
  :no_time_rec
  
  setlocal EnableDelayedExpansion
  
  set cppExtension=&quot;.cpp&quot;
  set PearlExtension=&quot;.P&quot;
  set SRecordExtension=&quot;.SR&quot;
  
  set /a &quot;SRecordSuccess=0&quot;
  set /a &quot;CppSuccess=0&quot;
  set /a &quot;numberOfModuls=0&quot;
  set doCompilePort=true
  
  for /f %%a IN ('dir /b *.P') do (
    
    echo Modul %%a
    set doCompilePort=true
  
    if &quot;%%a&quot; == &quot;INCL.P&quot; set doCompilePort=false
    if &quot;%%a&quot; == &quot;INCL_FKR.P&quot; set doCompilePort=false
    if &quot;%%a&quot; == &quot;INCL_GLO.P&quot; set doCompilePort=false
    if &quot;%%a&quot; == &quot;INCL_PE.P&quot; set doCompilePort=false
    if &quot;%%a&quot; == &quot;INCL_STB.P&quot; set doCompilePort=false
    if &quot;%%a&quot; == &quot;INCL_VDE.P&quot; set doCompilePort=false
    
    echo doCompilePort:!doCompilePort!
    
    
    if &quot;!doCompilePort!&quot; == &quot;true&quot; (
      echo start compiler
      
  
      set /a numberOfModuls=!numberOfModuls!+1
      echo modul number !numberOfModuls!
      
      echo Convert
      set filename=%%a
      set fileBasename=!filename:~0,-2!
      rm !fileBasename!.cpp
      echo returnValue rm !errorlevel!
    
      set filenameSR=!fileBasename!.SR
  
      call C:\programme\compiler\rtos-uh\pe\pe.exe C:\programme\compiler\rtos-uh\ppc\PEARL90\vers16_6\P16Q6H.VBI -si=!filename! -co=!filenameSR! -lo=no -e0
      IF !errorlevel! NEQ 0 (
        REM do something here to address the error
        echo returnValue Pearl-Compiler !errorlevel!
      ) ELSE (
        set /a SRecordSuccess=!SRecordSuccess!+1
      )
          
  
      call pqprep !filename!
      echo returnValue pqprep !errorlevel!
      
      call p2cpp !filename!x
      
      IF !errorlevel! NEQ 0 (
        REM do something here to address the error
        echo returnValue Pearl-Compiler !errorlevel!
      ) ELSE (
        set /a CppSuccess=!CppSuccess!+1
      )
    )
    echo ----------------------------------------------------------------------------------------------------
  )
  echo ----------------------------------------------------------------------------------------------------
  
  echo number of moduls: %numberOfModuls%
  echo successfully created S-Records: %SRecordSuccess%
  echo successfully created cpp-files: %CppSuccess%
  
  IF %numberOfModuls% == %SRecordSuccess% (
    IF %numberOfModuls% == %CppSuccess% (
      echo all moduls successful
    )
  )
  
  ::Globale Variablen aus *_ALLES.bat bearbeiten
  if &quot;%~3&quot; == &quot;&quot; goto noParams
  set /A &quot;PassedVar3=%~3+numberOfModuls&quot;
  
  if &quot;%~4&quot; == &quot;&quot; goto noParams
  set /A &quot;PassedVar4=%~4+SRecordSuccess&quot;
  
  if &quot;%~5&quot; == &quot;&quot; goto noParams
  set /A &quot;PassedVar5=%~5+CppSuccess&quot;
  
  :noParams
  
  if /i &quot;%2%&quot;==&quot;auto&quot; GOTO no_time_rec2
  
    :: Get the end time
    set StopTIME=%TIME%
    set H=%StopTIME:~0,2%
    if &quot;%H:~0,1%&quot;==&quot; &quot; set H=%H:~1,1%
    if &quot;%H:~0,1%&quot;==&quot;0&quot; set H=%H:~1,1%
    set M=%StopTIME:~3,2%
    if &quot;%M:~0,1%&quot;==&quot;0&quot; set M=%M:~1,1%
    set S=%StopTIME:~6,2%
    if &quot;%S:~0,1%&quot;==&quot;0&quot; set S=%S:~1,1%
    set U=%StopTIME:~9,2%
    if &quot;%U:~0,1%&quot;==&quot;0&quot; set U=%U:~1,1%
    )
  
    set /a Stop100S=%H%*360000+%M%*6000+%S%*100+%U%
    :: Test midnight rollover. If so, add 1 day=8640000 1/100ths secs
    if %Stop100S% LSS %Start100S% set /a Stop100S+=8640000
    set /a TookTime=%Stop100S%-%Start100S%
  
    echo wurden in %TookTime:~0,-2%.%TookTime:~-2% seconds uebersetzt und in PEARL-SRs compiliert
    echo ----------------------------------------------------------------------------------------------------
  
  :no_time_rec2
  
  echo *.Px -Dateien werden geloescht.
  echo ----------------------------------------------------------------------------------------------------
  
  if /i &quot;%2%&quot;==&quot;auto&quot; GOTO nopause_2
  
  Pause
  
  :nopause_2
  
  rm *.Px
  rm *.phx
  rm *.INCx
  
  rm *.incx
  
  
  pwd
  cd ./..
  pwd
  
  if &quot;%~3&quot; == &quot;&quot; goto noParams
  endlocal &amp; set &quot;%~3=%PassedVar3%&quot; &quot;%~4=%PassedVar4%&quot; &quot;%~5=%PassedVar5%&quot;
  
  :noParams
  
  echo Vorgang beenden
  </code></pre>