Hey All,
I have a text file that looks like this...
0000001001 eeee
0000001002 aaaa
0000001003 bbbb
0000001004 cccc
0000001005 dddd
How can I search for just the numbers on the left, but replace the whole
string when I find it? In other words, the letters on the right will change,
and I will not know what they are.
(example) I need to change the line "0000001003 bbbb" to "0000001003 zzzz"
but the only info I have is the numbers "0000001003".
Thanks in advance,
Ron
I'm using VB5.
Here is some code I found on here that almost works, except when I use it
it just puts the z's in front of the b's (0000001003 zzzz bbbb)...
And since I'm using VB5, I'm using this Replace function...Code:Public Function ReplaceStringInFile(ByVal StrFileName As String, _ ByVal StrFind As String, ByVal strReplace As String) As String Dim strTest As String Open StrFileName For Binary As #1 strTest = Space(LOF(1)) Get #1, , strTest strTest = Replace(strTest, StrFind, strReplace) Put #1, 1, strTest Close #1 ReplaceStringInFile = strTest End Function Private Sub Command1_Click() ReplaceStringInFile "C:\test.txt", "0000001003", "0000001003 zzzz" End Sub
Code:Public Function Replace(ByVal sIn As String, ByVal sFind As _ String, ByVal sReplace As String, Optional nStart As _ Long = 1, Optional nCount As Long = -1, _ Optional bCompare As VbCompareMethod = vbBinaryCompare) As _ String Dim nC As Long, nPos As Long Dim nFindLen As Long, nReplaceLen As Long nFindLen = Len(sFind) nReplaceLen = Len(sReplace) If (sFind <> "") And (sFind <> sReplace) Then nPos = InStr(nStart, sIn, sFind, bCompare) Do While nPos nC = nC + 1 sIn = Left(sIn, nPos - 1) & sReplace & _ Mid(sIn, nPos + nFindLen) If nCount <> -1 And nC >= nCount Then Exit Do nPos = InStr(nPos + nReplaceLen, sIn, sFind, _ bCompare) Loop End If Replace = sIn End Function




Reply With Quote