0

Batch newbie here. I have an issue with a variable not being captured correctly. I have SCCM Object variable's for ABC_RegionCode, ABC_DBNAME, ABC_Schema and srvpocWeb_%%% (where %%% equals one of 100's of regioncodes).

My Question: How can i get this batch to pull the variable value for srvPocWeb_(ABC_RegionCode) if the Regioncode variable changes for each machine it runs on? I need the script to know that srvPocWeb_%1="%4" means i want the value of the variable to be stored in %4.

this script works fine if i just hardcode the full sitecode name rather than srvPocWeb_%1="%4". its almost as if batch cannot tell i want this translated to a string before reading it.

example.bat %ABC_RegionCode% %ABC_DBNAME% %ABC_SCHEMA% !srvPocWeb_%1! >> C:\test.txt

@echo off
Setlocal EnableDelayedExpansion

Rem Pull in variables from SCCM OBject - 

@echo set ABC_RegionCode="%1"
@echo set ABC_DBNAME="%2"
@echo set ABC_SCHEMA="%3"
@echo set srvPocWeb_%1="%4"



@echo "C:\Program Files\Application_%1\Database\Tools\Client\Client.exe" sourcepath="C:\Source" databasetype="oracle" databaseconnection="data Source=%2;User Id=%3;Password=!srvPocWeb_%1!" reportsfolderpath="C:\reports"

@echo "C:\Program Files\Application_%1\Database\Tools\Client\Client.exe" sourcepath="C:\Source" reImportBuild=true outputonly=false databasetype="oracle" databaseconnection="data Source=%2;User Id=%3;Password=!srvPocWeb_%1!" reportsfolderpath="C:\reports"
@echo All Done
2
  • I have great difficulty in working out what you are trying to do with what data. Please supply an example of the parameters you are supplying to your routine, the results you are obtaining and the result that you want. Simply use the edit facility under the tags to edit this data into your question - and remember to disguise the data supplied if appropriate. Commented Mar 30, 2015 at 3:18
  • i've attmpted to explain it a bit better. hopefully this helps. Commented Mar 30, 2015 at 4:07

1 Answer 1

1

You have a confusion here. As I said you in your other question, you need to specify in which variables are stored each one of the values you want. If the Region code is stored in ABC_RegionCode variable, then its value is taken via %ABC_RegionCode%. In the srvpocWeb_%%% variable (where %%% equals one of 100's of regioncodes, and the specific region code is given by ABC_RegionCode variable) the name of the variable is srvpocWeb_%ABC_RegionCode% and its value is !srvpocWeb_%ABC_RegionCode%!. This way, you may modify your command-line this way:

example.bat %ABC_RegionCode% %ABC_DBNAME% %ABC_SCHEMA% !srvPocWeb_%ABC_RegionCode%! >> C:\test.txt

However, previous line works only if Delayed Expansion is active at the moment previous line is executed, and Delayed Expansion is not enabled in the command-line by default.

You must realize that %1 have the value of the first parameter inside the Batch file only, not in the same line used to invoke the Batch file.

Another way to solve your problem is this:

example.bat %ABC_RegionCode% %ABC_DBNAME% %ABC_SCHEMA% >> C:\test.txt

Inside the Batch file, %1 have the value of ABC_RegionCode, so you may get the rigth value of srvPocWeb_%%% variable this way:

setlocal EnableDelayedExpansion

set srvPocWeb=!srvPocWeb_%1!

PS - This problem should be followed in the same thread of the original question. It is a very bad idea to delete a question and post new details about the same topic in a new question.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for taking the time to answer (again) apreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.