BuckTurgidson
BuckTurgidson

Reputation: 299

batch file deal with Ampersand (&) in folder name

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 "&"

Upvotes: 3

Views: 11857

Answers (5)

user2540850
user2540850

Reputation: 21

Finally, when you are building your test data, don't limit yourself to file/folder names with just the ampersand. Be sure to include additional test cases with the greater-than sign (>), the less-than sign (<), the vertical bar (|), unbalanced left and right parentheses "(" and ")", the cap/hat (^), the percent sign (%) - URLs are a favorite, and the exclamation point (!).
For non-file/folder name inputs, include test cases with both balanced and unbalanced quotation marks (") as well.

Upvotes: 0

user2540850
user2540850

Reputation: 21

There are several other characters in addition to ampersand that will cause batch scripts to fail if they are not guarded against. These include greater-than (>), which is the output redirect symbol; less-than (<), which is the input redirect symbol; vertical bar (|), which is the pipe symbol; left and right parentheses "(" and ")", which are used for statement grouping - especially if they are unbalanced, and cap/hat (^), the escape symbol itself.

If you are using the escaping technique, you will need to escape all of these characters.

If you are using quotations to surround the string in question, then any code which handles the ampersand properly will also handle all the others.

Add to that the percent sign (%) which designates batch variables, exclamation points (!) if you have delayed expansion enabled, and quotation marks if the string you are dealing with is not a Windows filename or folder name or path. Quoting may not be adequate for these characters, because the batch script interpreter searches within quoted string to perform substitutions. You may be able to deal with percent signs and quotation marks by doubling them rather than escaping them (I'm not sure why this would be better or worse).

Upvotes: 0

Bill_Stewart
Bill_Stewart

Reputation: 24515

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%"

Upvotes: 0

jeb
jeb

Reputation: 82192

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 ^!"

Upvotes: 6

JosefZ
JosefZ

Reputation: 30103

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).

Upvotes: 1

Related Questions