是否有一种简单的方法可以在Windows批处理文件中定义和使用复杂的文字字符串而不手动逃脱每个特殊字符?

发布于 2025-02-08 21:06:32 字数 1600 浏览 2 评论 0 原文

背景

我正在尝试创建一个可以使用Azcopy将文件复制到Azure Storage Explorer的批处理文件。我只想将我的SAS密钥复制并粘贴到批处理文件中,而不必每次这样做时都必须找到并逃脱所有特殊字符(&,=等)。

我的问题

如何用这些字符轻松地在批处理文件中定义一个复杂的字符串,以免它们被解释,以便我不必手动逃脱它们?那甚至可能吗?例如,是否有一些关键字或可以将其添加到基本上告诉它的集合语句中的内容:“嘿,该变量值中的所有字符都应像没有任何解释的情况下使用”?

我查看过的一些资源

在某些语言中,您将变量值放在双引号中,以使它们成为文字。我不确定如何在批处理文件中执行此操作。

一个示例

假设我有此伪密钥:

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

说我想保留并准确地使用此字符串,以便在变量中,以便我使用在我的批处理命令中,它没有变化。

我尝试了此代码(带引号):

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

我在VSCODE中的终端输出给出了以下方式:

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

请注意,%3A 实例正在解释并因此从最终值中删除。

预先感谢您提供的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何必那么矫情 2025-02-15 21:06:32

百分位数甚至都不将其插入字符串(使用 set var )中,因此我使用了带有您的精确字符串的文本文件。延迟的扩展避免了要解析的字符串:

@echo off
setlocal EnableDelayedExpansion
<test.txt set /p "var="
set var
echo var=!var!

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!
染年凉城似染瑾 2025-02-15 21:06:32

一段时间前,我构建了魔术

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

你上屏幕

^%!%&lt;&gt; ^“^!%path%&lt;&gt;”

但是对于正常使用,宏有点过分。

但是,也许您应该研究批处理的Heredoc帖子,例如
Windows batch的Heredoc?
或者
dbenham的解决方案Heredoc用于Windows批处理?

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?

女皇必胜 2025-02-15 21:06:32
FOR /f "delims=" %%b IN (%filename%) DO ECHO %%b

如果代码可以在文件中。

如果需要引用它,则 echo“ %% b”

如果文件名包含空格,请使用“ usebackqdelims =” and and “%fileName%”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文