Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert point in a text file 1

Status
Not open for further replies.

dawtes

Programmer
Jun 23, 2005
31
0
0
US
What I am looking or hoping for is a simple routine/program to manipulate line text.
For this particular text file, the intent is to add a period 4 digits before a "/" and skip adding a period if it already exists. The text file could be very large so it is impractical to consider adding a period manually line by line. For example, I would like to change the file from this.I'm wondering if someone has an experiecne of editing a text file similar to this.
X
X
X
ITEM01,0220,2357606/00010,,600,2357,
X
X
X
X
ITEM01,0220,2360501/24200,,600,2360,
X
X
X
X

to this

X
X
X
ITEM01,0220,235.7606/00010,,600,2357,
X
X
X
X
ITEM01,0220,236.0501/24200,,600,2360,
X
X
X
X
 
Hello,
One way to do it, and likely the easiest, is to quantify the text you are trying to change so that you can work with it in a math enviroment. You will use the Instr(), and Mid() functions of VB in order to accomplish this.

The Instr()function is explained here: .

The Mid() function is explained here:
Here is a bit of code I came up with so that you can see how these functions work. I used two large text boxes; one to display the sample text you provided, and the other that displays the results. You will also need a command button to initiate the search and reformat, and another to exit the program with.

This is the code:

Private Sub Command1_Click()
Dim Slash As Long
Dim beforeTxt As String
Dim afterTxt As String
Dim LengthTxt As Long

LengthTxt = Len(Text1.Text)
Slash = FindSlash(Text1.Text, "/")
beforeTxt = Mid(Text1.Text, 1, Slash - 5)
afterTxt = Mid(Text1.Text, Slash - 4, LengthTxt)
Text2.Text = beforeTxt & "." & afterTxt



End Sub

You will need the following function inserted into a module of your program's project:

Function FindSlash(searchTxt As String, searchVal As String) As Long

Dim intStart As Long

intStart = InStr(1, searchTxt, searchVal, vbTextCompare)

FindSlash = intStart


End Function


Sure, it's a small piece of code, but the reason that I moved it to a function is because it will be called upon quite a bit once you develop your program. You might want to move more of the code into this same function to clean things up a bit.

Please note that I have not provided you with the ability to search and replace all of the slashes; only the first. The idea for searching and reformatting for multiple slashes is the same as searching for one slash since you will still use the mid() and Instr() functions; however, you will have to use some more math to determine the start point for your next search. It is all pretty straightforward from there. If you need any help then let us know.

LF


"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Oh, I forgot about the second part of your requirements. For this you will use the Mid() function and a little slick math to see if the "." is already there. Here is sample code that will give you a message box that says "true" if the point is already there. Place this before the piece of code that puts the modified text into the text2 box.

If Mid(beforeTxt, Len(beforeTxt), 1) = "." Then
MsgBox "true"
Else
End If

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
You need to realise that you can't change the contents of a textfile, so you have to read the lines in, do what you need to do with them, write them to a temporary file then at the end, delete the original file and rename the temporary one.

Try this:

Private Sub Command1_Click()
Dim p1 As Integer, L As Integer
Dim FileString As String

Open "C:\MyFile.txt" For Input As #1
Open "C:\TempFile.txt" For Output As #2
Do While Not EOF(1)
Line Input #1, FileString
L = Len(FileString)
p1 = InStr(FileString, "/")
If p1 Then
If Mid(FileString, p1 - 5, 1) <> "." Then
FileString = Left(FileString, p1 - 5) & "." & Right(FileString, L - p1 + 5)
Print #2, FileString
Else: Print #2, FileString
End If
Else: Print #2, FileString
End If
Loop
Close #1
Close #2

Kill "C:\MyFile.txt"
Name "C:\TempFile.txt" As "C:\MyFile.txt"

End Sub

Alan

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Bigalbigal,

Saving the file as a temp file, renaming it, and then killing the old file is not necessary. When the file is "read" into memory it is stored inside of a variable so that the program can use the data after the file has been closed; therefore, the orignal text file can be overwritten once the text has changed via the "Open 'c:\Originaltxt.txt' for output as #Filenum" as this will completely overwrite the original file which eliminates the need for a temporary file.

I didn't include the file handleing portion of the code as I assumed that it was implied.

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Private Function InsertDecimal(ByRef strSource As String) As String
With CreateObject("vbscript.regexp")
.MultiLine = True
.Global = True
.Pattern = "[^\.](\d{4}/)"
InsertDecimal = .Replace(strSource, ".$1")
End With
End Function
 
In fact, have a whole example including the file handling ...
Code:
[blue]Option Explicit

Private Sub Command1_Click()
    ReadInsertAndWrite "c:\test2.txt"
End Sub

Private Sub ReadInsertAndWrite(ByRef strFile As String)
    With CreateObject("scripting.filesystemobject")
        .OpenTextFile(strFile, ForWriting).Write InsertDecimal(.OpenTextFile(strFile, ForReading).ReadAll)
    End With
End Sub

Private Function InsertDecimal(ByRef strSource As String) As String
    With CreateObject("vbscript.regexp")
        .MultiLine = True
        .Global = True
        .Pattern = "[^\.](\d{4}/)"
        InsertDecimal = .Replace(strSource, ".$1")
    End With
End Function[/blue]
 
Gotta love those regular expressions! This is a very elegant solution, and strongm is again to be commended for using one line of code where several will suffice. I especially like the use of the With block to manage object creation and destruction.

Code:
.Pattern = "[^\.](\d{4}/)"

Means all instances of exactly four digits followed by a slash and not preceded by a period.

Now, to be pedantic: :)

In the above pattern, the backslash preceding the period is technically unnecessary in square brackets, since in that context the . (meaning any character except newline) has no special meaning, and is therefore already understood to be a literal. It's probably reasonable to leave it in for clarity's sake, however.

Bob


 
Strongm,

Somehow, you never cease to amaze me. Where on earth did you learn all you know.

I hope things are going well for you. I haven't been around these parts in a while. It's good to see you are still at it.

LF

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top