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.
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.
First .bat script:
::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
Second.bat script Pearl_p2cpp.bat
:
::Globale Variablen aus *_ALLES.bat bearbeiten
if NOT[%3]==[] goto noParams
set /a "%~3=%3%+%numberOfModuls%"
if NOT[%4]==[] goto noParams
set /a "%~4=%4%+%SRecordSuccess%"
if NOT[%5]==[] goto noParams
set /a "%~5=%5%+%CppSuccess%"
:noParams
First .bat script again:
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!
Output:
Es wurden insgesammt [0]*.P (PEARL)-Module gefunden
aus diesen wurden [0]-SREC und [0]-C++ Dateien in 143.41 seconds gebuildet!
Increasing, or editing the variables does not seem to work.
What is the problem?
UPDATE 2021-04-09:
Here are the changed batch scripts 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 endlocal
with set "%~3=%interimVAR%"
.
But the output is still wrong as output are the initial values of first batch file.
Full first .bat script:
@echo off
:: Store start time
set StartTIME=%TIME%
set H=%StartTIME:~0,2%
if "%H:~0,1%"==" " set H=%H:~1,1%
if "%H:~0,1%"=="0" set H=%H:~1,1%
set M=%StartTIME:~3,2%
if "%M:~0,1%"=="0" set M=%M:~1,1%
set S=%StartTIME:~6,2%
if "%S:~0,1%"=="0" set S=%S:~1,1%
set U=%StartTIME:~9,2%
if "%U:~0,1%"=="0" set U=%U:~1,1%
)
set /a Start100S=%H%*360000+%M%*6000+%S%*100+%U%
::Modules found
set /A "anz_Modules = 0"
::Modules build successful
set /A "SR_Modules = 0"
set /A "CPP_Modules = 0"
call "%~dp0Pearl_p2cpp.bat" EXTRA auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" FKT auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" FKTREP auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" FKTZU auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" KALIB auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" LASER auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" MOAB auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" MOENDE auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" MOZU auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" MTERM auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" MTERMRTOS auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ..
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" PAKZU auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" PE auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" SRS auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" SRTBEG2 auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" STDBY auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" VDE auto %anz_Modules% %SR_Modules% %CPP_Modules%
call cd ../.batch_scripts
call "%~dp0Pearl_p2cpp.bat" 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 "%H:~0,1%"==" " set H=%H:~1,1%
if "%H:~0,1%"=="0" set H=%H:~1,1%
set M=%StopTIME:~3,2%
if "%M:~0,1%"=="0" set M=%M:~1,1%
set S=%StopTIME:~6,2%
if "%S:~0,1%"=="0" set S=%S:~1,1%
set U=%StopTIME:~9,2%
if "%U:~0,1%"=="0" 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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Full second.bat script full:
@echo off
echo compile and port moduls in subfolder "%1"
cd .././%1
pwd
echo *.Px ,*.cpp und *.SR-Dateien werden geloescht.
if /i "%2%"=="auto" GOTO nopause_1
pause
rm *.Px
rm *.SR
echo Konvertierungen starten:
:nopause_1
if /i "%2%"=="auto" GOTO no_time_rec
:: Store start time
set StartTIME=%TIME%
set H=%StartTIME:~0,2%
if "%H:~0,1%"==" " set H=%H:~1,1%
if "%H:~0,1%"=="0" set H=%H:~1,1%
set M=%StartTIME:~3,2%
if "%M:~0,1%"=="0" set M=%M:~1,1%
set S=%StartTIME:~6,2%
if "%S:~0,1%"=="0" set S=%S:~1,1%
set U=%StartTIME:~9,2%
if "%U:~0,1%"=="0" set U=%U:~1,1%
)
set /a Start100S=%H%*360000+%M%*6000+%S%*100+%U%
:no_time_rec
setlocal EnableDelayedExpansion
set cppExtension=".cpp"
set PearlExtension=".P"
set SRecordExtension=".SR"
set /a "SRecordSuccess=0"
set /a "CppSuccess=0"
set /a "numberOfModuls=0"
set doCompilePort=true
for /f %%a IN ('dir /b *.P') do (
echo Modul %%a
set doCompilePort=true
if "%%a" == "INCL.P" set doCompilePort=false
if "%%a" == "INCL_FKR.P" set doCompilePort=false
if "%%a" == "INCL_GLO.P" set doCompilePort=false
if "%%a" == "INCL_PE.P" set doCompilePort=false
if "%%a" == "INCL_STB.P" set doCompilePort=false
if "%%a" == "INCL_VDE.P" set doCompilePort=false
echo doCompilePort:!doCompilePort!
if "!doCompilePort!" == "true" (
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:programmecompiler
tos-uhpepe.exe C:programmecompiler
tos-uhppcPEARL90vers16_6P16Q6H.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 "%~3" == "" goto noParams
set /A "PassedVar3=%~3+numberOfModuls"
if "%~4" == "" goto noParams
set /A "PassedVar4=%~4+SRecordSuccess"
if "%~5" == "" goto noParams
set /A "PassedVar5=%~5+CppSuccess"
:noParams
if /i "%2%"=="auto" GOTO no_time_rec2
:: Get the end time
set StopTIME=%TIME%
set H=%StopTIME:~0,2%
if "%H:~0,1%"==" " set H=%H:~1,1%
if "%H:~0,1%"=="0" set H=%H:~1,1%
set M=%StopTIME:~3,2%
if "%M:~0,1%"=="0" set M=%M:~1,1%
set S=%StopTIME:~6,2%
if "%S:~0,1%"=="0" set S=%S:~1,1%
set U=%StopTIME:~9,2%
if "%U:~0,1%"=="0" 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 "%2%"=="auto" GOTO nopause_2
Pause
:nopause_2
rm *.Px
rm *.phx
rm *.INCx
rm *.incx
pwd
cd ./..
pwd
if "%~3" == "" goto noParams
endlocal & set "%~3=%PassedVar3%" "%~4=%PassedVar4%" "%~5=%PassedVar5%"
:noParams
echo Vorgang beenden
See Question&Answers more detail:
os