连接变量

时间:2009-05-13 07:37:28

标签: batch-file

我需要为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

我该怎么做?

3 个答案:

答案 0 :(得分:54)

set ROOT=c:\programs 
set SRC_ROOT=%ROOT%\System\Source

答案 1 :(得分:8)

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

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%

干杯...

答案 2 :(得分:1)

如果需要用引号连接路径,则可以使用=替换变量中的引号。这不需要您知道路径是否已经包含引号。如果没有引号,则保持不变。

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

rem This is just to illustrate how the = operator works
set DIR_WITHOUT_SPACES=%DIRECTORY:"=%
echo %DIR_WITHOUT_SPACES%

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