我想在batch file中做一些string比较和提取。 该操作发生在SVN存储库中的一组文件夹名称上。
for /f %%f in ('svn list https://dev_server/svn/product/branches') do ( set folder=%%f echo Folder: %folder% :: get substring from %folder% starting at 0 with a length of %length% :: if this substring is equal to %folderStart% then get substring from %folder% starting at position %length% )
这里有几个问题:
有没有人有一个想法,我可以在上面的代码中的评论部分实现function?
感谢一百万提前。
亲切的问候,
马丁
动态子串很容易延迟扩展。
setlocal enableDelayedExpansion set "string=1234567890" ::using a normal variable set len=5 echo !string:~0,%len%! ::using a FOR variable for /l %%n in (1 1 10) do echo !string:~0,%%n!
它也可以用于搜索和替换
你的线
echo %folder%
需要是
echo !folder!
希望下面的内容能显示子字符串
@echo off setlocal ENABLEDELAYEDEXPANSION set theString=abcd for %%f in (1 2 3 4) do ( set pos=%%f call :Resolve theString:~0,!pos! echo !retval! ) goto :eof :Resolve for %%a in ("^!%*^!") do set retval=%%~a goto :eof
这使
a ab abc abcd
你有没有考虑使用Powershell而不是老式的批处理文件? Powershell在脚本方面有更多的功能