Using strings after _ symbol in batch file name to set variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Using strings after _ symbol in batch file name to set variable

#1 Post by SIMMS7400 » 10 Jul 2016 19:49

Hi Guys -

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!
Last edited by SIMMS7400 on 11 Jul 2016 13:12, edited 12 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using strings after _ symbol in batch file name to set variable

#2 Post by foxidrive » 10 Jul 2016 20:08

Try phrasing your question like this:

SIMMS7400 wrote:I need to set 2 variables:
%APP% which is based off of the string following the _symbol as shown here:

show the string here

%NUM% which is based off of the number following the _ symbol or T with examples here:

show examples here


as you've presented it, it is a huge slab of code in two files with no indication abut where someone should look for the text you are referring to. You know exactly where it is but a reader doesn't.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Using strings after _ symbol in batch file name to set variable

#3 Post by SIMMS7400 » 11 Jul 2016 01:50

Thank you, Fox! I've updated my post accordingly. It should be easier to understand now.

Thanks!

foxidrive wrote:Try phrasing your question like this:

SIMMS7400 wrote:I need to set 2 variables:
%APP% which is based off of the string following the _symbol as shown here:

show the string here

%NUM% which is based off of the number following the _ symbol or T with examples here:

show examples here


as you've presented it, it is a huge slab of code in two files with no indication abut where someone should look for the text you are referring to. You know exactly where it is but a reader doesn't.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using strings after _ symbol in batch file name to set variable

#4 Post by foxidrive » 11 Jul 2016 11:21

SIMMS7400 wrote:Thank you, Fox! I've updated my post accordingly. It should be easier to understand now.


I'm more focused on giving you pointers on how to ask a good question here as you have managed to misread what I'd outlined.

Where I indicated you should show the string and also to show examples you merely added the name of the large script. You should have instead added the actual single line of code you were referring to, and the example lines you were referring to.

This allows anyone to copy and paste the text into a search of the page, in their web browser.


I'm adding here that using [b.] [./b] style of markers to search for text in the way you showed is a poor way to show the lines you are referring to as it renders in the same colour inside the code tags.

They make the code inaccurate in the same step and the reader would have to edit the code before they could do any testing.

To use the bold tags or color tags to change the appearance of the line
you can wrap the code in regular quote tags, instead of code tags, as I've shown here.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Using strings after _ symbol in batch file name to set variable

#5 Post by SIMMS7400 » 11 Jul 2016 13:05

Hi Fox -

I tried to be more specific. Please let me know if that's okay.

foxidrive wrote:
SIMMS7400 wrote:Thank you, Fox! I've updated my post accordingly. It should be easier to understand now.


I'm more focused on giving you pointers on how to ask a good question here as you have managed to misread what I'd outlined.

Where I indicated you should show the string and also to show examples you merely added the name of the large script. You should have instead added the actual single line of code you were referring to, and the example lines you were referring to.

This allows anyone to copy and paste the text into a search of the page, in their web browser.


I'm adding here that using [b.] [./b] style of markers to search for text in the way you showed is a poor way to show the lines you are referring to as it renders in the same colour inside the code tags.

They make the code inaccurate in the same step and the reader would have to edit the code before they could do any testing.

To use the bold tags or color tags to change the appearance of the line
you can wrap the code in regular quote tags, instead of code tags, as I've shown here.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Using strings after _ symbol in batch file name to set variable

#6 Post by penpen » 11 Jul 2016 14:49

This may help you:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "app=%~n0"
set "app=%app:*_=%"
for /F "delims=0123456789" %%a in ("%app%") do set /A "num=!app:%%~a=!"

echo %app%, %num%

endlocal
goto :eof


penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using strings after _ symbol in batch file name to set variable

#7 Post by foxidrive » 12 Jul 2016 10:11

SIMMS7400, thank you for going to large lengths to improve your question.

Only now do I understand what you were asking. My own head isn't working so well at the moment, but your question was phrased in ways that a programmer couldn't comprehend what the aim was.


1) In your DATALOAD_AT1.cmd script change this line "call C:\Hyperion_Batch\Scripts\batch\_env.cmd" to the code just below.

2) Also alter your _env.cmd script as shown underneath it.

3) Launch DATALOAD_AT1.cmd and see if it displays the variables as they should be.


The code in line 1 and 2 below works for your DATALOAD_AT1.cmd file but a reader still has no knowledge of the range of filenames that could be used instead of DATALOAD_AT1.cmd and so the code can't take into account all the variations. That's the kind of information that should be in the question.

Many people will have a stab at answering questions even if there's not enough info but it usually leads to having to answer it again when more info is provided.



Code: Select all

for /f "tokens=2 delims=_" %%a in ("%~n0") do set "symbol=%%a"
set "number=%symbol:~-1%"
call C:\Hyperion_Batch\Scripts\batch\_env.cmd %symbol% %number%
set e
set g
pause



Code: Select all

REM Script Name : _env.cmd
REM --------------------------------
REM SET USER AND SERVER VARIABLES
REM --------------------------------

SET ESSB_SRVR=epm11124.peloton.lan
SET ESSB_USER_%1=AUTOTRAIN%2
SET ESSB_PSWD=peloton123
SET ESSB_APP_%1=GJA_%1
SET ESSB_DB=Fin_Plan

REM --------------------------------
REM GJA_XXX SPECIFIC VARIABLES
REM --------------------------------

SET GJA_%1=GJA_%1\
SET GENJBAPPLOGS_%1=GENJBAPP_Logs\%1\
SET GENJBAPPERRORS_%1=GENJBAPP_Errors\%1\

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using strings after _ symbol in batch file name to set variable

#8 Post by foxidrive » 12 Jul 2016 10:18

penpen wrote:This may help you:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "app=%~n0"
set "app=%app:*_=%"
for /F "delims=0123456789" %%a in ("%app%") do set /A "num=!app:%%~a=!"

echo %app%, %num%

endlocal
goto :eof


penpen


penpen's code is more dynamic in isolating the number than my code is. It will handle any variation in symbol and number but only if the filename format is exactly as described, and there's no certainty about that because it wasn't stated clearly.


Readers may think that I'm being pedantic but programming shouldn't be a situation where someone has to guess the variations of how the task is being used.

I don't include you in this SIMMS7400, but I get on a high horse about this because there have been so many arguments over so many years with people who have a bloated assessment of their own level of scripting knowledge, that I am not as accommodating as I once was.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Using strings after _ symbol in batch file name to set variable

#9 Post by SIMMS7400 » 12 Jul 2016 11:42

Hi Fox -

Thank you very much for the post! Please do not worry about offending me. I have broad shoulders and eat humble pie daily when it comes to scripting.

As far as your statement:
The code in line 1 and 2 below works for your DATALOAD_AT1.cmd file but a reader still has no knowledge of the range of filenames that could be used instead of DATALOAD_AT1.cmd and so the code can't take into account all the variations


I too dont know all the variation. I do know that its anything following the _ symbol. AT1 or ZX3 etc etc.

Thank you, Fox. I'll report back with my testing results. I will check out your suggestions now!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Using strings after _ symbol in batch file name to set variable

#10 Post by SIMMS7400 » 12 Jul 2016 12:43

Hi Fox -

Your suggestions work as expected; setting the variables in my _env.cmd perfectly!!! No issues there.

The issue I am experiencing though is after they are set in the _env.cmd file, they are not being passed back to DATALOAD_AT1.cmd appropriately. And that could very well be how im declaring the variables.

For instance, GENJBAPPLOGS_%1 is set as such in my environment file. And as I said, the argument has been passed appropriately. Now, to reference GENJBAPPLOGS_%1 back in my call script (DATALOAD_AT1.cmd) how do I that?

I've been trying many diffrent combination with no luck.

In the interest of space, I just threw an ECHO below the first portion of code in DATALOAD_AT1.cmd that references GENJBAPPLOGS_%1:

REM --Call Environment Script--

for /f "tokens=2 delims=_" %%a in ("%~n0") do set "symbol=%%a"
set "number=%symbol:~-1%"
call C:\Hyperion_Batch\Scripts\batch\_env.cmd %symbol% %number%

set e
set g

pause

REM --Set working directory as script path--
cd /d %~dp0

REM ------------------------------------------------------------------------
REM SET LOG & ERROR PATHS
REM ------------------------------------------------------------------------

SET intrapath=%MAINPATH%%LOGPATH%%GENJBAPPLOGS_%%1%
SET errorintrapath=%MAINPATH%%ERRORPATH%%GENJBAPPLOGS_%%SYMBOL%

ECHO %intrapath%>>C:\results.txt
PAUSE
EXIT


intrapath value is as follows:

C:\Hyperion_Batch\Logs\

It should be C:\Hyperion_Batch\Logs\GENJBAPP_AT1\

Any pointers? Thanks!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using strings after _ symbol in batch file name to set variable

#11 Post by foxidrive » 13 Jul 2016 05:51

I didn't test the script using the entire DATALOAD_AT1.cmd as you're the person with the environment for the script, so I didn't notice the flaw in your DATALOAD_AT1.cmd script

In a usual situation the _env.cmd would set the variables using a standard set of variable names and then the DATALOAD_AT1.cmd script would use those standard variables names.

So the %1 below in the variable names on the left hand side should be removed from both DATALOAD_AT1.cmd and _env.cmd


I'm shown the changed _env.cmd down at the bottom and the variable names with the "_%1" removed should also be changed in DATALOAD_AT1.cmd.

Code: Select all

REM Script Name : _env.cmd
REM --------------------------------
REM SET USER AND SERVER VARIABLES
REM --------------------------------

SET ESSB_SRVR=epm11124.peloton.lan
SET ESSB_USER_%1=AUTOTRAIN%2
SET ESSB_PSWD=peloton123
SET ESSB_APP_%1=GJA_%1
SET ESSB_DB=Fin_Plan

REM --------------------------------
REM GJA_XXX SPECIFIC VARIABLES
REM --------------------------------

SET GJA_%1=GJA_%1\
SET GENJBAPPLOGS_%1=GENJBAPP_Logs\%1\
SET GENJBAPPERRORS_%1=GENJBAPP_Errors\%1\





Code: Select all

REM Script Name : _env.cmd
REM --------------------------------
REM SET USER AND SERVER VARIABLES
REM --------------------------------

SET ESSB_SRVR=epm11124.peloton.lan
SET ESSB_USER=AUTOTRAIN%2
SET ESSB_PSWD=peloton123
SET ESSB_APP=GJA_%1
SET ESSB_DB=Fin_Plan

REM --------------------------------
REM GJA_XXX SPECIFIC VARIABLES
REM --------------------------------

SET GJA=GJA_%1\
SET GENJBAPPLOGS=GENJBAPP_Logs\%1\
SET GENJBAPPERRORS=GENJBAPP_Errors\%1\

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Using strings after _ symbol in batch file name to set variable

#12 Post by SIMMS7400 » 16 Jul 2016 19:38

Fox -

This works absolutely perfectly!!!

Thank you so much, I really appreciate it!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Using strings after _ symbol in batch file name to set variable

#13 Post by foxidrive » 17 Jul 2016 11:32

Beauty! That's good to hear, and thanks for your feedback.

Post Reply