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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add or Replace 1

Status
Not open for further replies.

Cimso

Technical User
Sep 25, 2001
22
US
What I'm trying to do is look for a string in a file. If it's not there, add it. It it is there, replace it with a new one. I seem to be doing something wrong....

Dim Autobat As Long
Dim aryByt() As Byte
Dim strFile As String
Autobat = FreeFile
Open "C:\Autoexec.bat" For Binary As #Autobat
ReDim aryByt(LOF(Autobat))
Get #Autobat, , aryByt
strFile = StrConv(aryByt, vbUnicode)
Close #Autobat
If InStr(1, strFile, Left("SET %INST=", 11), vbTextCompare) = 0 Then
Autobat = FreeFile
Open "C:\Autoexec.bat" For Append As #Autobat
Print #Autobat, "SET %INST=" + Left(drvInstall.Drive, 1)
Close #Autobat
Else
If InStr(1, strFile, Left("SET %INST=", 11), vbTextCompare) = 1 Then
Autobat = FreeFile
Replace(Left("SET %INST=", 11), Autobat, "SET %INST=" + Left(drvInstall.Drive, 1))
Print #Autobat, "SET %INST=" + Left(drvInstall.Drive, 1)
Close #Autobat
End If
End If

 
However, there is a varying letter after the =, that's why I put in 11.
 
Forget the 11. The extra character has nothing to do with finding the 10.
This works. Change from Binary to Input
Code:
    Dim lngI As Long
    Dim Autobat As Long
    Dim strFile As String
    Autobat = FreeFile
    Open App.Path & "\Autoexec.bat" For Input As #Autobat
    
    strFile = Input(LOF(Autobat), #Autobat)
    Close #Autobat
    lngI = InStr(1, strFile, "SET %INST=", vbTextCompare)
    If lngI = 0 Then  ' Add Card
        Autobat = FreeFile
        Open App.Path & "\Autoexec.bat" For Append As #Autobat
        ''Print #Autobat, "SET %INST=" + Left(drvInstall.Drive, 1)
        ' Testing
        Print #Autobat, "SET %INST=" & "A"
        Close #Autobat
    Else
         Autobat = FreeFile
         Open App.Path & "\Autoexec.bat" For Output As #Autobat
         ''Mid$(strFile, lngI + 10, 1) = Left(drvInstall.Drive, 1)
         Mid$(strFile, lngI + 10, 1) = "B"
         Print #Autobat, strFile; ' Do not add a vbcrlf
         Close #Autobat
    End If
[code] [URL unfurl="true"]www.VBCompare.com[/URL] 
[URL unfurl="true"]www.VBSortGen.com[/URL]
 
Thank you very very much, that works perfectly!!
 
Just thought of better. Just add to string if none.
Remember, crlf is a line seprator not a line terminator.
It matters to some programs beacuse they will see an empty line at the end if the file ends in crlf.
Code:
    Dim lngI As Long
    Dim Autobat As Long
    Dim strFile As String
    Autobat = FreeFile
    Open App.Path & "\Autoexec.bat" For Input As #Autobat
    
    strFile = Input(LOF(Autobat), #Autobat)
    Close #Autobat
    lngI = InStr(1, strFile, "SET %INST=", vbTextCompare)
    If lngI = 0 Then  ' Add Card
        if Right$(strFile,2) <> vbcrlf then
            strFile = strFile & vbcrlf ' line separator
        end if 
        strFile = strFile &  &quot;SET %INST=&quot; & &quot;A&quot;
    Else
        Mid$(strFile, lngI + 10, 1) = &quot;B&quot;
    end if
    Autobat = FreeFile
    Open App.Path & &quot;\Autoexec.bat&quot; For Output As #Autobat
    Print #Autobat, strFile; ' Do not add a vbcrlf
    Close #Autobat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top