I'm trying to get to the value of a variable that's made up of two other variables. The only way I've gotten this to work is by using the FOR command with the /F switch and opening a second command shell:
SET A=This
SET B=That
SET ThisThat=YES!
FOR /F %I IN ('ECHO %A%%B%') DO cmd.exe /C ECHO %%I%
Output:
C:\>cmd.exe /c echo %ThisThat%
YES!
Does anyone know of a more elegant way to do this?
UPDATE
Here's a better explanation of what I was trying to accomplish (and was able to accomplish using y'all's - especially Aacini's - responses)
For a Rational BuildForge project, I have environment variables defined for each deployment target host. Example:
CURAM_HOST_NAME_DEV=DDW5T
CURAM_HOST_NAME_UAT=DUW5T
CURAM_HOST_NAME_TRN=DTW5T
CURAM_HOST_NAME_PRD=DPW5P
Another variable HOST contains the environment to deploy to. The deployment code is common for all deployment targets and uses the variable CURAM_HOST_NAME, so I needed to set this variable to the value of the variable that corresponds to the HOST being deployed to. The following does the trick:
#ECHO OFF
SET CURAM_HOST_NAME_DEV=DDW5T
SET CURAM_HOST_NAME_UAT=DUW5T
SET CURAM_HOST_NAME_TRN=DTW5T
SET CURAM_HOST_NAME_PRD=DPW5P
SET HOST=DEV
CALL ECHO %%CURAM_HOST_NAME_%HOST%%%
SET HOST=UAT
CALL ECHO %%CURAM_HOST_NAME_%HOST%%%
SET HOST=TRN
CALL ECHO %%CURAM_HOST_NAME_%HOST%%%
SET HOST=PRD
CALL ECHO %%CURAM_HOST_NAME_%HOST%%%
Output:
DDW5T
DUW5T
DTW5T
DPW5P
In the BuildForge step, the code that sets this particular variable looks like the following:
.bset env "CURAM_HOST_NAME=`CALL ECHO %%%CURAM_HOST_NAME_%%HOST%%%%`"
(The BF parser requires additional %'s)
For a value of DEV for the HOST variable this produces the following in the log:
1026 5/3/14 7:34 PM STEP .bset env "CURAM_HOST_NAME=`CALL ECHO %%%CURAM_HOST_NAME_%%HOST%%%%`"
1027 5/3/14 7:34 PM EXEC .bset env 'CURAM_HOST_NAME' = 'DDW5BTH
Thanks a lot everyone. Wish I had the reputation that would allow me to upvote your answers!
Jozef
#echo off
SET A=This
SET B=That
SET ThisThat=YES!
call echo %%%A%%B%%%
EDIT: Updated answer to an updated question!
#ECHO OFF
SET CURAM_HOST_NAME_DEV=DDW5T
SET CURAM_HOST_NAME_UAT=DUW5T
SET CURAM_HOST_NAME_TRN=DTW5T
SET CURAM_HOST_NAME_PRD=DPW5P
for %%a in (DEV UAT TRN PRD) do CALL ECHO %%CURAM_HOST_NAME_%%a%%
However, you should note that the !variable! replacement (together with setlocal EnableDelayedExpansion) run much faster than the CALL echo %%variable%% trick:
#ECHO OFF
setlocal EnableDelayedExpansion
SET CURAM_HOST_NAME_DEV=DDW5T
SET CURAM_HOST_NAME_UAT=DUW5T
SET CURAM_HOST_NAME_TRN=DTW5T
SET CURAM_HOST_NAME_PRD=DPW5P
for %%a in (DEV UAT TRN PRD) do ECHO !CURAM_HOST_NAME_%%a!
Perhaps you may want to review this post
#echo off
setlocal enableDelayedExpansion
set "A=this"
set "B=that"
set "thisthat=YES^!"
echo !%A%%B%!
Note that the exclamation must be escaped in the assignment of thisthat because delayed expansion is enabled.
Related
I'm trying to use CMDER for a development environment that I've setup.
Basically I've created a .bat file that calls:
#ECHO OFF
start Z:\_DEV\OS_WINDOWS\9_MISC_TOOLS\CMDER\Cmder.exe
Then I've placed the file startdev.bat in:
%CMDER_HOME%\config\profile.d
So everything seems to work just fine, but when the startdev.bat finishes, issuing an:
echo %PATH%
returns:
Z:\_DEV\OS_WINDOWS\1_COMPILER\JDK\ORACLE\1.8.0_181\bin;Z:\_DEV\OS_CYGWIN\bin;Z:\_DEV\OS_WINDOWS\9_MISC_TOOLS\CLutils;Z:\_DEV\OS_WINDOWS\9_MISC_TOOLS\PUTTY;Z:\_DEV\OS_WINDOWS\6_VERSION_CONTROL\PortableGit\bin;C:\WINDOWS;C:\WINDOWS\SysWOW64;C:\WINDOWS\System32
...any idea what's happening?
I would either expect CMDER to override PATH with the value from its own settings, or use my full path, which before the startdev.bat ends shows the value of:
PATH=Z:\_DEV\OS_WINDOWS\9_MISC_TOOLS\CMDER\vendor\conemu-maximus5;Z:\_DEV\OS_WINDOWS\9_MISC_TOOLS\CMDER\vendor\conemu-maximus5\ConEmu;Z:\_DEV\OS_WINDOWS\9_MISC_TOOLS\CMDER\vendor\conemu-maximus5\ConEmu\Scripts;Z:\_DEV\OS_ALL\JVM\3_BUILD_TOOLS\GRADLE\5.4\bin;Z:\_DEV\OS_ALL\JVM\3_BUILD_TOOLS\MAVEN\3.5.4\bin;Z:\_DEV\OS_ALL\JVM\3_BUILD_TOOLS\ANT\1.10.5\bin;Z:\_DEV\OS_WINDOWS\3_BUILD_TOOLS\NODE\LTS\10.15.3;Z:\_DEV\OS_WINDOWS\3_BUILD_TOOLS\NODE\LTS\10.15.3\node_modules;Z:\_DEV\OS_WINDOWS\1_COMPILER\GO\1.12.4\bin;Z:\_DEV\OS_WINDOWS\1_COMPILER\PYTHON\32bit\2.7.13;Z:\_DEV\OS_WINDOWS\1_COMPILER\PYTHON\32bit\2.7.13\scripts;Z:\_DEV\OS_WINDOWS\1_COMPILER\ANDROID\android-sdk-windows\platform-tools;Z:\_DEV\OS_WINDOWS\1_COMPILER\JDK\ORACLE\1.8.0_181\bin;Z:\_DEV\OS_CYGWIN\bin;Z:\_DEV\OS_WINDOWS\9_MISC_TOOLS\CLutils;Z:\_DEV\OS_WINDOWS\9_MISC_TOOLS\PUTTY;Z:\_DEV\OS_WINDOWS\6_VERSION_CONTROL\PortableGit\bin;C:\WINDOWS;C:\WINDOWS\SysWOW64;C:\WINDOWS\System32
..but the fact that it only seems to be keeping the value as defined about halfway through the batch job is strange.
Any ideas?
First I recommend opening a command prompt window and run setlocal /? and endlocal /? to get displayed the help/documentation for those two commands. Very important to know is that every setlocal without a corresponding endlocal results in an implicit execution of endlocal by cmd.exe before exiting processing of a batch file or a subroutine called with command CALL.
Next I suggest reading this answer for even more details about the commands SETLOCAL and ENDLOCAL and what happens on using them.
I suggest like michael_heath to change this code block:
setLocal EnableDelayedExpansion
set CLASSPATH=.
for /R %JRE_HOME%\lib %%a in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%a
)
set CLASSPATH=!CLASSPATH!
Better would be:
setLocal EnableExtensions EnableDelayedExpansion
set CLASSPATH=.
for /R "%JRE_HOME%\lib" %%a in (*.jar) do set "CLASSPATH=!CLASSPATH!;%%a"
endlocal & set "CLASSPATH=%CLASSPATH%"
Now the local environment is ended with passing the environment variable CLASSPATH from local environment, on which it was defined, to the restored previous environment because of cmd.exe expands %CLASSPATH% to current value of the environment variable CLASSPATH in current local environment before executing the command endlocal which restores the previous environment.
Wrong in your batch file is also set WINDIR=%SystemRoot%;%SystemRoot% which should be set "WINDIR=%SystemRoot%".
I recommend further reading Why is no string output with 'echo %var%' after using 'set var = text' on command line? It explains why the syntax set "variable=string value" is recommended nowadays. Many of the environment variable definitions use directly or indirectly %UserProfile% which means depending on whatever the user currently running the batch file has entered as user name on creation of the user account. I have seen users entering their name containing a space and non ASCII characters. And I have seen users creating an account with a user name containing character & like Company GmbH & Co. An ampersand outside a double quoted argument string is interpreted as AND operator and cmd.exe tries to execute after set also the remaining string after & as command line on using something like set USERHOME=%DEVHOME%\%USERNAME% instead of set "USERHOME=%DEVHOME%\%USERNAME%". Well, startdev.bat redefines nearly all predefined Windows Environment Variables including USERNAME and USERPROFILE and so is written safe for most environment variable definitions.
This code block is also not optimal:
FOR /F "usebackq" %%i IN (`hostname`) DO SET HOSTNAME=%%i
echo Running on hostname: %HOSTNAME%
The host name respectively computer name could contain also a space or characters critical for command line or start with a semicolon for some unknown reason. So better would be:
FOR /F delims^=^ eol^= %%i IN ('hostname') DO SET "HOSTNAME=%%i"
setlocal EnableDelayedExpansion & echo Running on host name: !HOSTNAME!& endlocal
Whereby there is the environment variable COMPUTERNAME predefined by Windows making it possible to use just following command line:
setlocal EnableDelayedExpansion & echo Running on host name: !ComputerName!& endlocal
An ECHO command line containing an immediately expanded environment variable reference on which it is unknown if its value contains &|<> is always a problem because of the environment variable reference is expanded before further processing of the command line by cmd.exe as described at How does the Windows Command Interpreter (CMD.EXE) parse scripts?
I suggest also reading DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ and avoid the usage of echo. in the batch file to output an empty line.
"halfway through the batch job" as you have a
setLocal EnableDelayedExpansion which sets any further
changes to the variable PATH or other set variables as local.
The endLocal not specified is implied at the end of the script.
To resolve this, use endLocal and set CLASSPATH=%CLASSPATH%
on the same parsed line to set CLASSPATH as global.
Change this part:
setLocal EnableDelayedExpansion
set CLASSPATH=.
for /R %JRE_HOME%\lib %%a in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%a
)
set CLASSPATH=!CLASSPATH!
to this:
setLocal EnableDelayedExpansion
set CLASSPATH=.
for /R %JRE_HOME%\lib %%a in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%a
)
endLocal & set CLASSPATH=%CLASSPATH%
After that changed part, the script will set variables as global again.
I'm required to write a program that displays the computer name, username, and path to user files. All of these should be taken from local system variables. It's been hinted that I'm supposed to use the set command, however, I'm not sure how to use the set command in this situation... I had assumed that I could just use echo %COMPUTERNAME% etc. How can I implement the set command?
I'm guessing that the hint about the Set command was so that you could use the output of it to help you identify the local system variables to use in your batch file, not that you need to use Set to output them.
When you enter Set at the Command prompt you'll get output showing you each of the defined system variables.
Additionally, if you enter Set ComputerName at the prompt, you should get output showing you all variables which begin with the string ComputerName.
So based on the output of the Set command you could Echo, your three variables from a batch file like this:
#Echo Off
Echo %ComputerName%
Echo %UserName%
Echo %UserProfile%
You could also include the variables with their values:
#Echo Off
Echo %%ComputerName%%=%ComputerName%
Echo %%UserName%%=%UserName%
Echo %%UserProfile%%=%UserProfile%
You could also consider running a simple For loop in your batch file to show the same content using the Set command directly:
#Echo Off
For %%A In (ComputerName,UserName,UserProfile) Do Set %%A
Pause
Or you could return just their values using Set and Echo from nested For loops:
#Echo Off
For %%A In (ComputerName,UserName,UserProfile) Do (
For /F "Tokens=1* Delims==" %%B In ('Set %%A') Do Echo %%C)
Pause
to take the question very literal ("display the computer name ... using set command"):
set computername
set username
set userprofile
output like:
COMPUTERNAME=Elon-PC
USERNAME=Muscrat
USERPROFILE=C:\Users\Muskrat
(Compo already has this method in his answer, but I guess using the for command is over the boundaries of the current state of your course)
(Note: for practical use, in most cases Compo's answer (using variables) is better, because in practice, you will probably do something with the values, not "just" show them, but this literally answers your question)
I am trying to match a batch script to do this tutorial automatically, for any system.
#echo off
for /f "tokens=4* delims= " %%A in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Pictures"') do (set loc=%%A %%B)
set loc=%loc%\Spotlight
set /p loc=Location for images (default - "%loc%") [use %%loc%% for default]?:
start robocopy "%localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets" "%temp%\spotlight"
ren %temp%\spotlight\*.* *.jpg
robocopy "%temp%\spotlight" "%loc%"
start %SystemRoot%\explorer.exe %loc%
However, if I try typing %loc%\Spotlight2, it places it in the Desktop\%loc%\spotlight2 (desktop is the current working directory).
Is there a way to make set /p take environment variables?
the usual way to enter a default is an empty input (keeps predefined varable unchanged). But there is a way to enable a variable input. Try the following and answer the question with %windir%
#echo off
setlocal
set x=%username%
set /p "x=enter value ([ENTER] for default: "%x%") "
call set x=%x%
echo %x%
The trick is to expand the variable twice by using another layer of parsing (whith call).
SET /P is basically just an input routine. There is no documented way to have it do further evaluation, and I doubt one exists.
One could try to get CMD to evaluate the contents of a variable to expand any variables in the contents -- I have seem kludges to do this. However, I don't think that is a good way to do what you want.
I think you would be better off using something easier to type, and testing for that explicitly. For example:
set loc=%loc%\Spotlight
echo Enter location for images, or just press [ENTER] to use the default.
echo Default location: %loc%
set userloc=*
set /p userloc=Location?
if not "%userloc%"=="*" set loc=%userloc%
There's a way to do what you want without default locations! It involves variable manipulation:
...
setlocal enabledelayedexpansion
set loc=%loc%\Spotlight
set default=%loc%
set /p "loc=Location [%loc%] for default: "
set out=!loc:%%loc%%=%default%!
echo %out%
...
Hence, typing %loc% replaces it with the original value of loc.
I tested this by typing %loc%\s and it performed as expected. In terms of it being the correct directory, thats to do with how you set the value of loc prior to this code.
Imagine a script called batch.bat, invoked like this:
batch.bat Two
The script contains:
set One=1
set Two=2
set Three=3
set Choice=%1
echo %Choice%
But I want to echo 2, not Two.
What elegant way can I employ to do that? I thought about if %Choice%=One, etc. but my actual script is a bit more complex than the example provided.
Edit: I also tried:
set percent=%%
echo %percent%%Choice%%percent%
But it won't treat the resulting expression as a variable reference.
#echo off
setlocal EnableDelayedExpansion
set one=1
set c=%1
echo delayed expansion and variable: !%c%!
echo delayed expansion and paramr: !%1!
call echo using call and variable: %%%c%%%
call echo using call and param: %%%1%%
I personally prefer the delayed expansion method, but sometimes there are reasons to look for an alternative.
OK, I have figured it out:
The key is to use the "set delayed expansion" trick.
There's also a trick using the "call set" instead of the above but I reckon if I use that, the next guy after me won't know what I was trying to do and might get in trouble.
The script can be fixed by adding !! around the reference that we want to force into the variable name, like so:
#echo off
setlocal EnableDelayedExpansion
set One=1
set Two=2
set Three=3
set Choice=!%1!
echo %Choice%
I am writing a CMD script to generate documentation markdown pages for my GitHub repository. I have decided to give the script a default directory for the project and its documentation folder, and if the end user wants to use a different one, they must specify it, before the next step.
My code is something like:
echo.
setlocal enabledelayedexpansion
set projectDirectory=GroupManagementAppServer
set documentationFolder=documentation
rem ask user for confirmation of projectDirectory,documentationFolder to use
choice /m "By default, project directory is %projectDirectory% and documentation is stored in %documentationFolder%. Should I use these?"
rem if no
if %errorlevel% == 2 (
rem get projectDirectory,documentationFolder from user
set /p relativeDocumentationPathname=Please enter relative pathname to the documentation folder:
rem parse input
call :getAbsolutePath %relativeDocumentationPathname%
set documentationFolder=%_absolutePath%
set projectDirectory="%documentationFolder%\.."
)
echo %_absolutePath%
echo %documentationFolder%
echo %projectDirectory%
:getAbsolutePath
SETLOCAL
for %%i in ("%1%") do (
set filedrive=%%~di
set filepath=%%~pi
set filename=%%~ni
set fileextension=%%~xi
)
ENDLOCAL & SET _absolutePath=%filedrive%%filepath%%filename%%fileextension%
thus far, and when the echos complete, it's as if documentationFolder was never redefined! What the heck is going on, and how do I fix this, so that I can implement the rest of this and move on to actually getting some documentation on?
Here is the fixed code with delayed expansion properly applied, the sub-routine reduced and some minor improvements, mainly related to quotation:
echo/
setlocal EnableDelayedExpansion
set "projectDirectory=GroupManagementAppServer"
set "documentationFolder=documentation"
rem // Ask user for confirmation of `projectDirectory`, `documentationFolder` to use:
choice /M "By default, project directory is '%projectDirectory%' and documentation is stored in '%documentationFolder%'. Should I use these?"
rem // If no:
if %errorlevel% == 2 (
rem // Get `projectDirectory`, `documentationFolder` from user:
set /P relativeDocumentationPathname="Please enter relative pathname to the documentation folder: "
rem // Parse input:
call :getAbsolutePath "%relativeDocumentationPathname%"
set "documentationFolder=!_absolutePath!"
set "projectDirectory=!documentationFolder!\.."
)
echo %_absolutePath%
echo %documentationFolder%
echo %projectDirectory%
goto :EOF
:getAbsolutePath
setlocal
for /D %%I in ("%~1") do (
set "filespec=%%~fI"
)
endlocal & set "_absolutePath=%filespec%"
I'd suggest you use the SO search facility in the top black bar and try to find delayedexpansion. There are hundreds of items on this matter.
Fundamentally, when a block (parenthesised series of statements) is encountered, the entire block is evaluated, substituting the then-current values of the variables and once that is done, the code is executed.
In your case, call echo %%var%% would show the modified values, or using the modified values within a subroutine (like call :arouthethatechosthevalues) would implement the new values.