从文件中提取字符串的批处理脚本

发布于 2024-10-07 00:03:56 字数 1148 浏览 0 评论 0原文

我已经查找了一些字符串操作例程,并且知道我可以用其他几种语言来执行此操作,但为了简单起见,我希望将其放在批处理文件中。

我希望它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫多说 2024-10-14 00:03:56
rem indexof result haystack needle
:indexof
setlocal enabledelayedexpansion enableextensions
set result=
set "haystack=%~2"
call :strlength length %3
call :strlength haylength %2
set /a max=haylength-length
for /l %%i in (0,1,%max%) do (
    if "!haystack:~%%i,%length%!"=="%~3" (set result=%%i&goto indexofDone)
)
set result=-1
:indexofDone
endlocal && set %~1=%result%
goto :eof

也可在我的 SVN 中。请注意,我更改了 strLength 的定义来修复一些错误:

:strlength
setlocal enableextensions
set "#=%~2"
set length=0
:stringLengthLoop
if defined # (set "#=%#:~1%"&set /A length+=1&goto stringLengthLoop)
endlocal && set "%~1=%length%"
GOTO :EOF

进一步注意,所有这些都不会帮助您,因为使用批处理文件解析 XML 不是您应该做的事情。

rem indexof result haystack needle
:indexof
setlocal enabledelayedexpansion enableextensions
set result=
set "haystack=%~2"
call :strlength length %3
call :strlength haylength %2
set /a max=haylength-length
for /l %%i in (0,1,%max%) do (
    if "!haystack:~%%i,%length%!"=="%~3" (set result=%%i&goto indexofDone)
)
set result=-1
:indexofDone
endlocal && set %~1=%result%
goto :eof

Also available in my SVN. Note that I altered the definition of strLength to fix some bugs:

:strlength
setlocal enableextensions
set "#=%~2"
set length=0
:stringLengthLoop
if defined # (set "#=%#:~1%"&set /A length+=1&goto stringLengthLoop)
endlocal && set "%~1=%length%"
GOTO :EOF

Note further, that all this will not help you at all as parsing XML with batch files is not something you should do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文