Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: q0ok en 29 Mayo 2011, 02:37 am



Título: Filtro de variable numérica
Publicado por: q0ok en 29 Mayo 2011, 02:37 am
Mi objetivo es comprobar que la variable 'variable' sólo contenga números.

Código:
::Filtro de variable numérica by q0ok
setlocal enabledelayedexpansion
:inicio
set /p "variable=Introduzca su variable: "
if not defined variable (goto:inicio)
::Hallamos la longitud de la cadena de carácteres.
set /a cont=0 & set /a sum=0
call :longitud
echo %sum%
::Comprobamos los carácteres de la variable.
:verify
for %%_ in (0 1 2 3 4 5 6 7 8 9) do (
if "!variable:~%cont%,1!" NEQ %%_ (echo WRONG) else (echo RIGHT)
)
pause>nul
set /a cont+=1
if %cont%==%sum% (
echo fin
pause>nul
exit
) else (
goto:verify
)
:longitud
set "fun=!variable:~%sum%,1!"
if not defined fun (goto:eof)
set /a sum+=1
goto longitud

El problema llega dentro del for en "!variable:~%cont%,1!"... No me aparece el carácter como debería aparecer.

¿Alguna ayudita?

¡Gracias!


Título: Re: Filtro de variable numérica
Publicado por: SuperDraco en 29 Mayo 2011, 04:07 am
No encuentro el fallo en tu código, pero te dejo otra manera de hacer lo mismo, por si te sirve:


Código
  1. @echo off
  2.  
  3. set /p "variable=Introduzca su variable: "
  4. if not defined variable (goto:inicio)
  5.  
  6. call :getLength %variable%
  7. echo %length%
  8. pause>nul
  9.  
  10.  
  11.  
  12. echo %variable% |findstr "a b c d e f g h i j k l m n ¤ o p q r s t u v w x y z , + - _ ¬ ' = ? ¿ ( ) $ # @ º ª" >nul
  13. If %ERRORLEVEL% EQU 0 (echo WRONG) ELSE (echo RIGHT)
  14.  
  15.  
  16. pause>nul
  17. echo fin
  18. pause>nul
  19.  
  20.  
  21.  
  22. :Longitud
  23. :getLength
  24. set /a length+=0
  25. set str=%*
  26. :getLength_2
  27. set "str=%str:~1%" && set /a length+=1
  28. if defined str (goto :getLength_2) || (goto :eof)
  29.  


Título: Re: Filtro de variable numérica
Publicado por: q0ok en 29 Mayo 2011, 04:17 am
Gracias pitoloko por tu respuesta. Sí funciona pero me gustaría que si alguien lograse encontrar el error por el que no funciona el código que postee.