How to validate digit number from .txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rajnishjc_27
Posts: 21
Joined: 16 Aug 2019 23:35

How to validate digit number from .txt file

#1 Post by Rajnishjc_27 » 16 Aug 2019 23:41

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,

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to validate digit number from .txt file

#2 Post by penpen » 17 Aug 2019 09:43

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

Rajnishjc_27
Posts: 21
Joined: 16 Aug 2019 23:35

Re: How to validate digit number from .txt file

#3 Post by Rajnishjc_27 » 18 Aug 2019 01:24

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..

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: How to validate digit number from .txt file

#4 Post by SIMMS7400 » 18 Aug 2019 03:43

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
)

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: How to validate digit number from .txt file

#5 Post by siberia-man » 18 Aug 2019 15:36

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
)
Last edited by siberia-man on 19 Aug 2019 00:06, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to validate digit number from .txt file

#6 Post by penpen » 18 Aug 2019 19:21

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

Rajnishjc_27
Posts: 21
Joined: 16 Aug 2019 23:35

Re: How to validate digit number from .txt file

#7 Post by Rajnishjc_27 » 19 Aug 2019 00:33

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.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: How to validate digit number from .txt file

#8 Post by SIMMS7400 » 20 Aug 2019 03:51

How about you try yourself using one of the many examples already provided to you?

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: How to validate digit number from .txt file

#9 Post by siberia-man » 21 Aug 2019 06:48

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

Post Reply