连接变量

问题描述 投票:0回答:3

我需要为 DOS 执行以下操作的 .bat:

set ROOT = c:\programas\
set SRC_ROOT = (I want to put the ROOT Here)System\Source

所以在定义ROOT之后我想要SRC_ROOT = c:\programas\System\Source

我怎样才能做到这一点?

batch-file
3个回答
79
投票
set ROOT=c:\programs 
set SRC_ROOT=%ROOT%\System\Source

16
投票

请注意,如果字符串有空格,则定义时需要使用引号,并且在连接时必须将其截断:

rem The retail files set
set FILES_SET="(*.exe *.dll"

rem The debug extras files set
set DEBUG_EXTRA=" *.pdb"

rem Build the DEBUG set without any
set FILES_SET=%FILES_SET:~1,-1%%DEBUG_EXTRA:~1,-1%

rem Append the closing bracket
set FILES_SET=%FILES_SET%)

echo %FILES_SET%

干杯...


11
投票

如果需要用引号连接路径,可以使用

=
来替换变量中的引号。这不需要您知道路径是否已包含引号。如果没有引号,则不会发生任何更改。

@echo off
rem Paths to combine
set DIRECTORY="C:\Directory with spaces"
set FILENAME="sub directory\filename.txt"

rem Combine two paths
set COMBINED="%DIRECTORY:"=%\%FILENAME:"=%"
echo %COMBINED%

rem This is just to illustrate how the = operator works
set DIR_WITHOUT_QUOTES=%DIRECTORY:"=%
echo %DIR_WITHOUT_QUOTES%
© www.soinside.com 2019 - 2024. All rights reserved.