批次变量名称中的冒号 [英] Colon in batch variable name

查看:111
本文介绍了批次变量名称中的冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本,该脚本应该可以访问名为env:dev之类的变量,因此它内部有一个冒号...此变量是由第三方组件设置的,因此对那个名字...

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%
...

有什么建议吗?

推荐答案

例如,如果启用了命令扩展名(默认为Windows cmd),则:冒号在CMD环境变量中具有特殊含义

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

  • 变量编辑/替换:
    • %variable:StrToFind=NewStr%
    • Variable Edit/Replace:
      • %variable:StrToFind=NewStr%
      • %variable:~num_chars_to_skip%
      • %variable:~num_chars_to_skip,num_chars_to_keep%
      • %variable:~num_chars_to_skip%
      • %variable:~num_chars_to_skip,num_chars_to_keep%

      很难转义:冒号( <可能的话.一种解决方法是:创建变量名称,使:冒号被另一个字符替换,例如_ 低线(下划线):

      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
      

      输出:

      ==> 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
      
      ==>
      

      编辑:出于完整性考虑:

      @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 
      

      请注意禁用命令扩展的影响,请阅读cmd /?:

      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.

      这篇关于批次变量名称中的冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆