1)あなたがdelayed expansionで試すことができ:
@Echo off
setlocal enableDelayedExpansion
SET _startchar=2
SET _length=1
SET _donor=884777
SET _substring=!_donor:~%_startchar%,%_length%!
ECHO (%_substring%)
2)が、それはあなたが必要CALL SET
で動作させるために、二重%
(それは)最初のアプローチよりも遅くなります。
@Echo off
::setlocal enableDelayedExpansion
SET _startchar=2
SET _length=1
SET _donor=884777
call SET _substring=%%_donor:~%_startchar%,%_length%%%
ECHO (%_substring%)
3)あなたは遅れ拡大とforループを使用することができます。ブラケット・ブロックがネストされている場合、それは便利ですし、_startcharと_lengthはthere.StillがCALL SET
よりも速くなります定義されています:あなたがサブルーチンを使用することができます
@Echo off
setlocal enableDelayedExpansion
SET _startchar=2
SET _length=1
SET _donor=884777
for /f "tokens=1,2 delims=#" %%a in ("%_startchar%#%_length%") do (
SET _substring=!_donor:~%%a,%%b!
)
ECHO (%_substring%)
4)と遅延拡張:
@Echo off
setlocal enableDelayedExpansion
SET _startchar=2
SET _length=1
SET _donor=884777
call :subString %_donor% %_startchar% %_length% result
echo (%result%)
exit /b %errorlevel%
:subString %1 - the string , %2 - startChar , %3 - length , %4 - returnValue
setlocal enableDelayedExpansion
set "string=%~1"
set res=!string:~%~2,%~3!
endlocal & set "%4=%res%"
exit /b %errorlevel%
注:SS64の例はどちらも動作しません。 – SteveFest
1 .CMDファイル内のすべてのものを入れて、それがバッチファイルパーサーは、コマンドラインパーサよりもわずかに異なる動作します – SBF
動作します。前者は '%%'を '%'に変換しますが、後者はそうではありません。したがって、コマンドプロンプトではなくバッチファイルでコードを使用することができます。この参照してください:[Windowsのコマンド・インタプリタ(CMD.EXE)は、スクリプトを解析しない方法は?](https://stackoverflow.com/a/4095133) – aschipfl