如何从 Windows 批处理文件中的 ECHO 字符串中去除引号? [英] How do you strip quotes out of an ECHO'ed string in a Windows batch file?

查看:125
本文介绍了如何从 Windows 批处理文件中的 ECHO 字符串中去除引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在创建的 Windows 批处理文件,但我必须回显一个大的复杂字符串,所以我必须在两端加上双引号.问题是引号也被回显到我正在写入的文件中.你如何回显这样的字符串并去掉引号?

I have a Windows batch file I'm creating, but I have to ECHO a large complex string, so I'm having to put double quotes on either end. The problem is that the quotes are also being ECHOed to the file I'm writing it to. How do you ECHO a string like that and strip the quotes off?

更新:

过去两天我一直在研究这个,最后终于能够将一些东西拼凑起来.理查德的回答可以去除引号,但是即使我将 ECHO 放在子例程中并直接输出字符串,Windows 仍然挂在字符串中的字符上.我会接受理查德的回答,因为它回答了提出的问题.

I've spent the last two days working on this and finally was able to kludge something together. Richard's answer worked to strip the quotes, but even when I put the ECHO in the subroutine and directly outputted the string, Windows still got hung up on the chars in the string. I'll accept Richard's answer since it answers the question asked.

我最终使用了 Greg 的 sed 解决方案,但由于 sed/windows 错误/功能而不得不对其进行修改(它没有提供任何文档并没有帮助).在 Windows 中使用 sed 有一些注意事项:你必须使用双引号而不是单引号,你不能直接转义字符串中的双引号,你必须结束引用字符串,使用 ^ 转义(所以 ^") 然后是下一部分的 beqin 引用.另外,有人指出,如果您将输入通过管道传输到 sed,则字符串中存在管道的错误(我没有验证这一点,因为在我的最终解决方案中,我刚刚发现一种不在字符串中间包含所有引号的方法,只是删除了所有引号,我永远无法自行删除 endquote.) 感谢所有帮助.

I ended up using Greg's sed solution, but had to modify it because of sed/windows bugs/features (it didn't help that it came with no documentation). There are a few caveats to using sed in Windows: you have to use double quotes instead of single quotes, you can't escape the double quotes in the string directly, you have to endquote the string, escape using the ^ (so ^") then beqin quote for the next section. Also, someone pointed out that if you pipe input to sed, there's a bug with a pipe being in the string (I didn't get to verify this since in my final solution, I just found a way not to have all quotes in the middle of the string, and just removed all quotes, I never could get the endquote to be removed by itself.) Thanks for all the help.

推荐答案

call 命令内置了这个功能.引用 call 的帮助:

The call command has this functionality built in. To quote the help for call:

 Substitution of batch parameters (%n) has been enhanced.  You can
 now use the following optional syntax:

 %~1         - expands %1 removing any surrounding quotes (")

这是一个原始示例:

@echo off
setlocal
set mystring="this is some quoted text"
echo mystring=%mystring%
call :dequote %mystring%
echo ret=%ret%
endlocal
goto :eof

:dequote
setlocal
rem The tilde in the next line is the really important bit.
set thestring=%~1
endlocal&set ret=%thestring%
goto :eof

输出:

C:>dequote
mystring="this is some quoted text"
ret=this is some quoted text

我应该将环境变量隧道"技术(endlocal&set ret=%thestring%)归功于 Tim Hill,'Windows NT Shell 脚本'.这是我发现的唯一一本处理任何深度的批处理文件的书.

I should credit the 'environment variable tunneling' technique (endlocal&set ret=%thestring%) to Tim Hill, 'Windows NT Shell Scripting'. This is the only book I have ever found that addresses batch files with any depth.

这篇关于如何从 Windows 批处理文件中的 ECHO 字符串中去除引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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