1

If I run something that returns with a & or some other character that has special meaning, and I try to use it in a script, how would I escape it?

An example would be if I shared a local account with someone in the same computer, and I had a username that says John & Matt. Putting a batch file in the Desktop directory that had something like this:

@ECHO OFF
echo %~dp0

It would return something like this:

C:\Users\John
The system cannot find the path specified.

And the whole script from that point on breaks if I try to keep going. If anyone knows a way to make the whole path appear, I'd appreciate it.

EDIT: I didn't clarify this enough earlier. Sorry about that. Not really good at explaining, but if I try to use %~dp0 in a variable, and stack more things on top of that, something like

SETLOCAL
set loc=%~dp0
rename "%~dp0Dir\Dir1\Dir2\File.txt" "Something.txt"

It would probably confuse the rename command if I put another set of quotation marks when I'm declaring a variable to fix that issue.

The batch file that I was trying to create basically deletes a file where the batch file is located, replaces it with another file in a subfolder located inside the directory where the batch file is, and renames it to something else.

  • I do not understand your edit. Why are you setting `loc` to `%~dp0`? You could in that case just set your `%CD%` to `%~dp0` and use `Ren Dir\Dir1\Dir2\File.txt" "Something.txt"`. – Compo Feb 20 '17 at 15:08
  • I'm new to creating batch files, so I had no idea that %CD% did something like this. I'll keep that in mind when I try to create a new batch file that plays around a lot with directories. – Roast Chicken Feb 20 '17 at 15:14
  • Use this syntax: `set "loc=%~dp0"` -- the quotation marks do not become part of the value here, but certain special characters (like `&`) are hidden from `cmd` properly... – aschipfl Feb 20 '17 at 18:00

2 Answers2

3

If you want to reliably ECHO the value without enclosing quotes, then you can use either of the following strategies:

@echo off
:: Use a FOR loop
for %%F in ("%~dp0") do echo %%~F

:: Use delayed expansion.
:: Note that delayed expansion must be enabled after assignment of fullPath,
:: else any ! within path will be corrupted during the assignment.
set "fullPath=%~dp0"
setlocal enableDelayedExpansion
echo !fullPath!
dbenham
  • 127,446
  • 28
  • 251
  • 390
0
@ECHO OFF
echo %dp0

will return dp0 - I guess you mean %~dp0 ;-)

However, echo "%~dp0" will output C:\Users\John & Matt\...

By the way, it's \ and not /.

Windows: \

Unix/Linux: /

I'm not being pedantic, this sometimes causes serious problems (like here).

Community
  • 1
  • 1
MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • Was kind of rushing it, didn't fix mistakes. Sorry about that. I'll edit it out. – Roast Chicken Feb 20 '17 at 14:34
  • @RoastChicken Actually, this DOES fix the error. If it's just about echo, surrounding your variable with quotation marks works fine. – MichaelS Feb 20 '17 at 14:36
  • Sorry if I didn't clarify, but I needed to use %~dp0 in a variable. Putting quotation marks will screw up the variable since I need to put more things on top of the path it gives me. For example, I do something like this: set loc=%~dp0 and later on, I need to put in %loc%Dir1\Dir2\Program.exe – Roast Chicken Feb 20 '17 at 14:41
  • @RoastChicken OK, in this case, it won't be easy to help you without seeing more code. Depending on what exactly you are doing with your string you will have to handle it in different ways. Please show us some more code. Worst case: simply replace the & character with lets say _~_ or something, do your string building operations and replace it back with & at the end. – MichaelS Feb 20 '17 at 14:47