5

As an example, I created a batch file named concatenate.bat:

@echo off
set foo=%1\bar
echo %foo%

When I run concatenate.bat "C:\somewhere\with spaces"

I want foo to output: "C:\somewhere\with spaces\bar"

But instead I get: "C:\somewhere\with spaces"\bar


I also tried: set "foo=%1\bar"

Which outputs: "C:\somewhere\with spaces"\bar


What's the correct way to do this?

Anders
  • 15,227
  • 5
  • 32
  • 42

1 Answers1

9
@echo off
set foo="%~1\bar"
echo %foo%
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • 1
    Will someone explain what is going on in this answer, please? – DevJem Aug 14 '17 at 02:40
  • 1
    Explanation on what %~1 does can be found in [this answer](https://stackoverflow.com/questions/47426129/difference-between-1-and-1-in-batch). In short it expands %1 removing any surrounding quotes. – da-sha1 Apr 25 '19 at 13:58