I'm trying to make a process I have as dynamic as possible.
For instance, I have a batch file called DATALOAD_AT1.cmd which calls my _env.cmd file.
However, in my environment file, I need to set (2) variables:
%APP% which is the string derived following the "_" symbol in my call script NAME = DATALOAD_AT1.cmd
Here are the specific lines of code in my environment file that should be populated with AT1:
SET ESSB_USER_%APP%=AUTOTRAIN%NUM%
SET ESSB_APP_%APP%=GJA_%APP%
SET GJA_%APP%=GJA_%APP%\
SET GENJBAPPLOGS_%APP%=GENJBAPP_Logs\%APP%\
SET GENJBAPPERRORS_%APP%=GENJBAPP_Errors\%APP%\
Then %NUM% which should equal the only number following the _ symbol, whichever is easier to code for? In this case, "1" in my call script NAME = DATALOAD_AT1.cmd
Here is the specific line of code in my _env.cmd file that should be populated with 1:
SET ESSB_USER_%APP%=AUTOTRAIN%NUM%
So, %APP% & %NUM% need to be set in my _env.cmd file by parsing the file name of the calling script - DATALOAD_AT1.cmd
My environment file looks like the following:
REM Script Name : _env.cmd
REM --------------------------------
REM SET USER AND SERVER VARIABLES
REM --------------------------------
SET ESSB_SRVR=epm11124.peloton.lan
SET ESSB_USER_%APP%=AUTOTRAIN%NUM%
SET ESSB_PSWD=peloton123
SET ESSB_APP_%APP%=GJA_%APP%
SET ESSB_DB=Fin_Plan
REM --------------------------------
REM GJA_XXX SPECIFIC VARIABLES
REM --------------------------------
SET GJA_%APP%=GJA_%APP%\
SET GENJBAPPLOGS_%APP%=GENJBAPP_Logs\%APP%\
SET GENJBAPPERRORS_%APP%=GENJBAPP_Errors\%APP%\
Once successful, the variables would assume the following values:
REM Script Name : _env.cmd
REM --------------------------------
REM SET USER AND SERVER VARIABLES
REM --------------------------------
SET ESSB_SRVR=epm11124.peloton.lan
SET ESSB_USER_AT1=AUTOTRAIN1
SET ESSB_PSWD=peloton123
SET ESSB_APP_AT1=GJA_AT1
SET ESSB_DB=Fin_Plan
REM --------------------------------
REM GJA_XXX SPECIFIC VARIABLES
REM --------------------------------
SET GENJBAPPLOGS_AT1=GENJBAPP_Logs\AT1\
SET GENJBAPPERRORS_AT1=GENJBAPP_Errors\AT1\
Then, with my environment variables set, I'd need to pass them back to the call script as shown where in red
REM Script Name : DATALOAD_AT1.cmd
REM --Call Environment Script--
call C:\Hyperion_Batch\Scripts\batch\_env.cmd
REM --Set working directory as script path--
cd /d %~dp0
REM ------------------------------------------------------------------------
REM SET LOG & ERROR PATHS
REM ------------------------------------------------------------------------
SET intrapath=%MAINPATH%%LOGPATH%%GENJBAPPLOGS_%APP%
SET errorintrapath=%MAINPATH%%ERRORPATH%%GENJBAPPERRORS_%APP%
REM ------------------------------------------------------------------------
REM SET TOKENS FOR DATE / TIME PARSING
REM ------------------------------------------------------------------------
FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)
REM ------------------------------------------------------------------------
REM SET & DELETE LOG FILES
REM ------------------------------------------------------------------------
SET logfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log
SET errorfile=%errorintrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log
SET maxllogfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0_MaxL.log
if exist %logfile% del %logfile%
if exist %errorfile% del %errorfile%
if exist %maxllogfile% del %maxllogfile%
REM -- Begin Data Load Process
echo ********************************************************>>%logfile%
echo %~n0 at %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo ********************************************************>>%logfile%
echo BEGIN DATA LOAD PROCESS %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo BEGINNING DATA LOAD PROCESS
SET CLR_DATA_SCRIPT=GJACLRD
SET GENJBAPPDLR=FCSTCD
SET DATAFILE=GENJBAPP_FCST_DF.csv
SET AGG_DATA_SCRIPT=GJAAGGD
CALL %STARTMAXL% %MAINFOLDER%%MAXLSCRIPTPATH%GENJBAPP_MAXL_PD.mxl %ESSB_USER_%APP% %ESSB_PSWD% %ESSB_SRVR% %ESSB_APP_%APP% %ESSB_DB% %CLR_DATA_SCRIPT% %GENJBAPPDFPATH%%DATAFILE% %GENJBAPPDLR% %AGG_DATA_SCRIPT% %maxllogfile%
SET myError2=%errorlevel%
IF %myError2%==0 goto NormalExit
echo ********************************************************>>%logfile%
echo Error Encountered in EXECUTE DATA LOAD PROCESS >>%logfile%
echo ********************************************************>>%logfile%
goto AbnormalExit
:NormalExit
echo ********************************************************>>%logfile%
echo %~n0 Success >>%logfile%
echo ********************************************************>>%logfile%
echo ********************************************************>>%logfile%
echo Normal Exit - %~nx0 >>%logfile%
echo ********************************************************>>%logfile%
date /t >>%logfile%
time /t >>%logfile%
EXIT /B 0
:AbnormalExit
echo ********************************************************>>%errorfile%
echo %~n0 results in Abnormal Exit >>%errorfile%
echo ********************************************************>>%errorfile%
echo Please Check the log file for errors >>%errorfile%
echo ********************************************************>>%errorfile%
echo Abnormal Exit - %~nx0 >>%errorfile%
echo ********************************************************>>%errorfile%
date /t >>%errorfile%
time /t >>%errorfile%
EXIT 1
Is this possible? Or perhaps even an easier way? Thank you!