Page 1 of 1 1
Topic Options
#137167 - 2005-04-05 06:43 PM Help: Scan text files for a number combination *solved*
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Okay... I haven't got any ideas how to solve this in a nice manner. I want to scan text files in a directory and find a 7-digit number. This number only appears once in each file, but it could be anywhere in the file, inbetween characters or between spaces. Example: "0283086".

The textfiles are relatively small (often < 10kb). Any ideas


Edited by masken (2005-04-07 10:59 PM)
_________________________
The tart is out there

Top
#137168 - 2005-04-05 06:50 PM Re: Help: Scan text files for a number combination
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Just thinking out loud... here's some pseudo code...

Code:

$number=1234567
open($file)
$=readline()
$found=0
while @error =0 and $found=0
if instr($,$number)
$found=1
endif
$=readline()
loop
close()


Top
#137169 - 2005-04-05 06:56 PM Re: Help: Scan text files for a number combination
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Allen,
You left out the Dir() part.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#137170 - 2005-04-05 06:59 PM Re: Help: Scan text files for a number combination
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
$number = "somevalue"
$dir = enumdir("some dir")
for each $file in $dir
$content = readfile($file)
for each $line in $content
if instr($line,$number)
? $file
endif
next
next


or something like that...
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#137171 - 2005-04-05 07:10 PM Re: Help: Scan text files for a number combination
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
do you know what number you're looking for?

Code:

$theDir = 'c:\'
$txtFile = dir($theDir + '*.txt')
while @error = 0
$ = open(1,$theDir + $txtFile)
$line = readline(1)
While @error = 0
for $j = 1 to len($line)-7
$possibleNumber = substr($line,$j,7)
if $possibleNumber = 0.0 + $possibleNumber
? $possibleNumber
endif
next
$line = readline(1)
loop
$ = close(1)
$txtFile = dir()
loop


Top
#137172 - 2005-04-05 11:18 PM Re: Help: Scan text files for a number combination
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
@maciep, oops, forgot to tell. No, that's the "trick", I don't know the number, that's what I'm looking for in the textfiles. An effective wildcard expression like "#######" - search. RegEx support, anyone?
_________________________
The tart is out there

Top
#137173 - 2005-04-05 11:56 PM Re: Help: Scan text files for a number combination
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
is it ALWAYS a 7-digit number ?

What exactly is the real task at hand for? Knowing what the task really is might make it easier to come up with a good solution.

Top
#137174 - 2005-04-06 12:27 AM Re: Help: Scan text files for a number combination
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
did you try my code?
Top
#137175 - 2005-04-06 01:13 AM Re: Help: Scan text files for a number combination
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Masken, minor update to maciep's code.

It does appear to work even with binary files in the folder.

I added line to show which file contains the entry. But seems to find the length of numbers anywhere in the file as you requested.

Nice job maciep.

Code:
Dim $theDir, $txtFile, $, $line, $j, $possibleNumber
$theDir = 'c:\numtest\'
$txtFile = dir($theDir + '*.*')
while @error = 0
$ = open(1,$theDir + $txtFile)
$line = readline(1)
While @error = 0
for $j = 1 to len($line)-7
$possibleNumber = substr($line,$j,7)
if $possibleNumber = 0.0 + $possibleNumber
? $txtFile + ' ' + $possibleNumber
endif
next
$line = readline(1)
loop
$ = close(1)
$txtFile = dir()
loop


Top
#137176 - 2005-04-06 03:28 AM Re: Help: Scan text files for a number combination
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Use some standard code to read the files line by line and the regex code to check for the 7 digit sequence.

http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB2&Number=76373

RegExpTest("\d{7}", $line, 1)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#137177 - 2005-04-06 06:11 AM Re: Help: Scan text files for a number combination
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
DOH! Thanks Howard. We have so many UDFs that I guess we sometimes forget what they all do.

But Maciep's code does appear to fit the bill as well in my simple tests.

Top
#137178 - 2005-04-06 02:33 PM Re: Help: Scan text files for a number combination
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Actually, my code didn't seem to like numbers that started with '0'. So I made some adjustments

Code:

$theDir = 'c:\'
$txtFile = dir($theDir + '*.txt')
while @error = 0
$ = open(1,$theDir + $txtFile)
$line = readline(1)
While @error = 0
for $j = 1 to len($line)-6
$possibleNumber = substr($line,$j,7)
$asNum = 0.0 + $possibleNumber
if $possibleNumber = padstr($asNum,'0', len($possibleNumber))
? $txtFile + ' ' + $possibleNumber
endif
next
$line = readline(1)
loop
$ = close(1)
$txtFile = dir()
loop

Function PadStr($Input, $Pad, $Length, optional $PadSide)
Dim $i, $x
$PadStr = ""
$Input = "" + $Input
$Pad = "" + $Pad
$Length = 0 + $Length
If $PadSide="" or Len($PadSide)>1 or Instr("LR",$PadSide)= 0
$PadSide = "L"
Endif

$x = Len($Input)

For $i=$x to $Length - 1 Step Len($Pad)
If $PadSide = "L"
$Input = $Pad + $Input
Else
$Input = $Input + $Pad
Endif
Next
If $PadSide = "L"
$Input = Right($Input, $Length)
Else
$Input = Left($Input, $Length)
Endif
$PadStr = $Input
Exit 0
Endfunction


Top
#137179 - 2005-04-07 10:53 PM Re: Help: Scan text files for a number combination
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
Thanks for all the help guys! Howard, I didn't know there was a RegEx function made, or even that it existed in VBS! Thanks alot!

RegEx did the trick in a very slick way @maciep, unfortunately I didn't get your script to work... maybe it's related to the fact that the number could be exactly anywhere in the file?


Edited by masken (2005-04-07 10:54 PM)
_________________________
The tart is out there

Top
#137180 - 2005-04-08 12:08 AM Re: Help: Scan text files for a number combination
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Well not KiX friendly but cool program for non Unix types.

RegexBuddy

Using the latest version of the code posted by Maciep also grabs the number and says which file. But if Howard's works better for you that's cool too. Getting the task done is what counts.

Top
#137181 - 2005-04-08 01:09 AM Re: Help: Scan text files for a number combination
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Masken, are you saying the number could be split across lines? I would like to determine why the code I pointed you to didn't work.

Could you post your regex solution. I think that it would be useful for others that may have similar problems.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#137182 - 2005-04-08 02:01 PM Re: Help: Scan text files for a number combination
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
If the number could be split between two different lines, then my script definitely wouldn't work. In any event, good to see you found a solution
Top
#137183 - 2005-04-09 12:09 AM Re: Help: Scan text files for a number combination
masken Offline
MM club member
*****

Registered: 2000-11-27
Posts: 1222
Loc: Gothenburg, Sweden
@Howard, nono, not split across lines, but anywhere horisontal or vertical within the file, always non-splitted though

I just used the RegEx function you had

Code:

CALL "@SCRIPTDIR\RegEx.udf"

$SourceFile = "E:\MyTextFile.txt"
$ArrSourceFile = ReadFile($SourceFile)

FOR EACH $TextLine IN $ArrSourceFile
$ArrMatches = RegExpFind("\d{7}", $TextLine, $1, 1)
FOR EACH $Match IN $ArrMatches
? "Match = " + CHR(34) + $Match[0] + CHR(34)
NEXT
NEXT

FUNCTION ReadFile($File)
DIM $Handle, $null, $Text
$Handle = FREEFILEHANDLE()
$null = OPEN($Handle, $File)
IF @ERROR EXIT @ERROR ENDIF
DO $Text = $Text + @CRLF + READLINE($Handle) UNTIL @ERROR
$null = CLOSE($Handle)
$ReadFile = SPLIT(SUBSTR($Text, 1), @CRLF)
ENDFUNCTION




Edited by masken (2005-04-09 12:17 AM)
_________________________
The tart is out there

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.071 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org