By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

read a text file and take a selection to rename another file...

P: 2
Sounds a little complicated...but this is what I need to do. First accept entry from two textboxes, join them together, then open a file and search that file for all occurances of the string, After locating the string, I need to rename a file that matches another section of that string and store it for later use.
Thanks for all the assistance you could give.
May 9 '07 #1
Share this Question
Share on Google+
4 Replies


SammyB
Expert 100+
P: 807
Sounds a little complicated...
Sounds easy...
What do you need help with?
What VB are you using? VBA, VB6, or VB.Net?
May 9 '07 #2

Expert 5K+
P: 8,434
Sounds a little complicated...but this is what I need to do. First accept entry from two textboxes, join them together, then open a file and search that file for all occurances of the string, After locating the string, I need to rename a file that matches another section of that string and store it for later use.
Thanks for all the assistance you could give.
I think you'll find that most programming tasks seem complicated at first. Once you break it down to the individual steps involved, you'll likely find the steps are relatively simple.

I'm sure you can already join together the two text boxes, for instance. Opening a file is simple enough. And so on...

The specifics of how you do this stuff will depend on the version of VB you're using, but I use VB6 so I'll talk about that for now.

To search a file is much simpler than you might think. For example, this little piece of code will read an entire file into memory (assuming the file isn't too large) and print to the immediate window a list of the positions where a given string occurs.
Expand|Select|Wrap|Line Numbers
  1. Public Sub FindStringInFile(ByVal FileName As String, ByVal TheString As String)
  2.  
  3.   Dim Fnum As Long, Buffer As String, StrPos As Long
  4.  
  5.   ' Retrieve the entire file into a string.
  6.   Fnum = FreeFile
  7.   Open FileName For Binary Access Read Shared As #Fnum
  8.   Buffer = Space$(LOF(Fnum))
  9.   Get #Fnum, , Buffer
  10.   Close Fnum
  11.  
  12.   ' Scan the string for our search string, report all occurrences.
  13.   StrPos = InStr(Buffer, TheString)
  14.   Do While StrPos
  15.     Debug.Print "Found string at position: "; Format(StrPos)
  16.     StrPos = InStr(StrPos + 1, Buffer, TheString)
  17.   Loop
  18. End Sub
May 9 '07 #3

P: 2
Thanks for the pointers. I am trying to this in VB6, but i am still in the process of learning VB as we go. What I'm trying to create is an opening for Instalshield to read a customer number and zip code and then find that in a flat file which should let me know what applications the customer can install and which specials they should receive. Thanks for the help.
May 15 '07 #4

Expert 5K+
P: 8,434
Thanks for the pointers. I am trying to this in VB6, but i am still in the process of learning VB as we go. What I'm trying to create is an opening for Instalshield to read a customer number and zip code and then find that in a flat file which should let me know what applications the customer can install and which specials they should receive. Thanks for the help.
Good luck. This should give you enough to get started, I hope.

Oh, here's a slightly different version to read a text file, line by line, and check whether each line matches a requested value. It illustrates a different file access method.

Expand|Select|Wrap|Line Numbers
  1. Public Sub FindLineInFile(ByVal FileName As String, ByVal TheString As String)
  2.   Dim Fnum As Long, Buffer As String, LineNo As Long
  3.   Fnum = FreeFile ' Get next unused file number.
  4.   Open FileName For Input Access Read Shared As #Fnum
  5.   Do Until Eof(Fnum)
  6.     ' Retrieve one line of text from the file a string.
  7.     Line Input #Fnum, Buffer
  8.     LineNo = LineNo + 1
  9.     if Buffer = TheString Then
  10.       Debug.Print "Found string at line: "; Format(LineNo)
  11.     End If
  12.   Loop
  13.   Close Fnum
  14. End Sub
May 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.