Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 2nd, 2003, 04:39 PM
peter berten peter berten is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 9 peter berten User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
FILE I/O help

I dont know where to even start, i need to open a file search for a string, and then read a few characters after the string into a variable if the string is found true and also take the file that turned up true and copy it to a diffrent directory, any help is appreciated thanks
, Peter

Reply With Quote
  #2  
Old August 3rd, 2003, 10:34 AM
K-TC K-TC is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: England
Posts: 12 K-TC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to K-TC Send a message via AIM to K-TC Send a message via Yahoo to K-TC
You could try this:

(explaination below)

Code:
Private Sub Form_Load()
    MsgBox Search_File("C:\YourFileToSearch.txt", "SearchText")
End Sub

Function Search_File(strFile As String, strSearch As String) As String
    Dim strData As String
    
    Open strFile For Input As #1
    
    Do Until EOF(1)
        Line Input #1, strData
        
        If InStr(strData, strSearch) Then
            Search_File = Right(strData, Len(strData) - InStr(strData, strSearch) - Len(strSearch) + 1)
            Exit Function
        End If
        
        DoEvents
    Loop
    
    Search_File = "Not Found"
    
    Close #1
End Function


What this does is it makes a function, which is probably best for this kind of thing. It uses strFile to hold the path to the file, strSearch for the string to search for, strData holds the current line gathered from the file.

So when the function is called it opens the file path held in strFile, and makes it so that it will input (other settings are output and append) as #1, that is just a tag so you can refer to it.

It then sets a loop until EOF (end of file), using 1 (the file we have opened). It then inputs a single line from the file #1 and puts that into strData for later processing.

It then uses the instr function to check if the search string is in the inputted line. The instr function works as both a boolean (to detect if a string is inside another) and an integer (to give you the point at which the search string is in the input string provided).

Then the Right function is used to grab all the characters from the end search string onwards. Unfortunately, I always have great difficulty trying to explain the Right function, but here goes.

The Right function works by you supplying it a string to work with, and a position from which it will take. Take this for example:

We search for "hello" in a text file that contains "testahello=test". The purpose of the right function is to get the right hand side of that, so we want the "=test". We have to get the length of the entire thing, which is 15. We then work out the position of "hello" in the string, which is 6 (instr finds the position of the first character). Then it collects the length of the search string, takes that from the current position and plus one to that position.

That now returns everything after the "hello".

I hope this is of help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > FILE I/O help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway