ããããã¡ã¤ã«ã§é åã使ãï¼ç°å¢å¤æ°ã使ã£ã¦é åå¤æ°é¢¨ã«ä½¿ãï¼æ¹æ³ã«ã¤ãã¦
ãã®ã¯ã¦ãªãã¤ã¢ãªã¼ã§ã¯èªåçã«åã¨ã³ããªã¼ã®ä¸ã«ãªã³ã¯å ã表示ããã¾ããç°¡æçãªã¢ã¯ã»ã¹ãã°ã¿ãããªããã§ãããæè¿ãããããã¡ã¤ã«ãé åãã¨ãããã¼ã¯ã¼ãã§è¦ã«æ¥ã¦ãã人ãå¤ããã¨ãå¤æãã¾ããã
ã¤ã¾ããããããã¡ã¤ã«ã§é åå¤æ°ã使ãããã使ãæ¹ãç¥ãããã¨ãããã¨ã§ããã
çµè«ããè¨ãã¨ãé
åå¤æ°ãã®ãã®ã¯ããããã¡ã¤ã«ã«ã¯ããã¾ããã
ãããé
åå¤æ°é¢¨ã®ãã®ãä½ã£ã¦ä½¿ããã¨ã¯ã§ãã¾ãã
ã¾ãã¯é åå¤æ°ãå®ç¾ãã¦ãã風ã«è¦ãããµã³ãã«ããã
æã£åãæ©ããé åå¤æ°é¢¨ã®ãã®ã使ã£ããµã³ãã«ããè¦ããã¾ãã
:--------------------------------------------sample.bat echo off for /L %%i in (1,1,10) do set x[%%i]=%%i echo x[1]=%x[1]% echo x[8]=%x[8]% echo. set x echo. set p=4 call echo call echo ã®å ´å --- x[4]=%%x[%p%]%% for /L %%i in (1,1,10) do ( call echo %%x[%%i]%% ) setlocal ENABLEDELAYEDEXPANSION echo setlocalã®å ´å --- x[4]=!x[%p%]! for /L %%i in (1,1,10) do ( echo !x[%%i]! ) echo. set sum=0 for /L %%i in (1,1,10) do ( if %%i == 5 call set ans5=%%x[%%i]%% if %%i == 7 set ans7=!x[%%i]! call set /a sum = sum + %%x[%%i]%% ) echo ans5=%ans5% echo ans7=%ans7% echo sum=%sum%
ä½ã¨ãªãå¤ã£ã¦ãã¾ãããï¼
è¦ã¯ãâå¤æ°å[æ°å]âã¨ããç°å¢å¤æ°åã使ã£ã¦ããã ãã§ããããã"[" æ°å "]"ã¯æ·»ãåã§ã¯ãªãããã¾ã§ç°å¢å¤æ°åã®ä¸é¨ã§ãã
é åå¤æ°é¢¨ã®é åçãªä½¿ãæ¹ã£ã¦ï¼
ãªãé
åã使ããããã¨ããã¨ãé
åã¨ããã®ã¯æ·»ãåã®ä¸ã®æ°å¤ãå¤æ°ã«ãã¦ãã«ã¼ããããåç
§ããããããããã§ãã
æ®éã®è¨èªã®æè¦ã§ããããæ·»ãåã®ä¸ãå¤æ°ã«ãã¦ããã®ã¾ã¾
echo x[i]
ã¨è¡¨ç¤ºãããã
set a=x[i]
ã¨ãã風ã«ä»£å ¥ãããã§ãã¾ãããï¼ãã®å ´åiãå¤æ°ï¼ããããã¯ç°å¢å¤æ°ã%i%ã¨ãã風ã«"%"ã§ããã£ã¦åç §ããã®ã§ãåãããã«èãã¦
echo %x[%i%]%
ã
set a=%x[%i%]%
ãªã©ã¨æ¸ãã¦ä¸æãåããªãã¦æ©ãããã§ãã
ãããã©ãæ¸ãã°ãããã以ä¸ã«ã¾ã¨ãã¾ãã
é åå¤æ°é¢¨æ¸ãæ¹ã®ã¾ã¨ã
ï¼æ·»ãåã«ä½¿ãç°å¢å¤æ°ãiã¨ãã¾ããããããã set i=1 ãªã©ãå®è¡ããã¦ãããï¼
â æ·»ãåã®æ°å¤ãåºå®ã®å ´å ã ã¤ã¾ãããã¯ãã ã®ç°å¢å¤æ°ã§ãã
é
åã¸ã®ä»£å
¥ãããããset x[1]=sample
é
åã®è¡¨ç¤ºããããããecho %x[1]%
é
åãä»ã®å¤æ°ã¸ä»£å
¥ãset ans=%x[1]%
â æ·»ãåã«å¤æ°ã使ãããå ´å ã call echo , call set ã使ãå ´å
é
åã¸ã®ä»£å
¥ãããããset x[%i%]=sample
é
åã®è¡¨ç¤ºããããããcall echo %%x[%i%]%%
é
åãä»ã®å¤æ°ã¸ä»£å
¥ãcall set ans=%%x[%i%]%%
ã©ãããçå±ã説æãã¾ãã¨ã
(1)call echo %%x[%i%]%% ã®è¡ãè©ä¾¡ãããã%%â%ã%i%â1(ç°å¢å¤æ°iã®ä¸èº«)ã«ç½®ãæããããã
(2)call echo %x[1]% ã¨ãã¦callæãå®è¡ããçµã¿è¾¼ã¿ã³ãã³ãã®echoãåããã»ã¹ã§å®è¡ãããï¼ãããï¼ã
(3)çµæã¨ãã¦ãecho %x[1]% ã¨ããè¡ãè©ä¾¡ããã¦ãç°å¢å¤æ°x[1]ã®ä¸èº«ã表示ãããã
ã¨ãããã¨ã«ãªãã¾ãã
â æ·»ãåã«å¤æ°ã使ãããå ´å ã setlocal ENABLEDELAYEDEXPANSION ã使ãå ´å
ããããããããã®å
é ï¼éä¸ã§ãè¯ãï¼ã§
setlocal ENABLEDELAYEDEXPANSION
ã¨å®£è¨ãã¦ããã¾ãããã®å¾ä»¥ä¸ã®ããã«è¨è¿°ãã¾ãã
é
åã¸ã®ä»£å
¥ãããããset x[%i%]=sample
é
åã®è¡¨ç¤ºããããããecho !x[%i%]!
é
åãä»ã®å¤æ°ã¸ä»£å
¥ãset ans=!x[%i%]!
以ä¸ã¾ã¨ãçµããã
call set,call echo ã使ãããsetlocal ENABLEDELAYEDEXPANSIONã使ããã¯å¥½ã¿ã§æ±ºãã¦ãã ãã:-)
ä½è«
ãæ·»ãåã®ä¸ã«ä½¿ãç°å¢å¤æ°ã®ä¸èº«ã¯æ°å¤ã§ããå¿
è¦ã¯ããã¾ãããæååã§ããã¾ãã¾ããããªããªãæ°åã§ãããã¨æååã§ãããã¨ç°å¢å¤æ°åã®ä¸é¨ã ããã§ããä¸æã使ãã°é£æ³é
åã®ããã«ä½¿ãã¾ãã
ãã¾ããæ·»ãåã®"[" "]"ãç¡ãã¦ããã¾ãã¾ãããåºåããããããããããããã«ä½¿ã£ã¦ããã ãã§ãã使ããªãã¨ç¹ã«call set ãã使ã£ãæã«"%%x%i%%%"ãªã©ã¨%ãç¶ãã¦ãããã«ããã§ãããï¼åèã¨ã³ããªâYahoo!知恵袋(バッチファイルで反復処理) バッチで配列変数のような添え字を環境変数で使う - バッチもん研究所 blogï¼ã
ããããªãã£ãããã³ã¡ã³ãæ¬ã§ã質åãã ããã質å大æè¿ï¼