Determine Integer

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Determine Integer

#1 Post by Cleptography » 15 Jun 2011 01:38

How can I search through a text file like this and determine the integers?

Code: Select all

99201 22839 19283
19283 ABSJD 92839
00293 LSKDJ 90LSK

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Determine Integer

#2 Post by nitt » 15 Jun 2011 01:45

Cleptography wrote:How can I search through a text file like this and determine the integers?

Code: Select all

99201 22839 19283
19283 ABSJD 92839
00293 LSKDJ 90LSK


I do not understand what you mean by "determine". Do you want to like run a FOR loop through every character and if one is an integer then return true for that one loop or something? Or do you want to check an entire line, or the entire thing? Or do you want to count how many integers there are?

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Determine Integer

#3 Post by Cleptography » 15 Jun 2011 01:57

Just check each group of chars and determine if it is an integer, and if it is not print something that says ????? was not an integer.
This might make it easier.

Code: Select all

02932
10293
KL039
12OPE
93029


So determine which of the above are integers.
Thanks nitt

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Determine Integer

#4 Post by orange_batch » 15 Jun 2011 03:41

Easy as pie. 8)

Code: Select all

for /f "tokens=1" %%a in (textfile.txt) do set var=&set /a var+=1%%a 2>nul||echo:%%a is not an integer.

We prefix the 1 to prevent DOS from interpreting a number starting with 0 as an octal. It also prevents set /a from interpreting any %%a starting with a letter as a variable.

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Determine Integer

#5 Post by Cleptography » 15 Jun 2011 04:23

Good job orange :mrgreen:

Code: Select all

for /f "Tokens=*" %%a in ('type %1') do (
   if %%a NEQ +%%a echo\Line containing {%%a} in file %1 is NOT an integer.
)

Post Reply