I need help in passing a variable value of a batch file to another batch file.
I am using this statement:
call vartest.bat
if %username%==NA (
echo First login detected.
set /p usernameIN= Username:
#echo set username=%usernameIN% > vartest.bat
)
The problem is that, the value of "usernameIN" does not pass-on to the external batch file. I tried it using normal text instead of the variable and it works.
Is there any way to make this possible?
Thank you.
You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within brackets (and ))
setlocal enabledelayedexpansion
call vartest.bat
if %username%==NA (
echo First login detected.
set /p usernameIN= Username:
#echo set "username=!usernameIN!" > vartest.bat
)
Also you should use set "var=value" to avoid unintended spaces in the variable.
See here for a short demonstration of delayed expansion.
Related
I would like to create a batch script which allows to create number and name of folder chosen by the user.
However, I received a syntax error using the script below.
variable "nome" is not taken.
Here is my code:
echo How many folders?
set /p cc=tell me how many
SETLOCAL EnableDelayedExpansion
FOR /L %%G IN (1,1,%cc%) DO (set /p nome=tell me the name
md %nome%)
pause
mr xyz, please use the search bar to search for delayed expansion. It's not that hard.
What Is DelayedExpansion
Batch-file variables are expanding in the moment the line is parsed. That means the nome variable isn't set when the entire for loop is parsed.
How-To Make The Variable Expand At Run-time?
SETLOCAL Method
Add
setlocal enableDelayedExpansion
anywhere before the for loop, so cmd will process the variable at run-time. And change %nome% to !nome!.
CALL MKDIR/MD Method
Change your MD statement to:
call MD %%nome%%
As this will trigger the emulated delayed expansion using the special property of call and %%.
I'm having trouble access the value stored in the example below. I need to access the value stored in a variable, but the variable name is stored in a different variable. Please help.
Example:
setlocal enabledelayedexpansion
set a222333password=hellopass
for %%i in (%fileserver%\t*) do (
set currentfilename=%%~ni --> file name is "t222333"
set currentlogin=!currentfilename:t=a! --> login is "a222333"
set currentpasswd=!!currentlogin!password! --> password should be "hellopass"
echo !currentpasswd! --> this gives me the value "a222333password" instead of "hellopass"
)
You cannot nest delayed expansion like set currentpasswd=!!currentlogin!password!, because this first detects !!, which are combined to one opening !, so the variable expansion !currentlogin! is done resulting in a222333, then there is the literal part password, and finally another ! that cannot be paired and is therefore ignored.
However, you could try this, because call initiates another parsing phase:
call set "currentpasswd=%%!currentlogin!password%%"
Or this, because for variable references become expanded before delayed expansion occurs:
for /F "delims=" %%Z in ("!currentlogin!") do set "currentpasswd=!%%Zpassword!"
Or also this, because argument references, like normally expanded variables (%-expansion), are expanded before delayed expansion is done:
rem // Instead of `set currentpasswd=!!currentlogin!password!`:
call :SUBROUTINE currentpasswd "!currentlogin!"
rem // Then, at the end of your current script:
goto :EOF
:SUBROUTINE
set "%~1=!%~2password!"
goto :EOF
rem // Alternatively, when you do not want to pass any arguments to the sub-routine:
:SUBROUTINE
set "currentpasswd=!%currentlogin%password!"
goto :EOF
All these variants have got two important things in common:
there occur two expansion phases;
the inner variable reference is expanded before the outer one;
this is my code :
SET JAVA_VERSION=%1
REM here lets assume the user enter a JAVA_7_HOME as an argument
#ECHO %JAVA_VERSION%
REM this print me JAVA_7_HOME because JAVA_7_HOME is STRING
REM Now I want to access to the value of environment variable JAVA_7_HOME so I done this :
if %%%JAVA_7_HOME%%% ==[]GOTO INDEFINED_VARIABLE
REM here I concatinate JAVA_7_HOME with % % at the left and at the right but doesn't works
I Do not Know how to access to the value of the environment variable JAVA_7_HOME.
Thank you at all.
CALL SET JAVA_VERSION=%%%1%%
The line will be parsed to
CALL SET JAVA_VERSION=%JAVA_7_HOME%
Now, the call command is executed and the line reparsed, and the final command executed is
SET JAVA_VERSION=C:\SomeWhere
Also, you can enable delayed expansion and do
SETLOCAL EnableDelayedExpansion
SET JAVA_VERSION=!%1!
I backed to this subject :), So your solution it works fine without a loop for but now I changed the concept and I have a variable called CONTEXT_VARAIABLE which contains some others names of environnements variables for example in my case is:
CONTEXT_VARAIABLE=JAVA_HOME;JBOSS_HOME
so now I loop from this variable CONTEXT_VARAIABLE to get each variable and do some treatement with it so my code here is:
setlocal EnableDelayedExpansion
FOR %%L IN (%SOLIFE_VERSION%) DO (
SET JAVA_JBOSS_HOME=%%%%L%%
#ECHO !JAVA_JBOSS_HOME!
)
here each time of the loop it prints me:
%JAVA_HOME%
%JBOSS_HOME%
and now my problem I want to access to these variables and if I use CALL SET it makes an error because I think we are not allowed to use CALL SET in loop for.
So please if you have an idea about this problem.
Thank you.
I currently have this code in my batch file to store a value in variable %%i:
FOR /F %%i in (AylaUnits.txt) DO set %%i
set "$Unit=%%i"
echo The unit is : %$Unit%
Pause
This seems to loop fine, but for each item I get this response:
set AC000W000004591
Environment variable AC000W000004595 not defined
The unit is : %i
There is going to be 100+ different lines in the text file. Will I need to create an environmental variables for each one? I don't think that's the best way to do it and I'm not sure I'm understanding environmental variables correctly.
I need to plug each line of the file into a url section if that's of any importance.
Thanks,
DM
You have a syntax issue, see the proposed change below:
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F %%i in (AylaUnits.txt) DO (
set "$Unit=%%i"
echo The unit is : !$Unit!
)
Pause
Please see the edit above, since we're setting the variable inside the for loop, we need to use SETLOCAL ENABLEDELAYEDEXPANSION to be able to expand on it. Since we're expanding the var that's set in the for loop, we need to use ! instead of % when using those variables.
I'd like to put each of the many properties' file names into variable fileName and echo them out to the command prompt window. But only the last properties file name to be cycled thru is printed out as many times as there are properties files. Is there an easy fix to this problem. I know that ...DO echo %%-nxG can do the same thing but I'd like to save the file name in %%~nxG for future use.
FOR %%G IN (C:\ExecutionSDKTest_10.2.2\*.properties) DO (
set fileName=%%~nxG
echo %fileName%
)
You need to use delayed expansion:
setlocal enabledelayedexpansion
FOR %%G IN (C:\ExecutionSDKTest_10.2.2\*.properties) DO (
set fileName=%%~nxG
echo !fileName!
)
Environment variables in cmd are expanded when a command is parsed – in this case this includes the whole block in parentheses. So %fileName% gets replaced by an empty string because it didn't have a value before the loop ran. Delayed expansion uses ! instead of % and changes variable evaluation so that they are evaluated just before a command is run.
help set has more details about why and when it is necessary. In general, whenever you modify and use a variable within a loop you have to use delayed expansion, but it comes with a few other benefits too.