是否有一种简单的方法可以在Windows批处理文件中定义和使用复杂的文字字符串而不手动逃脱每个特殊字符?
背景
我正在尝试创建一个可以使用Azcopy将文件复制到Azure Storage Explorer的批处理文件。我只想将我的SAS密钥复制并粘贴到批处理文件中,而不必每次这样做时都必须找到并逃脱所有特殊字符(&,=等)。
我的问题
如何用这些字符轻松地在批处理文件中定义一个复杂的字符串,以免它们被解释,以便我不必手动逃脱它们?那甚至可能吗?例如,是否有一些关键字或可以将其添加到基本上告诉它的集合语句中的内容:“嘿,该变量值中的所有字符都应像没有任何解释的情况下使用”?
我查看过的一些资源
- https://ss64.com/nt/sntax-esntax-esc.html
- =“ https://stackoverflow.com/questions/9757480/echo-echo-ectact-string-in-windows-batch-file”> Windows batch文件中的Echo精确字符串?
- https://wwwwwww.tutorialspoint.com/batch_script/batch_script/batch_script_script_script_variables.htm
- /Question/22331483/how-do-i-set-ster-variables-letherally-interly-interally-interly-interally-interally-In the>我如何从批处理中设置变量?
在某些语言中,您将变量值放在双引号中,以使它们成为文字。我不确定如何在批处理文件中执行此操作。
一个示例
假设我有此伪密钥:
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
实例正在解释并因此从最终值中删除。
预先感谢您提供的任何帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
百分位数甚至都不将其插入字符串(使用
set var
)中,因此我使用了带有您的精确字符串的文本文件。延迟的扩展避免了要解析的字符串:The percent-signs don't even make it into the string (verify with
set var
), so I used a text file with your exact string. Delayed expansion avoids the string to be parsed:一段时间前,我构建了
魔术
宏你上屏幕
但是对于正常使用,宏有点过分。
但是,也许您应该研究批处理的Heredoc帖子,例如
Windows batch的Heredoc?
或者
dbenham的解决方案Heredoc用于Windows批处理?
Some time ago I build a
magicEcho
macrodostips: MagicEcho
You get on the screen
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?
如果代码可以在文件中。
如果需要引用它,则
echo“ %% b”
如果文件名包含空格,请使用
“ usebackqdelims =”
andand
“%fileName%”
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%"