Page 1 of 1

It is string consists of digits?

Posted: 25 Jun 2015 12:08
by alexandredneto
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"
     )
   )

Re: It is string consists of digits?

Posted: 25 Jun 2015 12:43
by aGerman
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

Re: It is string consists of digits?

Posted: 25 Jun 2015 13:34
by alexandredneto
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.

Re: It is string consists of digits?

Posted: 25 Jun 2015 13:39
by aGerman
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

Re: It is string consists of digits?

Posted: 25 Jun 2015 13:49
by alexandredneto
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

Re: It is string consists of digits?

Posted: 25 Jun 2015 14:12
by aGerman
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

Re: It is string consists of digits?

Posted: 25 Jun 2015 15:39
by foxidrive
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