Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to locate a line in a file

Status
Not open for further replies.

JamesBBB

Technical User
Nov 2, 2005
74
GB
Hi All,

I have a number of text files which are the results of a dir of certain hard drives in the system.

What I would like to do is strip off the amount of bytes used and the amount of bytes free for each drive.

Basically what I need to be able to do is open each of these files and navigate down to the lines that say "bytes" and bytes free.

Examples of the end of each file is as follows:-

Total Files Listed:
85 File(s) 248,582,163,171 bytes
3 Dir(s) 1,399,914,496 bytes free
---
771 File(s) 249,402,397,641 bytes
2 Dir(s) 576,229,376 bytes free
---
Total Files Listed:
44039 File(s) 188,416,106,965 bytes
1149 Dir(s) 61,402,812,416 bytes free

etc etc etc

Once I have got these lines I would then want to strip out the values:-

248,582,163,171
1,399,914,496

249,402,397,641
576,229,376
etc etc


I have tried but I do not know how to do this.

Here is a snippet of code I am using to read each file and then manipulating each string and writing it back to another file, however I have no idea how to read a file and search for a word and then use that record.

aax = rst![Drive Name] ' Get record from table
dstr = con1 & "\" & aax & ".txt" ' Set locatiion string for text file
Set f = fs1.OpenTextFile(dstr, 1) ' open input file
Do While Not f.atendofstream
' read the input text file
ab = f.ReadLine
a.Writeline (ab)
End If
Loop


Any help or pointers would be very gratefully received

many thanks

James


 
Assuming that "ab" is a string, you could try something like:
Code:
dim a as integer, b as integer, txtNum as string
  ab = f.ReadLine
  if instr(1,ab,"(s)") and instr(1,ab,"bytes") then
    a=instr(1,ab,")")
    b=instr(1,ab,"bytes")
    txtNum=trim(mid(ab,a,b-a))
  endif
 
Hi GhostWolf,

Many thanks for the response, it works perfectly as I expected. However there is a little problem, which is probably because I did not explain clearly enough.

When the amount of space used is greater that the free space on the drive the strings populate perfectly, however is there is more space free than used I get nothing is the txtnum string.

Ive worked it out to the line
txtNum=trim(mid(ab,a,b-a))

Example

36 File(s) 238,093,039,616 bytes
0 Dir(s) 11,888,246,784 bytes free works

11 File(s) 69,514,088,448 bytes
0 Dir(s) 180,467,359,744 bytes free Does Not work

But Ive no idea how to change it to take account of the above situation, is this something that could be done easily, or not.

many thanks

James

 
One change I might suggest:
a=instr(1,ab,")")[red]+1[/red]
to position to the blank space after the ")".

I'd have to step through the code in debug to see what's causing that, if the inclusion of the "+1" doesn't solve it.

If that code snippet works on one line, it should work on the other - since the only thing it considers is the absolute location of two constants.

Please, let me know what you find, (even if it's just my silly mistake of the missing "+1"!).
 
It would probably be more efficient to use RegEx

Code:
Dim re As RegExp 
Set re = New RegExp 
Dim s As String 
Dim i as Integer
Dim matches As MatchCollection 
Dim mcMatch As Match 

[COLOR=green]' I hope this pattern works for you[/color]
re.Pattern = "^\s+\d+\s[Files(s)|Dir(s)]\s+(.*)\s[bytes|bytes free]&"
re.Global = False 
re.IgnoreCase = True 
re.MultiLine = True 

[COLOR=green]' do the normal read from file here to get a string to match
' then loop through the text file
' for testing purposes:-[/color]
s = "            1149 Dir(s)  61,402,812,416 bytes free" 

Set matches = re.Execute(s) 

For Each Match In Matches
   Debug.Print "source > >", Match.Value
   For i = 0 To Match.SubMatches.Count - 1
      [COLOR=green]' you are looking for $1 here[/color]
      Debug.Print "[ $" & i + 1 & "]", Match.SubMatches(i)
   Next i
Next Match

J.
 
In your case the lines:-

Code:
re.IgnoreCase = True 
re.MultiLine = True

Should be:-

Code:
re.IgnoreCase = False 
re.MultiLine = False

Sorry,

J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top