windows - How do I get the number at the end of a string in batch? -
i'm writing batch command file uses local machine's hostname, , need extract number @ end of string. how number @ end of string in batch?
input:
w2008r2t001 w2008r2t002 w2008r2t003 w8_1_901 qatest84 qatest85 qatest86
desired output:
001 002 003 901 84 85 86
here little batch script written myself.
@echo off setlocal enableextensions enabledelayedexpansion set "hostname=w2008r2t001" set /p "hostname=enter host name (default %hostname%): " call :getnumber "%hostname%" if "%number%" == "" ( echo host name has no number @ end. ) else ( echo found number @ end of host name is: %number% ) pause endlocal goto :eof :getnumber set "number=" set "stringtoparse=%~1" set "digits=0123456789" :checklastchar if "!digits:%stringtoparse:~-1%=!" equ "%digits%" goto:eof set "number=%stringtoparse:~-1%%number%" set "stringtoparse=%stringtoparse:~0,-1%" if "%stringtoparse%" neq "" goto checklastchar goto :eof
this batch file lets user enter string. string subroutine getnumber
called using delayed environment variable expansion enabled @ beginning of main batch routine copy digits @ end of string parse in right order environment variable number
.
the main routine evaluates value of environment variable number
, continues processing accordingly.
for details on how works, open command prompt window, execute there following commands, , read pages output each command.
call /?
goto /?
if /?
set /?
Comments
Post a Comment