ããããã¡ã¤ã«ã§ãsetlocalãendlocalå ã§ã®å¤æ°ã®å¤ãå¤é¨ã«å¼ãç¶ãããï¼
setlocal/endlocalã³ãã³ãã¯ç°å¢å¤æ°ã®ãã¼ã«ã«åã®ããã«ããã¾ãã
ãã¼ã«ã«åã¨ã¯setlocalããendlocalã®ç¯å²å
ã§å®ç¾©ããå¤æ°ããã®ä¸ã ãæå¹ã§ãã®å¤é¨ã®ååã®å¤æ°ã«å½±é¿ãåã¼ããªãããã«ãããã¨ã§ãã
@echo off setlocal set a=1 echo %a% endlocal echo %a% goto :EOF
ãå®è¡ããã¨
1 ECHO 㯠<OFF> ã§ãã
ã¨ãªãã¾ãã
å
¥ãåã«ãã§ãã¾ãã
@echo off setlocal set a=0 setlocal echo %a% set a=1 setlocal echo %a% set a=2 echo %a% endlocal echo %a% endlocal echo %a% endlocal goto :EOF
ãå®è¡ããã¨
0 1 2 1 0
ã¨ãªãã¾ããå¤å´ããå
å´ã¸ã¯ååã®å¤æ°ã®å¤ãä¿åããã¾ãããendlocalã®ãã¨ã¯åä¸é層ã§setããå¤ã復å
ããã¦ãã¾ãã¾ãã
setlocalã«ã¯enabledelayedexpansionï¼é
延ç°å¢å¤æ°ã®å±éï¼ã¨ããæç¨ãªãªãã·ã§ã³ãããã¾ããããããããã使ããµãã«ã¼ãã³ã¯setlocal-endlocalã®å
å´ã«ãªã£ã¦ãã¾ãã¾ããé
延ç°å¢å¤æ°ã®å±éã®æ©è½ã使ã£ã¦ä½ããæ±ããã®ã«ãendlocalã§æ¶ãã¦ãã¾ãã®ã¯ãã£ãããªãã§ãã
ããããããããã®ã³ãã³ãã¯ãå¤å´ãã«å½±é¿ããããªããããã«ããã®ã§ãããå
å´ããå¤å´ã«å¤ã渡ããããã¨ããã°ãã°ããã¾ããã©ãããã°ããã®ã§ãããï¼
å®ã¯ã以ä¸ã®ããã«ããã¨â¦
@echo off setlocal set a=0 setlocal echo %a% set a=1 setlocal echo %a% set a=2 echo %a% endlocal && set a=%a% echo %a% endlocal && set a=%a% echo %a% endlocal && set a=%a% goto :EOF
ãããå®è¡ããã¨
0 1 2 2 2
ã¨ãªãendlocalã®å¤ã«å¤ãå¼ãç¶ãã¾ããï¼
ãããç¨ãã¦ã以åæ¸ããã¨ã³ããªã®文字列の部分文字列を取得するサブルーチンãã©ãã§ããããæã£ã¦ãããããã«æ¸ãç´ãã¦ã¿ã¾ãã
::é¨åæåååå¾ é¨åæååãåå¾ãã ::使ç¨æ³ call :GET_SUBSTR (æåå) (éå§ã¤ã³ããã¯ã¹) (é¨åæååã®é·ã) ::ãããããã®ãã¨ã§ãsubstrã®ä¸ã«æ ¼ç´ :GET_SUBSTR setlocal enabledelayedexpansion set str=%1 set startIndex=%2 set length=%3 ::setlocalã§enabledelayedexpansionã宣è¨ãã¦ããã®ã§ä»¥ä¸ã®è¨è¿°ãå¯è½ã set substr=!str:~%startIndex%,%length%! endlocal && set substr=%substr% exit /b
ãããªããenabledelayedexpansionã¤ãã®setlocalãå¤å´ã«ãªããããã®ä¸ã§ãã³ãã¼ï¼ãã¼ã¹ãã§ä½¿ç¨å¯è½ã§ãã