Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call concat : %variable% when the variable has internal spaces?

Tags:

batch-file

cmd

I am trying to concat variables in my class path using the following command, but it isn't working when the folder name contains spaces:

call concat : %variable%

where %variable% ={folder name containing space}

I tried putting quotes:

call concat : "%variable%"

but this adds 2 double-quotes in my classpath as follows:

""folder name containing space""

:concat
set CLASSPATH=%CLASSPATH%;"%1"
like image 913
Anuj Mehta Avatar asked Mar 18 '13 13:03

Anuj Mehta


People also ask

How do you concatenate variables?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect.

How do I concatenate a variable and a string in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."

How do you concatenate variables in SPSS?

Concatenating in SPSS is done by the CONCAT function. The most basic usage is COMPUTE A = CONCAT(B,C,D) . Note that you can concatenate only strings. For concatenating numbers, first convert them to strings, for example by using the stringfunction.


3 Answers

Put quotes around the entire SET statement:

SET "FOO=AB C"
SET "BAR=%FOO%D EF"
echo %BAR%

Outputs:

AB CD EF
like image 194
oggmonster Avatar answered Oct 23 '22 13:10

oggmonster


Do not use additional double quotes for strings with spaces. The parameter %~ removes all pairs of double quotes around the string:

@echo off &setlocal
set "Variable="my var""
echo Variable:  %Variable%
call :concat %Variable%
goto :eof

:concat
echo concat %%1:    %1
set "NewVar=%~1"
echo concat NewVar: %newvar%
goto :eof
endlocal

Output is:

Variable:       "my var" 
concat %1:      "my var" 
concat NewVar:  my var

If you put additional double quotes around the string, the following will happen:

@echo off &setlocal
set "Variable="my var""
echo Variable:  %Variable%
call :concat "%Variable%"
goto :eof

:concat
echo concat %%1:    %1
set "NewVar=%~1"
echo concat NewVar: %newvar%
goto :eof
endlocal

With broken output:

Variable:       "my var"
concat %1:      ""my
concat NewVar:  "my
like image 44
Endoro Avatar answered Oct 23 '22 13:10

Endoro


remove the double quote from input, use double quote around the entire set, when use the concatenated string, surrounding them by double quote, for example:

set root=%~1
@echo off
setlocal EnableDelayedExpansion
set i=0
for %%d in (
1.07 1.023 1.075 1.08 1.04
) do (
   set /A i=i+1
   set scale[!i!]=%%d
)
set i=0
for %%d in (
CO DE MH PL Pr
) do (
   set /A i=i+1
   set port[!i!]=%%d
)
for %%A in (0 1 2 3 4) do (
set "file1=%root%\Event\Events.txt"
set "file2=%root%\Port E\PortE_!port[%%A]!.txt"
set "file3=%root%\Poli E By Port\E_!port[%%A]!.txt"
%0\..\average101v2.exe "!file1!" "!file2!" "!file3!" !scale[%%A]!
)
like image 1
Frank Avatar answered Oct 23 '22 14:10

Frank