从文件中提取字符串的批处理脚本
我已经查找了一些字符串操作例程,并且知道我可以用其他几种语言来执行此操作,但为了简单起见,我希望将其放在批处理文件中。
我希望它在 XML 文件中搜索标签并提取从那里到文件末尾的所有内容。
所以我想,例如在伪javascript中:
marketIndex = str.indexOf("<Markets>");
length = str.length;
marketString = str.substring(markeIndex, length-1);
return str;
我在bat中有一个子字符串函数:
:Substring
::Substring(retVal,string,startIndex,length)
:: extracts the substring from string starting at startIndex for the specified length
SET string=%2%
SET startIndex=%3%
SET length=%4%
if "%4" == "0" goto :noLength
CALL SET _substring=%%string:~%startIndex%,%length%%%
goto :substringResult
:noLength
CALL SET _substring=%%string:~%startIndex%%%
:substringResult
set "%~1=%_substring%"
GOTO :EOF
和一个字符串函数的长度:
:StrLength
::StrLength(retVal,string)
::returns the length of the string specified in %2 and stores it in %1
set #=%2%
set length=0
:stringLengthLoop
if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)
::echo the string is %length% characters long!
set "%~1=%length%"
GOTO :EOF
所以我想我在bat中缺少一个indexOf()函数...
I've looked around for some string manipulation routines, and am aware that I could do this in several other languages, but I'd like it in a batch file for simplicity.
I wish it to search an XML file for a tag and extract everything from there to the end of the file.
So I guess, for example in pseudo-javascript:
marketIndex = str.indexOf("<Markets>");
length = str.length;
marketString = str.substring(markeIndex, length-1);
return str;
I have a substring function in bat:
:Substring
::Substring(retVal,string,startIndex,length)
:: extracts the substring from string starting at startIndex for the specified length
SET string=%2%
SET startIndex=%3%
SET length=%4%
if "%4" == "0" goto :noLength
CALL SET _substring=%%string:~%startIndex%,%length%%%
goto :substringResult
:noLength
CALL SET _substring=%%string:~%startIndex%%%
:substringResult
set "%~1=%_substring%"
GOTO :EOF
and a length of a string function:
:StrLength
::StrLength(retVal,string)
::returns the length of the string specified in %2 and stores it in %1
set #=%2%
set length=0
:stringLengthLoop
if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)
::echo the string is %length% characters long!
set "%~1=%length%"
GOTO :EOF
so I guess I'm missing an indexOf() function in bat...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也可在我的 SVN 中。请注意,我更改了
strLength
的定义来修复一些错误:进一步注意,所有这些都不会帮助您,因为使用批处理文件解析 XML 不是您应该做的事情。
Also available in my SVN. Note that I altered the definition of
strLength
to fix some bugs:Note further, that all this will not help you at all as parsing XML with batch files is not something you should do.