你如何剥离出的报价在Windows批处理文件ECHO'ed字符串? [英] How do you strip quotes out of an ECHO'ed string in a Windows batch file?

查看:137
本文介绍了你如何剥离出的报价在Windows批处理文件ECHO'ed字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我创建一个Windows批处理文件,但我有呼应的大型复杂的字符串,所以我不得不把双引号的两端。问题是,报价也正在回显到我它写入文件。你怎么ECHO这样的字符串,并剥去引号了吗?

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仍然得到了挂在字符串中的字符。我会接受Richard的答案,因为它回答了提出的问题。

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的/窗口的bug /功能(这并没有帮助,才出现了没有文档)的。有几个注意事项,以在Windows中使用sed的:你必须使用双引号代替单引号,你不能直接逃脱字符串中的双引号,你必须endquote字符串,逃生使用^(^这么 ),然后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.

推荐答案

调用命令内置了这个功能引述呼叫的帮助:

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 (")

下面是一个基本的例子:

Here is a primitive example:

@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&安培;设置RET =%thestring%)添山, 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'ed字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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