Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate variables

Tags:

batch-file

I need to do a .bat for DOS that do the following:

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

so after defining ROOT I want to have SRC_ROOT = c:\programas\System\Source

How can I do that?

like image 643
UcanDoIt Avatar asked May 13 '09 07:05

UcanDoIt


People also ask

What is concatenation in variable?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

What is an example of concatenation?

The concatenation of two or more numbers is the number formed by concatenating their numerals. For example, the concatenation of 1, 234, and 5678 is 12345678. The value of the result depends on the numeric base, which is typically understood from context.

How do you concatenate variables in Excel?

First, enter the first string using the double quotation marks. After that, type an ampersand. Next, enter the second text using the double quotation marks. In the end, assign that value to a cell, variable, or use a message box to see it.


3 Answers

set ROOT=c:\programs 
set SRC_ROOT=%ROOT%\System\Source
like image 85
podosta Avatar answered Oct 12 '22 16:10

podosta


Note that if strings has spaces then quotation marks are needed at definition and must be chopped while concatenating:

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%

Cheers...

like image 20
Hertzel Guinness Avatar answered Oct 12 '22 18:10

Hertzel Guinness


If you need to concatenate paths with quotes, you can use = to replace quotes in a variable. This does not require you to know if the path already contains quotes or not. If there are no quotes, nothing is changed.

@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_SPACES=%DIRECTORY:"=%
echo %DIR_WITHOUT_SPACES%
like image 9
VLL Avatar answered Oct 12 '22 17:10

VLL