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

Posted on 2003-02-25
Medium Priority
Last Modified: 2010-04-07
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?


Question by:fevster
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Expert Comment

ID: 8015682
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)

Author Comment

ID: 8015740
I want to try and automate this using my own VB program. So any code to help me do this would be great.


Accepted Solution

drydenhogg earned 300 total points
ID: 8015772
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

  End If

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.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8015800
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. :)

Author Comment

ID: 8015825
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??


Expert Comment

ID: 8015931
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.


Author Comment

ID: 8016029
thanks for the help... it'll be a great help

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question