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
  • 14,997
  • 4
  • 32
  • 41

1 Answers1

9
@echo off
set foo="%~1\bar"
echo %foo%
Bali C
  • 29,317
  • 35
  • 118
  • 150
  • No problem, glad I could help :) – Bali C Nov 15 '12 at 18:00
  • 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