• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Search for string in .cls file and then replace another string in that file

I want to search all .cls files in a directory and look for a string 'setcomplete' in each file. I then need to change another string in the file to a certain value depending on whether the 'setcomplete' is present or not.

What would be the best way to do this?

Thanks

fevster
0
fevster
Asked:
fevster
  • 3
  • 3
1 Solution
 
asafmmCommented:
use InterDev Search dialog to search in your folder for your string - then doubleclick each file found and edit this file.
(U can use 'replace' too)
0
 
fevsterAuthor Commented:
I want to try and automate this using my own VB program. So any code to help me do this would be great.

Thanks
0
 
drydenhoggCommented:
Id kinda of agree that id use the VB IDE to do this for me, however, from a coding perspective.

Dim fso As FileSystemObject
Dim objFolder As Folder
Dim objFile As File
Dim strFileTextStream As TextStream
Dim strReadLine As String

Set fso = New FileSystemObject
Set objFolder = fso.GetFolder("c:\projects\myproject\")

For Each objFile In objFolder.Files
  ' check if the file is a cls
  If InStr(UCase$(objFile.Name), ".CLS") Then
    Set strFileTextStream = objFile.OpenAsTextStream
    ' read the lines in turn etc
    Do While Not strFileTextStream.AtEndOfStream
      strReadLine = strFileTextStream.ReadLine

' Process the strReadLine and insert your logic stuff here

    Loop
    strFileTextStream.Close
  End If
Next

Its really very brute force, no elegance here im afraid. I wouldnt use the old VB file handling functions, the FSO is orders of magnitude faster, so whilst its brute force, it will run very fast.

One disadvantage of the text stream is its forward only, so to get back to start, reopen the file.

ADH.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
drydenhoggCommented:
Addendum:
I really should of used a proper comparison on the instring command
i.e. InStr(UCase$(objFile.Name), ".CLS") > 0
It works as is, that was rather lazy. :)
0
 
fevsterAuthor Commented:
thats great thanks. Bit of a VB junior you see. Now that I can find my string in the file. I need to re-open the file to start reading again for the string to change. right??

How would I then change that string. Use replace or would I have to write the whole file out line by line??

Cheers
0
 
drydenhoggCommented:
Brute force again would be to just read each line in turn and write out each line until you find the line to adjust. In that instance you write out your adjusted line.

Write it to a diff file, a.) it makes your life easier b.) you probs want to keep original files for the 'just-in-case' scenarios

Equally, could read the entire file into a string using the strFileTextStream.ReadAll and use the VB6 replace command.
Then write it to a file.

All depends on the nature of the string being replaced, as to whether you can use the replace command with confidence that it wont replace things you didnt plan.

ADH.
0
 
fevsterAuthor Commented:
thanks for the help... it'll be a great help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now