batch file:在for循环中如何做可变长度的子string

我想在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% ) 

这里有几个问题:

  1. 出于某种原因,%% f的值未被分配给%folder%。
  2. 即使我已经广泛地searchnetworking,我没有find一个解决scheme来做可变长度的子string。 batch filesubstring函数:〜似乎只有固定的整数值。

有没有人有一个想法,我可以在上面的代码中的评论部分实现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在脚本方面有更多的功能