这不是问题,但请继续阅读本文:[http://ss64.com/nt/syntax-substring.html]。我对该例程的问题是它在FOR循环中不起作用。所以我与其中一位作者联系,因此#Ritchie Lawrence#得到了这个子程序的全部功劳:
@echo off
echo 123456789-1-3>strManip.txt
echo 123456789-2-3>>strManip.txt
echo 123456789-3-3>>strManip.txt
echo 123456789-3-4>>strManip.txt
echo 123456789-3-5>>strManip.txt
echo 123456789-3-6>>strManip.txt
Setlocal EnableDelayedExpansion
set MyResult=
for /f "tokens=1-3 delims=-" %%a in (strManip.txt) do (
call :strManip %%a %%b %%c MyResult
echo Mainr:[!MyResult!]
)
endlocal
del strManip.txt >nul
pause
exit
rem *** subroutine
:strManip
setlocal
set string=%1
set start=%2
set end=%3
call call set _string=%%string:~%start%,%end%%%
echo Subr:{%_string%}
endlocal & (set %4=%_string%)
goto :EOF
Output:
Subr:{234}
Mainr:[234]
Subr:{345}
Mainr:[345]
Subr:{456}
Mainr:[456]
Subr:{4567}
Mainr:[4567]
Subr:{45678}
Mainr:[45678]
Subr:{456789}
Mainr:[456789]
Press any key to continue . . .
显然,你可以/也可以在其他情况下应用这种技术(不仅仅是字符串操作,我的意思)。
答案 0 :(得分:3)
无论如何,当您使用延迟扩展时,可以这样做:
@echo off
(
echo 123456789-1-3
echo 123456789-2-3
echo 123456789-3-3
echo 123456789-3-4
echo 123456789-3-5
echo 123456789-3-6
)>strManip.txt
Setlocal EnableDelayedExpansion
for /f "tokens=1-3 delims=-" %%a in (strManip.txt) do (
set "MyResult=%%a"
echo Mainr:[!MyResult:~%%b,%%c!]
)
endlocal
del strManip.txt >nul
pause