How to validate digit number from .txt file
Moderator: DosItHelp
-
- Posts: 21
- Joined: 16 Aug 2019 23:35
How to validate digit number from .txt file
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,
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
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):
penpen
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
-
- Posts: 21
- Joined: 16 Aug 2019 23:35
Re: How to validate digit number from .txt file
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..
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
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
)
-
- Posts: 208
- Joined: 26 Dec 2013 09:28
- Contact:
Re: How to validate digit number from .txt file
These three are implementations of one way, practically.
1.
2.
3.
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
)
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
)
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.
Re: How to validate digit number from .txt file
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):
penpen
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
-
- Posts: 21
- Joined: 16 Aug 2019 23:35
Re: How to validate digit number from .txt file
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.
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
How about you try yourself using one of the many examples already provided to you?
-
- Posts: 208
- Joined: 26 Dec 2013 09:28
- Contact:
Re: How to validate digit number from .txt file
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