如何在批处理文件中的for循环中执行可变长度的子字符串? [英] How to do substring with variable length in a for loop inside batch files?
本文介绍了如何在批处理文件中的for循环中执行可变长度的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在批处理文件中进行一些字符串比较和提取.该操作在SVN信息库中的一组文件夹名称上进行.
I am trying to do some string comparison and extraction in a batch file. The operation takes place on a set of folder names from a SVN repository.
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%
)
这里有两个问题:
- 由于某些原因,未将%% f的值分配给%folder%.
- 即使我已经在网络上进行了广泛的搜索,我也没有找到解决长度可变的子字符串的解决方案.批处理文件子字符串函数:〜似乎只采用固定的整数值.
有人知道我如何实现上面代码中注释部分中的功能吗?
Does anyone have an idea how I could implement the functions in the commented section in the code above?
推荐答案
动态子字符串易于扩展,易于扩展.
Dynamic substring is easy with delayed expansion.
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!
它还可以用于搜索和替换
It can also work with search and replace
这篇关于如何在批处理文件中的for循环中执行可变长度的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文