3

Batch file below:

@echo off
set filelocation=C:\Users\myself\Documents\This&That
cd %filelocation%
echo %filelocation%
pause

give the following output:

'That' is not recognized as an internal or external command, 
 operable program or batch file.
 The system cannot find the path specified.
 C:\Users\myself\Documents\This
 Press any key to continue . . .

Considering I cannot change the folder name, how do I handle the "&"

BuckTurgidson
  • 289
  • 3
  • 8
  • 17

3 Answers3

6

You need two changes.

1) The extended set syntax with quotes prefix the variable name and at the end of the content

2) delayed expansion to use the variable in a safe way

setlocal enableDelayedExpansion
set "filelocation=C:\Users\myself\Documents\This&That"
cd !filelocation!
echo !filelocation!

Delayed expansiuon works like percent expansion, but it have to be enabled first with setlocal EnableDelayedExpansion and then variables can expanded with exclamation marks !variable! and still with percents %variable%.

The advantage of the delayed expansion of variables is that the expansion is always safe.
But like the percent expansion, where you have to double percents when you need it as content, you have to escape exclamation marks with a caret when you use it as content.

set "var1=This is a percent %%"
set "var2=This is a percent ^!"
jeb
  • 78,592
  • 17
  • 171
  • 225
  • References on the difference between "!" and "%" are welcomed – BuckTurgidson Oct 26 '15 at 17:25
  • 2
    @Bill_Stewart Powershell is just another Microsoft tool that is today _the future_ and tomorrow MS will introduce the next tool that will be _the future_ and then powershell is deprecated like all the other MS tools/technics. MS never builded anything reliable for 10 or 20 years, Something like that exists only in the linux world – jeb Oct 30 '15 at 08:03
1

Unlike Jeb, I don't think you need delayed expansion to use the variable in a safe way. Proper quotation could suffice for most use:

@echo off
SETLOCAL EnableExtensions DisableDelayedExpansion
set "filelocation=C:\Users\myself\Documents\This&That"
cd "%filelocation%"
echo "%filelocation%"
rem more examples:
dir /B "%filelocation%\*.doc"
cd
echo "%CD%"
md "%filelocation%\sub&folder"
set "otherlocation=%filelocation:&=!%" this gives expected result


SETLOCAL EnableDelayedExpansion
set "otherlocation=%filelocation:&=!%" this gives unexpected result
ENDLOCAL
pause

Moreover, this is universal solution while delayed expansion could fail in case of ! exclamation mark in processed string (e.g. last set command above).

Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • You are right, you don't need delayed expansion, but to use a variable in a safe way is nearly impossible with percent expansion (or at least a challenge). Like your example shows, you used quotes in the `echo "%CD%"`, and it's get really nasty when there can be also quotes in the variable. – jeb Oct 26 '15 at 18:45
  • @jeb nasty or clean, I don't care: IMHO double quoting percent-expanded variables with `cmd` poisonous characters seems to be at least as safe as those unquoted and `!`-expanded in delayed expansion scope. Delayed expansion _could be useful_ (and sometimes _is necessary_) however its necessity is not the case here. BTW, volatile variable `%CD%` can't contain double quotes :) – JosefZ Oct 26 '15 at 19:42
  • @jeb for `echo` you can do `for %%a in ("%filelocation%") do echo %%~a`. Not pretty, but does work. – tomasz86 Apr 12 '20 at 07:54
0

Here are two ways:

a. Quote the string; e.g:

set "filelocation=C:\Users\myself\Documents\This&That"

b. Use the escape character; e.g.:

set filelocation=C:\Users\myself\Documents\This^&That

To use that path with the cd command, enclose it in quotes.

cd /d "%filelocation%"
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • This is correct, but it solves only the first problem – jeb Oct 26 '15 at 17:10
  • `set filelocation=C:\Users\myself\Documents\This^&That` gives the same error. – BuckTurgidson Oct 26 '15 at 17:13
  • 1
    No, it's the same error message,but now it's from the line `cd %filelocation%`. Before the error occurs at the `set file...` – jeb Oct 26 '15 at 17:16
  • 1
    Indeed non of the two ways actualy solves the problem – BuckTurgidson Oct 26 '15 at 17:20
  • "non[e] of the two ways actually solves the problem" - just tested my commands and verified they work. Not sure what you're referring to. As JosefZ pointed out, since `&` is `cmd.exe`'s command separator character, you can prevent its interpretation by quoting or escaping. – Bill_Stewart May 25 '17 at 16:45