In fact, the character interception function of variables in CMD is very practical, but some friends haven't really understood the rules inside. So today I specially wrote the following code, hoping to help novice friends understand the character interception function of variables under CMD.
::code by lxmxn @ cn-dos.net .
::Purpose: To help novices better understand the variable character interception function.
::Date 2006-12-16 .
::Description: Some variables may have empty values after interception, which is normal.
@echo off&setlocal EnableDelayedexpansion
::Set the value of variable var:
set var=0123456789ABCDEF
call :begin
for /l %%a in (1,3,15) do (
for /l %%b in (1,4,15) do (
call :printf %%a %%b
)
)
call :begin
for /l %%a in (1,4,15) do (
for /l %%b in (-1,-3,-15) do (
call :printf %%a %%b
)
)
call :begin
for /l %%a in (-2,-3,-15) do (
for /l %%b in (1,4,15) do (
call :printf %%a %%b
)
)
call :begin
for /l %%a in (-1,-4,-15) do (
for /l %%b in (-2,-3,-15) do (
call :printf %%a %%b
)
)
pause
endlocal&cls&echo.
echo The demonstration is over, press any key to exit...
pause>nul
goto :eof
::Output result:
:printf
echo %%var:~%1,%2%% The intercepted character is: !var:~%1,%2!
goto :eof
::Clear the screen:
:begin
echo\&pause&cls&echo.
echo The value of variable var is: %var%
echo.
goto :eof
Click here to download the attachment (Getstr.rar)
[ Last edited by lxmxn on 2007-10-31 at 06:33 PM ]
Recent Ratings for This Post
( 20 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| ccwan | +5 | 2006-12-17 06:45 |
| zh159 | +5 | 2006-12-17 06:52 |
| redtek | +5 | 2006-12-17 21:20 |
| lianjiang2004 | +4 | 2006-12-17 21:30 |
| HUNRYBECKY | +5 | 2007-03-17 11:46 |
| qinbuer | +2 | 2007-06-01 02:03 |
| 26933062 | +4 | 2007-06-04 23:50 |
| zouzhxi | +4 | 2007-06-28 15:58 |
| fengjian | +1 | 2007-10-31 18:01 |
| upsco | +1 | 2007-11-21 21:18 |
| xiaohacker | +2 | 2007-12-23 20:32 |
| haiou327 | +4 | 2008-01-04 21:21 |
| 313885174 | +2 | 2008-01-26 12:34 |
| hy433124shc | +2 | 2008-02-20 19:14 |
| easonL | +2 | 2008-05-27 13:44 |
| dywjzh | -1 | 2008-09-09 11:43 |
| shifengl | +1 | 2009-05-21 08:38 |
| a7147125 | +1 | 2009-08-23 23:35 |
| mwm5 | +1 | 2009-10-17 20:49 |
| gool123456 | +1 | 2010-05-04 20:02 |

