ZTree.com  | ZEN  | About...  

 Index   Back

'isAnInteger' batch function   [Q]

By: Nick Williams     West Sussex, England (forum time +5 hrs)  
Date: Aug 27,2017 at 22:12
In Response to: [Q] how do i test if a string is a number? (Ryan)

> >You are very welcome to have this -- are you interested ?
> yes, if numbers? am all ears. and i get to know more about batches.

Bear with me, I haven't done this before -- I'm having trouble with the embedded HTML tags that my editor uses ...

OK, try it out, if you're generally happy with it, I'll show you how to turn the "isAnInteger" function into a library function that you can CALL from anywhere on your system.

If you have any questions or amendments -- I'll try to tackle these in a day or so.

Today, (Monday 28th Aug) is the "End-Of-August Bank Holiday" (a public holiday) in the UK, so I'm just going to chill out for a while.


::[ ISINTEGR.CMD, v1.0, 28-Aug-2017, freeware ]=====[ Author: Nick Williams ]
@ECHO OFF
ECHO:>NUL & GOTO:BEGIN & REM Is the command separator (&) supported?
GOTO:END & REM ==============================================================


:isAnInteger ::{fold} ----------------------------------------------------[1]
::
:: Func: Determines whether the %1 argument given, is a string representation
:: of a valid positive or negative integer
::
:: Args: %1 var to be tested (by val)
:: %2 var to receive the test result (See 1st note, below) (by ref)
:: %3 var to receive the possibly re-formatted integer string (by ref)
::
:: Note: To determine the outcome, the CALLer can use either of two choices:-
::
:: (1) Use the validation result ('isValidNumb') whose value is passed
:: back through the %2 argument (where 1=success; the %1 argument
:: does represent an integer, or 0=failure; not a valid integer)
::
:: -or-
::
:: (2) Check the ERRORLEVEL returned by the function -- which will give
:: the inverse of that from choice (1) (i.e. 0=success, 1=failure)
::
:: Note: The %1 argument to be tested is copied to a variable ('numberToChk')
:: which then undergoes (potentially) two stages of re-formatting.
::
:: Regardless of success or failure, this re-formatted copy of the %1
:: argument is returned via the %3 argument, but is only really useful
:: if the function determines that the %1 argument is a valid integer
::
::{fold-section} ------------------------------------------------------------
(SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION)
:: =======
SET "isValidNumb=0"
SET "numberToChk=%~1"
FOR /F "TOKENS=*" %%I IN ("%numberToChk%") DO (SET "numberToChk=%%~I")
IF NOT DEFINED numberToChk (GOTO :isAnIntegerEND)
::
IF "%numberToChk:~0,1%"=="+" (SET "tmpWorkCopy=%numberToChk:~1%") ELSE (
IF "%numberToChk:~0,1%"=="-" (SET "tmpWorkCopy=%numberToChk:~1%") ELSE (
SET "tmpWorkCopy=%numberToChk%"))
::
FOR /F "TOKENS=*" %%I IN ("%tmpWorkCopy%") DO (SET "tmpWorkCopy=%%~I")
IF NOT DEFINED tmpWorkCopy (GOTO :isAnIntegerEND)
::
SET "isValidNumb=1"
FOR /F "EOL= TOKENS=1 DELIMS=0123456789" %%I IN ("%tmpWorkCopy%") DO (
SET "isValidNumb=0"
)
::
IF %isValidNumb%==1 (
(SET /A "tmpWorkCopy=%numberToChk%" 2>NUL) || (SET "isValidNumb=0")
)
IF %isValidNumb%==1 (SET "numberToChk=%tmpWorkCopy%")
::
:isAnIntegerEND
::
SET /A "errorRtnVal=(!%isValidNumb%)"
::
::{fold-section} ------------------------------------------------------------
ENDLOCAL & (SET "%2=%isValidNumb%")^
& (SET "%3=%numberToChk%") & (EXIT /B %errorRtnVal%)
::{/fold} -------------------------------------------------------------------


:helperFnctn ::{fold} ----------------------------------------------------[0]
::
:: Func: Simple helper function works on behalf of the main batch script. It
:: calls the 'isAnInteger' function to test the %2 argument provided.
::
:: It then produces a one-line report of the outcome, as follows:-
:: [Test No.][ErrorLevel][Result][Original string][Re-formatted string]
::
:: Args: %1 the test number (by val)
:: %2 var to be tested (by val)
::
::{fold-section} ------------------------------------------------------------
(SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION)
::
SET /P "=Test number %~1: " 0 ::
CALL :isAnInteger "%~2" isNumbValid isNumbValue
SET "errLevelRtn=%ErrorLevel%"
::
IF %isNumbValid%==1 (SET "isValidFlag=Y") ELSE (SET "isValidFlag= ")
::
ECHO:[%errLevelRtn%][%isValidFlag%] "%~2" interpreted as: "%isNumbValue%"
::
::{fold-section} ------------------------------------------------------------
ENDLOCAL & (EXIT /B 0)
::{/fold} -------------------------------------------------------------------


:BEGIN ::{fold} ===================================================[ Header ]
::
:: Func: Test suite for the 'isAnInteger' function. A helper function is used
:: to execute each test -- to reduce cluttered and repetitive coding
::
:: Args: None
::
::{fold-section} ------------------------------------------------------------
(COLOR 00)& (SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION) || (GOTO:END)
(PUSHD "%~dp0" 2>NUL) || (GOTO:END)
::{fold-section} ==================================================[ main() ]
::
(CLS) & (ECHO:)
::
CALL :helperFnctn 01
CALL :helperFnctn 02 ""
CALL :helperFnctn 03 " "
CALL :helperFnctn 04 " abc "
CALL :helperFnctn 05 " abc def "
CALL :helperFnctn 06 abc
CALL :helperFnctn 07 abc6
CALL :helperFnctn 08 6abc
CALL :helperFnctn 09 -1
CALL :helperFnctn 10 -0
CALL :helperFnctn 11 0
CALL :helperFnctn 12 +0
CALL :helperFnctn 13 00
CALL :helperFnctn 14 007
CALL :helperFnctn 15 009
CALL :helperFnctn 16 +1
CALL :helperFnctn 17 6
CALL :helperFnctn 18 " 6 "
CALL :helperFnctn 19 0x20
CALL :helperFnctn 20 12345
CALL :helperFnctn 21 3.142
CALL :helperFnctn 22 123*4
CALL :helperFnctn 23 123/4
CALL :helperFnctn 24 123+4
CALL :helperFnctn 25 123-4
::
(ECHO:) & (ECHO:Press any key to CONTINUE...) & (PAUSE 1>NUL)
(CLS) & (ECHO:)
::
CALL :helperFnctn 26 !1
CALL :helperFnctn 27 "12&34"
CALL :helperFnctn 28 "12|34"
CALL :helperFnctn 29 "12<34"
CALL :helperFnctn 30 "12>34"
CALL :helperFnctn 31 "12,34"
CALL :helperFnctn 32 "12^3"
CALL :helperFnctn 33 "12=34"
CALL :helperFnctn 34 "12;34"
CALL :helperFnctn 35 "12%34"
CALL :helperFnctn 36 "12(3)"
CALL :helperFnctn 37 +
CALL :helperFnctn 38 -
CALL :helperFnctn 39 " + "
CALL :helperFnctn 40 " - "
CALL :helperFnctn 41 " -+8 "
CALL :helperFnctn 42 " +-8 "
CALL :helperFnctn 43 " - + "
CALL :helperFnctn 44 " + - "
CALL :helperFnctn 45 " 4 5 "
CALL :helperFnctn 46 " 8+ "
CALL :helperFnctn 47 " -8+ "
CALL :helperFnctn 48 " - 8 "
CALL :helperFnctn 49 " + 56"
CALL :helperFnctn 50 " - 00"
::
(ECHO:) & (ECHO:Press any key to FINISH...) & (PAUSE 1>NUL)
::
::{fold-section} ==================================================[ Footer ]
COLOR & POPD & ENDLOCAL & (EXIT /B 0)
:END
::{/fold} ============================================================[ EOF ]


Kind regards
Nick

279 views      
Thread locked
 

Messages in this Thread

 
94,476 Postings in 11,922 Threads, 348 registered users, 4 users online (1 registered, 3 guests)
Index | Admin contact |   Forum Time: Oct 17, 2019 - 5:29 pm EDT  |  Hits:28,769,132  (3,561 Today )
RSS Feed