I am new to batch scripting. although i have shell and python solutions for this problem but got stuck in batch script.
I have string like 123_happy
,
probably the easiest and the fastest way:
set z=123_happy
for /l %%# in (%z%,1,%z%) do echo %%#
this will leave only the leading numbers.Though it is limited to 32b integers. As a subroutine (will fail if the input contains delimiters):
:extractLeadingNumbers input [rtrnVar]
for /l %%# in (%~1;1;%~1) do (
if "%~2" neq "" (
set "%%#=%~2"
) else (
echo %%#
)
)
More robust way (which will also remove leading zeroes):
cmd /c exit /b 123_happy
echo %errorlevel%