Page 1 of 1

How to validate digit number from .txt file

Posted: 16 Aug 2019 23:41
by Rajnishjc_27
Hi Friends,

I have .TXT file and data as below
ID , Name, Address
001 , ABC , USA
002 ,XYZ , SNG
A003,PQR,UK

Now i have read each line from .txt file and
put validation on ID field as it should be digit.
Example : if id<>numeric then throw message like echo 'field is not numeric'

please help to write this logic in Batch Script.

Thank you,

Re: How to validate digit number from .txt file

Posted: 17 Aug 2019 09:43
by penpen
Assumed the above text is the content of "test.txt" and you also want to ignore space characters, then the following might help you (untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /f "skip=1 tokens=1 delims=, " %%a in ('findstr /v /r /c:"^[0-9, ]*," "test.txt"') do (
	echo(field "%%~a" is not numeric.
)
goto :eof
penpen

Re: How to validate digit number from .txt file

Posted: 18 Aug 2019 01:24
by Rajnishjc_27
Thanks PenPen for the instant reply.

one more logic needed on the same,
if i want check size of the fields value with 'IF' condition which syntax need to be use.

example.
Name
Rajnish
Alpesh
ABCDEFGH

if length(Name) > 6 then echo 'not more then 6 digit'

Kindly help..

Re: How to validate digit number from .txt file

Posted: 18 Aug 2019 03:43
by SIMMS7400
This works, let us know if this meets your need.

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /f "skip=1 tokens=1 delims=, " %%a in ('findstr /v /r /c:"^[0-9, ]*," "test.csv"') do (
    setlocal enabledelayedexpansion
    SET "string=%%~a" & CALL :STRLEN result string
    if "!result!" GTR "6" (
        echo(field "%%~a" is not numeric and greater than 6 digits
    ) else (
        echo(field "%%~a" is not numeric and is 6 characters or less
    )
)
pause
goto :eof



:STRLEN <resultVar> <stringVar>
(   
    SET "S=!%~2!#"
    SET "LEN=0"
    FOR %%P IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (
        IF "!S:~%%P,1!" NEQ "" ( 
            SET /a "LEN+=%%P"
            SET "S=!S:~%%P!"
        )
    )
)
( 
    ENDLOCAL
    SET "%~1=%LEN%"
    EXIT /B
)

Re: How to validate digit number from .txt file

Posted: 18 Aug 2019 15:36
by siberia-man
These three are implementations of one way, practically.

1.

Code: Select all

@echo off

for /f "usebackq skip=1 tokens=1,2,3 delims=," %%a in ( "test.csv" ) do (
	setlocal
	set /a "%%~a=%%~a" 2>nul && echo:%%~a - Not a number
	endlocal
)
2.

Code: Select all

@echo off

for /f "usebackq skip=1 tokens=1,2,3 delims=," %%a in ( "test.csv" ) do (
	setlocal enabledelayedexpansion
	set /a "%%~a=%%~a" 2>nul
	if !errorlevel! equ 0 echo:%%~a - Not a number
	endlocal
)
3.

Code: Select all

@echo off

for /f "usebackq skip=1 tokens=1,2,3 delims=," %%a in ( "test.csv" ) do for /f "tokens=1*" %%k in ( 'set /a "%%~a=%%~a" 2^>nul' ) do (
	echo:%%~a - Not a number
)

Re: How to validate digit number from .txt file

Posted: 18 Aug 2019 19:21
by penpen
You also could also use a filter to find that information, but you typically can't mix those solution, so you would have to call findstr multiple times on such case (depends on the case if that is slower or faster; untested):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /f "skip=1 tokens=1 delims=, " %%a in ('findstr /v /r /c:"^[0-9, ]*," "test.txt"') do (
	echo(field "%%~a" is not numeric.
)

:: note you have to be carefull if you have to skip the header line or not:
for /f "tokens=2 delims=, " %%b in ('findstr /r /c:"^[^\,]*,[ ]*[^\,, ][^\,, ][^\,, ][^\,, ][^\,, ][^\,, ][^\,, ][^\,, ]*[ ]*," "test.txt"') do (
	echo(field "%%~b": not more then 6 digit'
)
goto :eof
penpen

Re: How to validate digit number from .txt file

Posted: 19 Aug 2019 00:33
by Rajnishjc_27
Hi PenPen/All

i need multiple validation first column example like below.

Id
A002
003
B0000006
10000002

First Numeric validation is working, also but need to check other validation same fields 'ID'
i need Both validation in One loop as below

like Field --- 'A002' ' --> Not Nnumeric.'
Field --- '10000002' --- > should not be more then 6 digit.'

means multiple validation on same field.

kindly help.

Re: How to validate digit number from .txt file

Posted: 20 Aug 2019 03:51
by SIMMS7400
How about you try yourself using one of the many examples already provided to you?

Re: How to validate digit number from .txt file

Posted: 21 Aug 2019 06:48
by siberia-man
If you need to validate some field is numeric value and it is not more than 6 digits, you have to check that the field is number and is less 1000000. Do something like this:

Code: Select all

for /f ... %%a in ... do (
	call :check_num %%~a && if %%~a lss 1000000 (
		do something cool when number is < 1000000 (no more 6 digits)
	)
)

something useful

goto :EOF

:check_num
one of suggested implementations from this thread
this routine is supposed to return the exit code equal one of:
0 - if is number
1 - if is not number