简体   繁体   English

如何在 cmd/批处理文件中删除不带转义 (^) 的双引号?

[英]How to remove double quotes without escape (^) in cmd/ batchfile?

Note: as the OP is a new user, I'm editing the question with reproducible details so that it doesn't get closed.注意:由于 OP 是新用户,我正在使用可重现的详细信息编辑问题,以免它被关闭。 It's a good question, but was originally confusing.这是一个很好的问题,但最初令人困惑。

The Windows username is: jhon^smith Windows 用户名是:jhon^smith

ECHO %username%
output : jhonsmith

My expectation:我的期望:

jhon^smith

If I use:如果我使用:

echo "%username%"

then the output is:那么输出是:

"jhon^smith"

How can I remove the double quotes but keep the caret, which is actually part of the username?如何删除双引号但保留插入符号,这实际上是用户名的一部分?

This should work:这应该有效:

for /f %a in ("%username%") do echo %a

output:输出:

jhon^smith

(btw: It should be spelt john^smith but never mind.) (顺便说一句:应该拼写为john^smith但没关系。)

Explanation:解释:

In the command echo %username% , cmd.exe replaces %username% with its actual content, so the command becomes echo jhon^smith .在命令echo %username%中, cmd.exe%username%替换为其实际内容,因此命令变为echo jhon^smith This is then parsed, the the ^ is processed as the escape character, so it replaces ^s with s .然后对其进行解析,将^处理为转义字符,因此它将^s替换为s

In the command for /f %a in ("%username%") do echo %a , the quoted string is treated as a string without the quotes, and no parsing of ^ is performed.for /f %a in ("%username%") do echo %a命令中,带引号的字符串被视为不带引号的字符串,并且不执行^解析。 Type for /? for /? for more details.更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM