Solved

Find and replace a text in a.TXT file

Posted on 2008-10-26
4
446 Views
Last Modified: 2009-01-03
What i am trying to do here is to CANCEL a subscription of a CLIENT by searching for his email address in a .TXT file and if that email address FOUND THEN start READING the previous line so we can find and REPLACE the text "Subscription: Active" to "Subscription: Cancelled".

I have made a simple example of my .txt file.

My .txt contains the following:

Purch in 11/27/2008 serial: AsDFGH2123456 Subscription: Cancelled Deleted: No Charged: 1
admin1@xxx.com
Purch in 11/28/2008 serial: ns7YBSUjkskj Subscription: Active Deleted: No Charged: 1
admin2@xxx.com
Purch in 11/28/2008 serial: SNus8USHJ Subscription: Active Deleted: No Charged: 1
admin3@xxx.com

How can i cancel the subscription of the admin2@xxx.com ?

I am using vb 6.0!

Thanks
Purch in 11/27/2008 serial: AsDFGH2123456 Subscription: Cancelled Deleted: No Charged: 1
admin1@xxx.com
Purch in 11/28/2008 serial: ns7YBSUjkskj Subscription: Active Deleted: No Charged: 1
admin2@xxx.com
Purch in 11/28/2008 serial: SNus8USHJ Subscription: Active Deleted: No Charged: 1
admin3@xxx.com

Open in new window

0
Comment
Question by:KingSencat
  • 2
4 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 22807169
1. Does this need to be a .txt file?  It seems more suitable as a database application.

2. How big is this file? (lines)

3a. If the file is relatively small, then you can read the entire contents of the file into a string variable, use InStr() to find the email addres and then InStrRev() to find the prior "Subscription:"

3b. Convert this text file into a database table or a fixed/delimited text file.  Use SQL Update statement to change the Subscription field Where the email field is admin2@xxx.com.  Note, the email address will appear on the same line as the "Subscription:" data.

3c. For larger files, use pairs of Line Input statements in a loop, placing the results in two string variables.  If the email string variable equals the email address you seek, then change the subscription data.
Whether you change a string variable or not, you output both string variables to a new text file.  Upon completion, replace the original text file with the text file you just created.

3d. If you have a fixed length record format, you can open the file as Random and update the 'records' in place.  You will need a TYPE structure with fixed length string components.
0
 

Author Comment

by:KingSencat
ID: 22820374
1) I need to do it with a .txt file
2) It is not big, under 5KB
The file its in fixed length format, i just need to replace the subscription to cancelled, any example please?
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 22821663
@KingSencat

1. What I meant was could this txt file be a different format?  If we restructured your txt file, it might serve its current purpose and be accessed with a database driver or VB Open Random statement.  Is the format of the text file used by other programs, not just the originator of the data?

2. it is NOT in fixed length format.  If you open the code snippet window in your original question comment, you will see that each of the three 'first row' records are a different length.  see snippet below for example of what I meant.

3.
Fixed length data format example:
 
Purch in 11/27/2008 AsDFGH2123456 Cancelled  No 1   admin1@xxx.com
Purch in 11/28/2008 ns7YBSUjkskj  Active     No 1   admin2@xxx.com
Purch in 11/28/2008 SNus8USHJ     Active     No 1   admin3@xxx.com
 
=====================
Public Sub ChangeSubStatus(parmFile As String, parmEmail As String, parmNewStatus As String)
    Dim intFN As Integer
    Dim strFileText As String
    Dim strLines() As String
    Dim lngLoop As Long
    Dim lngSubPosn As Long
    Dim lngDelPosn As Long
    
    intFN = FreeFile
    Open parmFile For Input As #intFN
    strFileText = Input(LOF(intFN), #intFN)
    strLines = Split(strFileText, vbCrLf)
    Close intFN
    For lngLoop = 1 To UBound(strLines) Step 2
        If strLines(lngLoop) = parmEmail Then
            lngSubPosn = InStr(strLines(lngLoop - 1), "Subscription:")
            lngDelPosn = InStr(strLines(lngLoop - 1), "Deleted:")
            strLines(lngLoop - 1) = Left$(strLines(lngLoop - 1), lngSubPosn - 1) & "Subscription: " & parmNewStatus & " " & Mid$(strLines(lngLoop - 1), lngDelPosn)
        End If
    Next
    strFileText = Join(strLines, vbCrLf)
    intFN = FreeFile
    Open parmFile For Output As #intFN
    Print #intFN, strFileText
    Close intFN
End Sub

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 53
A macro to Count the number of rows across all worksheets 3 93
MS Access - Capture pressed key onclick 4 32
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 51
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now