Colon in batch variable name

Matthias

I have a batch script which should have access to a variable named something like env:dev, so it has a colon inside... this variable is set by a third-party component, so I don't have influence on that naming...

How can I access the content of this variable in my batch script? I know that : is a special character, so can I perhaps escape it? The following doesn't work:

echo %env:dev%
echo "%env:dev%"
echo %env^:dev%
...

Any suggestions?

JosefZ

A : colon has special meaning in CMD environment variables if command extensions are enabled (Windows cmd default), for instance

Hard to escape a : colon in variable name, if possible at all. Here's a workaround: create variables with such names that : colon is replaced by another character, e.g. _ Low Line (underscore):

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
rem create sample variables
set "env:dev1=some thing!"     value contains exclamation mark
set "env:dev2=some thing%%"    value contains percent sign
set "an:other=some:thing3"     another name containing colon 

echo --- before ---
set env
set an

for /F "tokens=1* delims==" %%G in ('set') do (
    set "auxName=%%G"
    set "auxValue=%%H"
    call :colons
)

echo --- after  ---
set env
set an

rem 
ENDLOCAL
goto :eof

:colons
  if not "%auxName::=_%" == "%auxName%" set "%auxName::=_%=%auxValue%"
goto :eof

Output:

==> d:\bat\so\37973141.bat
--- before ---
env:dev1=some thing!
env:dev2=some thing%
an:other=some:thing3
--- after  ---
env:dev1=some thing!
env:dev2=some thing%
env_dev1=some thing!
env_dev2=some thing%
an:other=some:thing3
an_other=some:thing3

==>

Edit: for the sake of completeness:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
rem create sample variables
set "env:dev1=some thing!"     value contains exclamation mark
set "env:dev2=some thing%%"    value contains percent sign
set "an:other=some:thing3"     another name containing colon

rem use sample variables 
SETLOCAL DisableExtensions
echo Disabled Extensions %env:dev1% / %env:dev2% / %an:other%
ENDLOCAL 

Be aware of disabling command extensions impact, read cmd /?:

The command extensions involve changes and/or additions to the following commands:

DEL or ERASE
COLOR
CD or CHDIR
MD or MKDIR
PROMPT
PUSHD
POPD
SET
SETLOCAL
ENDLOCAL
IF
FOR
CALL
SHIFT
GOTO
START (also includes changes to external command invocation)
ASSOC
FTYPE

To get specific details, type commandname /? to view the specifics.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to split variable by colon in Batch?

From Dev

Batch increment part of variable name

From Dev

Batch Variable name and Percent sign

From Dev

Add space between variable name and colon in Visual Studio Code + Typescript

From Dev

Batch escape colon :

From Dev

Batch Colon not Parsed in Replace

From Dev

Batch escape colon :

From Dev

Batch Colon not Parsed in Replace

From Dev

input a variable name and display - BATCH FILE

From Dev

Get value of nested variable name in batch script

From Dev

Get product name from WMIC for a variable in batch

From Dev

What is the meaning of ':Q=' in variable name in batch cmd

From Dev

Batch : Dynamic variable name (eval equivalent)

From Dev

Get value of nested variable name in batch script

From Dev

Windows batch: how to assign variable with dynamic name?

From Dev

Assigning directory name to a variable in cmd batch

From Dev

What is the meaning of ':Q=' in variable name in batch cmd

From Dev

colon in variable declaration

From Dev

Colon : used in variable initialization?

From Dev

Use of colon in variable declaration

From Dev

colon in variable declaration

From Dev

Purpose of colon in variable expansion

From Dev

How to put variable value inside another variable name in batch?

From Dev

How to put variable value inside another variable name in batch?

From Dev

Batch: variable indirection: getting the value of a variable by dynamically constructed name

From Dev

Flake 8: "multiple statements on one line (colon)" only for variable name starting with "if"

From Dev

Parsing field name with a colon in JSON

From Dev

What happens to the file with colon in the name?

From Dev

Adding all cpp files name in a single variable in batch file

Related Related

HotTag

Archive