2

Background

I'm trying create a batch file that can copy a file to Azure Storage Explorer using azcopy. I'd like to just copy and paste my SAS key into my batch file without having to locate and escape out all the special characters (&, =, etc.) each time I do so.

My Question

How can I easily define a complex string in a batch file with those characters so that they don't get interpreted and so that I don't have to manually escape them out? Is that even possible? For example, is there some keyword or something that I can add to a set statement that basically tells it, "Hey, all the characters in this variable's value should be used as is without any interpretation"?

Some Resources I've Looked At

In some languages you put variable values in double-quotes to make them literals. I'm not sure how to do it in a batch file.

An Example

Suppose I have this pseudo key:

nifty-11-stuff%3A28GOOBER%3A5&se=2022

Say I want to preserve and use this string EXACTLY as is in a variable, so that if I use it in my batch command later it's unchanged.

I tried this code (with quotes):

@echo off
setlocal EnableDelayedExpansion
set SAS="nifty-11-stuff%3A28GOOBER%3A5&se=2022"
echo %SAS%
pause

My Terminal output in VSCode gives this:

PS D:\git\documentationutilities> cmd /c "d:\git\documentationutilities\test.bat"
"nifty-11-stuffA28GOOBERA5&se=2022"

Notice that the %3A instances are getting interpreted and thus deleted from the final value.

Thank you in advance for any help you can offer.

Jared
  • 179
  • 1
  • 10
  • 1
    Is this string in a file, or can it be? – Magoo Jun 20 '22 at 19:16
  • 1
    Batch files are pain and suffering for scenarios like this. If PowerShell is an option, strongly consider that instead. Or indeed *any* shell other than `cmd`. – Jeroen Mostert Jun 20 '22 at 19:29
  • @Magoo - Yes, I can put it in a file if needed. I was sort of thinking that reading the value from a file might be what I needed to do to get around this. – Jared Jun 20 '22 at 20:33
  • @Jeroen Mostert - I think I do have access to Powershell. I'll consider it. – Jared Jun 20 '22 at 20:34

3 Answers3

3

The percent-signs don't even make it into the string (verify withset var), so I used a text file with your exact string. Delayed expansion avoids the string to be parsed:

@echo off
setlocal EnableDelayedExpansion
<test.txt set /p "var="
set var
echo var=!var!
Stephan
  • 53,940
  • 10
  • 58
  • 91
2

Some time ago I build a magicEcho macro

dostips: MagicEcho

%magicEcho% ^%!%<> "^!%path%<>" ^

You get on the screen

^%!%<> ^ "^!%path%<>"

But for normal usage the macro is a bit of an overkill.

But perhaps you should look into the batch heredoc posts, like
heredoc for Windows batch?
or dbenham's solution heredoc for Windows batch?

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    I verified that I was able to use your code to get the exact string back that I put in with all the special characters. It's probably a bit more code than I want to include for what I'm trying to do, but regardless, I appreciate your response and am amazed at your DOS Kung Fu! – Jared Jun 20 '22 at 20:52
2
FOR /f "delims=" %%b IN (%filename%) DO ECHO %%b

if the code can be in a file.

If you need it quoted, then echo "%%b"

If the filename contains spaces, use "usebackqdelims=" and "%filename%"

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks @Magoo - I placed my key inside of a sas.txt file and verified that I was able to get the exact key to display using this code ```FOR /f "delims=" %%b IN (sas.txt) DO ECHO %%b``` – Jared Jun 21 '22 at 15:06