Octal Decimal and Leading Zeros

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
robbot
Posts: 5
Joined: 24 Dec 2009 04:17

Octal Decimal and Leading Zeros

#1 Post by robbot » 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 :PRIFIRST)

: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

:PRIFIRST
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 :PRILAST

: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

:PRILAST
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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#2 Post by ghostmachine4 » 24 Dec 2009 07:20

what exactly is the problem you are trying to solve??

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 25 Dec 2009 23:11

robbot,

To process a number with up to 3 digits in the "set /a" command and avoid problems with leading zeros, simply prefix the number with 1000 during assignment and use modulo arithmetic to remove the added value, e.g.:

Code: Select all

set numstr=002
set /a num=1000%numstr% %% 1000
rem now num equals 2

Your case however may be simpler. If you want the zero prefix in your output, simply work with the new large number you got and then only use the last 3 characters of the number for your output.

Code: Select all

@Echo Off
Setlocal Enabledelayedexpansion

set /p fst= First station?
set /p lst= Last Station?
for /l %%N in (1000%fst:~3%, 1, 1000%lst:~3%) do (
    set n=%%N
    echo.!n:~-3!
)
goto:eof

Output:

Code: Select all

 First station? sws001
 Last Station? sws012
001
002
003
004
005
006
007
008
009
010
011
012


DosItHelp? :wink:

robbot
Posts: 5
Joined: 24 Dec 2009 04:17

#4 Post by robbot » 26 Dec 2009 15:41

Thanks very much for your help.

GhostMachine - I am just a beginner with dos scripting (did a bit of vba and javascript in a previous life) so this is me trying to get familiar with variables and loops. Up til now I have only used scripts of 1 or 2 lines targeting a single station. If I have to target more than 1 station then I duplicate the lines and explicitly name each station. Ok except each site I work in has 100's of stations. Also as I document processes I am responsible for I want colleagues to be able to pick up my documentation and if they are to use any scripts that I use then I want them to be able to just type in station names in the format they are used to.

What I hope to use this script for is to have the basic building block for other scripts. First thing I want to do is get a script that lets me ping all the stations and direct the output to a text file. I've also started to try to do this in vbscript in an hta but whilst getting the station nos is easy the ping part isn't looking so good. With vbscript it looks like WMI needs to be used and the results captured and tested for but with dos I'll get the info I want - average min max lost - for less trouble.

DosItHelp - yes it certainly does and its nice and neat compared to my amateur attempt. Thanks again - really appreciated.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#5 Post by avery_larry » 26 Dec 2009 21:02

I'll strip and pad leading zeros much the way Dos has shown (though I've never used the for /l loop method).

On the other hand, because of the annoyance -- whenever I have the ability to control a set I just start with a large number instead of starting with 0 or 1



So instead of:

set01
set02
set03
set04
set05
...
set97
set98
set99

I would start with set101 instead of set01

But that's just me.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

#6 Post by alan_b » 16 Jan 2010 04:27

Another benefit of starting at 1000etc is very simple sorting.

I often create a log file when a batch script runs,
and once I had to sort the lines of text :-
What should have been a sequence from 1 to 12 became
1, 10, 11, 12, 2. 3, 4, 5, 6, 7, 8, 9

Since then I prefer to always start at 1000 just in-case I may later wish to "sort" something.

Alan

Post Reply