It is string consists of digits?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

It is string consists of digits?

#1 Post by alexandredneto » 25 Jun 2015 12:08

Hello.
My script is not working.
I want to check if you have any other than I type.

Code below:

Code: Select all

SET STRCPF=%1

echo.%STRCPF%>len
for %%a in (len) do set /a len=%%~za -2

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %desloca% in (1,1%len%) DO (
   echo desloca=%desloca%
   set "X=!STRCPF:~%desloca%,1!"
   IF 1 GEQ %X% (
      IF 10 LEQ %X% (
        ECHO "Digit"
       )
      ) ELSE (
        ECHO "No digit"
     )
   )

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: It is string consists of digits?

#2 Post by aGerman » 25 Jun 2015 12:43

You may use a simple FOR /F loop.

Code: Select all

@echo off
for /f "delims=1234567890" %%i in ("%~1") do (echo other characters found&exit /b 1)
echo all digits&exit /b 0


Regards
aGerman

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: It is string consists of digits?

#3 Post by alexandredneto » 25 Jun 2015 13:34

I did not like merge your code in my above.

I want a code that do:
It is assigned a value in the string by argument, and then do the validation if they are only digits.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: It is string consists of digits?

#4 Post by aGerman » 25 Jun 2015 13:39

But that's what it already does :?
If you want you can assign a variable first ...

Code: Select all

set "var=%~1"
for /f "delims=1234567890" %%i in ("%var%") do (echo other characters found&exit /b 1)
echo all digits&exit /b 0

... but that's meaningless.

Regards
aGerman

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: It is string consists of digits?

#5 Post by alexandredneto » 25 Jun 2015 13:49

I expressed myself badly. Excuse me.

Yes the code is well done.
But needs some adjustments. Therefore, it is returning as digits: space, empty, comma, for example:
D:\>test a
other characters found

D:\>test 9
all digits

D:\>test
all digits

D:\Script\Cria_atalho_vbs\teste>test ,
all digits

D:\Script\Cria_atalho_vbs\teste>tt
all digits

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: It is string consists of digits?

#6 Post by aGerman » 25 Jun 2015 14:12

The comma belongs to the default argument delimiters. Thus, a single comma is always interpreted as no argument at all. If you enclose the argument into quotation marks then a comma would belong to the argument as well as spaces, semicolons, etc.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
set "var=%~1"
if not defined var (echo no argument&exit /b 1)
setlocal EnableDelayedExpansion
for /f "delims=1234567890 eol=" %%i in ("!var!") do (echo other characters found&exit /b 1)
echo all digits&exit /b 0

Code: Select all

C:\>test.bat ","
other characters found

C:\>

Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: It is string consists of digits?

#7 Post by foxidrive » 25 Jun 2015 15:39

alexandredneto, test this with your space, comma, semicolon, and digits and you will see what happens:

Code: Select all

@echo off
SET "STRCPF=%1"
echo."%STRCPF%"
pause

Post Reply