在Windows批处理中转义特殊字符 [英] Escape special character in a windows batch

查看:1025
本文介绍了在Windows批处理中转义特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,该文件接收路径作为第一个参数.路径始终由特殊字符组成,例如^éè.

I have a batch file that receive a path as first argument. The path is always composed with specials characters like ^,é or è.

通话与此类似

D:\Script>MyBatch My\path\test_00170_LASTNAME^Firstname\image

总会出现此错误:未知的指定路径

There is always this error: unknown specified path

当我在bash中回显第一个参数时,我可以看到(注意缺少的^)

When I echo the first argument in my bash I can see (notice the missing ^)

My\path\test_00170_LASTNAMEFirstname\image

所以我试图通过在前面添加另一个^来逃脱此字符

So I tried to escape this character by adding another ^ just before

My\path\test_00170_LASTNAME^^Firstname\image

但是当我回显这一点时,我得到的结果是相同的...

But when I echo this one, I have the same result ...

My\path\test_00170_LASTNAMEFirstname\image

我还尝试将^放在引号之间,但这不起作用

I also tried to put the ^ between quotes but this did not work

推荐答案

您需要在命令行中转义插入符号,或者最好将路径放在引号中.

You need to escape the caret signs at the command line or better put the path into quotes.

在两种情况下,您都应使用延迟扩展,因为扩展时内容将不会被修改.

In both cases you should work with delayed expansion, as then the content will not be modified when it is expanded.

myBatch "C:\LASTNAME^Firstname\image"

myBatch C:\LASTNAME^^Firstname\image

然后在批处理中使用像这样的

And in your batch use swomething like this

@echo off
set "arg1=%~1"
setlocal EnableDelayedExpansion
echo !arg1!

这篇关于在Windows批处理中转义特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆