Octal Decimal and Leading Zeros
Posted: 24 Dec 2009 04:49
Hello
I'm a beginner trying to write a script that I thought would be simple but is turning out a bit tricky. I am trying to generate a list of station numbers from a given input of first and last station name eg sws001 to sws500. My problem is that the 3 digit number format seems to cause dos to interpret the numbers as being Octal (eg it refuses to accept 008 or 009). I have tried to strip off the leading zeros to make it appear decimal but it still doesn't work.
Here are two attempts, first is simple and the second not so tidy and i'm only really interested in sorting out the numbers - the sws bit I can get later.
Any help is gratefully appreciated, thanks
FIRST ATTEMPT:
@echo off
echo
set /p first= First station?
set /p last= Last Station?
set prefirst=%first:~0,3%
set prelast=%last:~0,3%
set prefix=SWS
REM is it sws or priws
if %prefirst%==%prefix% (goto :SWSFIRST) else (goto RIFIRST)
:SWSFIRST
REM test for leading zero 0xx and if found remove it
if %first:~3,1%==0 (set /a endfirst=%first:~-2%) else (set /a endfirst=%first:~-3%)
goto :SWSLAST
RIFIRST
REM test for leading zero 0xx and if found remove it
if %first:~5,1%==0 (set /a endfirst=%first:~-2%) else (set /a endfirst=%first:~-3%)
goto RILAST
:SWSLAST
REM test for leading zero 0xx and if found remove it
if %last:~3,1%==0 (set /a endlast=%last:~-2%) else (set /a endlast=%last:~-3%)
goto :CALC
RILAST
REM test for leading zero 0xx and if found remove it
if %last:~5,1%==0 (set /a endlast=%last:~-2%) else (set /a endlast=%last:~-3%)
goto :CALC
:CALC
set /a counter=%endfirst%
goto :NUMBERS
:NUMBERS
if %counter% ==%endlast% (goto :fin) else (echo.%prefix%%counter% >> E:\Documents\mybats\stations.txt)
set /a counter=%counter%+1
goto :numbers
:fin
exit
SECOND ATTEMPT:
@echo off
echo
set /p first= First station?
set /p last= Last Station?
set /p prefirst=%first:~0,3%
set /p prelast=%last:~0,3%
set /a fx00=%first:~3,1%
set /a f0x0=%first:~2,1%
set /a f00x=%first:~-1%
set /a lx00=%last:~3,1%
set /a l0x0=%last:~2,1%
set /a l00x=%last:~-1%
set /p endfirst=%first:~-3%
set /p endlast=%last:~-3%
set /a numfirst=%fx00%%f0x0%%f00x%
set /a numlast=%lx00%%l0x0%%l00x%
REM check for leading zeros where no is eg SWS001
REM if its 0xx goto step 1 else step2
if %endfirst:~0,1%==0 (goto :zerostep1) else (goto :zerostep2)
:zerostep1
REM if its 00x make endfirst last number from right else last 2 nos
if %endfirst:~0,2%==0 (set /a numfirst=%f00x%) else (set /a numfirst=%f00x%%f0x0%)
pause
goto :calc
:zerostep2
REM if its 0xx goto step 3 else calc
if %endlast:~0,1%==0 (goto :zerostep3) else (goto :calc)
:zerostep3
REM if its 00x make endfirst last number from right else last 2 nos
if %endlast:~0,2%==0 (set /a numlast=%l00x%) else (set /a numlast=%l00x%%l0x0%)
pause
goto :calc
:calc
set /a counter=%numfirst%
pause
goto :numbers
:numbers
if %counter% ==%numlast% (goto :fin) else (echo.%counter% >> E:\Documents\mybats\stations.txt)
set /a counter=%counter%+1
goto :numbers
:fin
exit
I'm a beginner trying to write a script that I thought would be simple but is turning out a bit tricky. I am trying to generate a list of station numbers from a given input of first and last station name eg sws001 to sws500. My problem is that the 3 digit number format seems to cause dos to interpret the numbers as being Octal (eg it refuses to accept 008 or 009). I have tried to strip off the leading zeros to make it appear decimal but it still doesn't work.
Here are two attempts, first is simple and the second not so tidy and i'm only really interested in sorting out the numbers - the sws bit I can get later.
Any help is gratefully appreciated, thanks
FIRST ATTEMPT:
@echo off
echo
set /p first= First station?
set /p last= Last Station?
set prefirst=%first:~0,3%
set prelast=%last:~0,3%
set prefix=SWS
REM is it sws or priws
if %prefirst%==%prefix% (goto :SWSFIRST) else (goto RIFIRST)
:SWSFIRST
REM test for leading zero 0xx and if found remove it
if %first:~3,1%==0 (set /a endfirst=%first:~-2%) else (set /a endfirst=%first:~-3%)
goto :SWSLAST
RIFIRST
REM test for leading zero 0xx and if found remove it
if %first:~5,1%==0 (set /a endfirst=%first:~-2%) else (set /a endfirst=%first:~-3%)
goto RILAST
:SWSLAST
REM test for leading zero 0xx and if found remove it
if %last:~3,1%==0 (set /a endlast=%last:~-2%) else (set /a endlast=%last:~-3%)
goto :CALC
RILAST
REM test for leading zero 0xx and if found remove it
if %last:~5,1%==0 (set /a endlast=%last:~-2%) else (set /a endlast=%last:~-3%)
goto :CALC
:CALC
set /a counter=%endfirst%
goto :NUMBERS
:NUMBERS
if %counter% ==%endlast% (goto :fin) else (echo.%prefix%%counter% >> E:\Documents\mybats\stations.txt)
set /a counter=%counter%+1
goto :numbers
:fin
exit
SECOND ATTEMPT:
@echo off
echo
set /p first= First station?
set /p last= Last Station?
set /p prefirst=%first:~0,3%
set /p prelast=%last:~0,3%
set /a fx00=%first:~3,1%
set /a f0x0=%first:~2,1%
set /a f00x=%first:~-1%
set /a lx00=%last:~3,1%
set /a l0x0=%last:~2,1%
set /a l00x=%last:~-1%
set /p endfirst=%first:~-3%
set /p endlast=%last:~-3%
set /a numfirst=%fx00%%f0x0%%f00x%
set /a numlast=%lx00%%l0x0%%l00x%
REM check for leading zeros where no is eg SWS001
REM if its 0xx goto step 1 else step2
if %endfirst:~0,1%==0 (goto :zerostep1) else (goto :zerostep2)
:zerostep1
REM if its 00x make endfirst last number from right else last 2 nos
if %endfirst:~0,2%==0 (set /a numfirst=%f00x%) else (set /a numfirst=%f00x%%f0x0%)
pause
goto :calc
:zerostep2
REM if its 0xx goto step 3 else calc
if %endlast:~0,1%==0 (goto :zerostep3) else (goto :calc)
:zerostep3
REM if its 00x make endfirst last number from right else last 2 nos
if %endlast:~0,2%==0 (set /a numlast=%l00x%) else (set /a numlast=%l00x%%l0x0%)
pause
goto :calc
:calc
set /a counter=%numfirst%
pause
goto :numbers
:numbers
if %counter% ==%numlast% (goto :fin) else (echo.%counter% >> E:\Documents\mybats\stations.txt)
set /a counter=%counter%+1
goto :numbers
:fin
exit