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!

Need txt on a certain line 1

Status
Not open for further replies.

Cimso

Technical User
Sep 25, 2001
22
US
How would I add txt to a certin line in a txt or bat file? Say I wanted to add it to line 4...

Dim lngI As Long
Dim Autobat As Long
Dim strFile As String
Autobat = FreeFile
Open "C:\Autoexec.bat" For Input As #Autobat
strFile = Input(LOF(Autobat), #Autobat)
Close #Autobat
lngI = InStr(1, strFile, "SET %INST=", vbTextCompare)
If lngI = 0 Then
Autobat = FreeFile
Open "C:\Autoexec.bat" For Append As #Autobat
Print #Autobat, "SET %INST=" + Left(drvInstall.Drive, 1)
Close #Autobat
Else
Autobat = FreeFile
Open "C:\Autoexec.bat" For Output As #Autobat
Mid$(strFile, lngI + 10, 1) = Left(drvInstall.Drive, 1)
Print #Autobat, strFile;
Close #Autobat
End If
 
This may be overkill or it may be what you are looking for:

you basically pass it the line number you want to write to, the path of the file, the string to write, and whether you want it appended. If you want it appended it just amends it with a space to the current information in that line. If not it inserts it at that location and drops the remaining lines down one.


Private Sub AddText(intLine As Integer, strPath As String, strText As String, blnAppend As Boolean)
Dim intFileNo As Integer 'store a free file number
Dim aryLineText() As String 'array to store file line information
Dim I As Integer 'loop counter
Dim blnFoundLine As Boolean 'track if specified line to write to was found by EOF

'set initial values
blnFoundLine = False
I = 1
intFileNo = FreeFile

'open up the file for input
Open strPath For Input As #intFileNo
'loop through file line by line
Do While Not EOF(intFileNo)
ReDim Preserve aryLineText(I)
If I = intLine Then 'this is the line the user specified to change
If (blnAppend) Then 'we want to append to this line
Line Input #intFileNo, aryLineText(I)
aryLineText(I) = aryLineText(I) & " " & strText
Else 'we want to add the line instead of appending
aryLineText(I) = strText
End If
blnFoundLine = True 'we found the line specified
Else 'just read the line in and load in array
Line Input #intFileNo, aryLineText(I)
End If
'increment the counter
I = I + 1
Loop
'if we have reached the end of file and not found the line specified then just
'add it to the end of the file

If Not (blnFoundLine) Then
ReDim Preserve aryLineText(I)
aryLineText(I) = strText
End If

Close #intFileNo

intFileNo = FreeFile

Open strPath For Output As #intFileNo
'loop through the array and write the info back to the file
For I = 1 To UBound(aryLineText)
Print #intFileNo, aryLineText(I)
Next

Close #intFileNo

End Sub


I hope this helps you out..... :)
 
Wow, thanks!! I have to do this about 7 times, is there another way that's any less complicated or a bit shorter?
 
Hmmm... I think I should have asked it a little diffently. That is more than I need by far I think. As I rewrote what I did, I no longer had to look if something was already there, just append. What I need to do is just insert a string of txt into file on a certain line.... say like this... but add the string on say line 4 of the said bat file.


Dim Sdrvbat As String
Sdrvbat = FreeFile
Open "C:\Sdrv.bat" For Append As #Sdrvbat
Print #Sdrvbat, "SET %DRV=" + Left(drvSoftware.Drive, 1)
Close Sdrvbat

 
Hmmmm....
Well, you could cut it back just for that. The following will create a new line for the passed text at the line number passed of the file specified.

I would set it up as a public function that you can call from anywhere you will need to.

The following is cut down some but you should be able to call it however many times you need from wherever in the application.


Public Sub AddTextAtLine(strText As String, intLine As Integer, strPath As String)
Dim intFileNo As Integer 'store a free file number
Dim aryLineText() As String 'array to store file line information
Dim I As Integer 'loop counter
'set initial values

I = 1
intFileNo = FreeFile
'open up the file for input
Open strPath For Input As #intFileNo
'loop through file line by line
Do While Not EOF(intFileNo)
ReDim Preserve aryLineText(I)
If I = intLine Then 'this is the line the user specified to change
aryLineText(I) = strText
Else 'just read the line in and load in array
Line Input #intFileNo, aryLineText(I)
End If
'increment the counter
I = I + 1
Loop
Close #intFileNo
'now we will open the file again and output the array
intFileNo = FreeFile
Open strPath For Output As #intFileNo
'loop through the array and write the info back to the file
For I = 1 To UBound(aryLineText)
Print #intFileNo, aryLineText(I)
Next
Close #intFileNo

End Sub


And then for what you are describing above you could just replace what you have with:


AddTextAtLine "SET %DRV=" + Left(drvSoftware.Drive, 1), 4, "C:\Sdrv.bat"


This will add the specified text at line 4 for the file c:\Sdrv.bat.

If you have other lines to add to the same file or even different files just call the same procedure and pass the information you need added and the line you want it added at.

Hope this is more what you are looking for.
 
That was extremly helpful!!! Thank You very very much!!
 
One more question.... I made that a Public Function. Now can I access that from another form in the same project?
 
I would add a standard module to the project if you do not already have one. You can call it basCommon or something like that. Add the procedure to this module and you will be able to call it from any of your forms within the project. Make sure it has Public Sub versus Private Sub though.
 
This is all getting a bit frustrating. I'm new at this, I'm not even a programer, but I'm doing some projects for the company. Now because of the error checking for the customer ID, I do have to check to see if "SET %DRV=" + Left(drvSoftware.Drive, 1) has been added to the file because a number isn't added, all of the "Set..." get added to the batch file and then when the customer ID is added into it's field, the "SET %DRV=" + Left(drvSoftware.Drive, 1) and the rest of them get added to the file again. I might just be babbling by now, but I hope that makes sence. I don't know if the first way you showed me will work since I need things on certain lines and not addended to the end of the file. Sorry to keep buggin you... but thank you very very much for your help jitter!!
 
I apologize Cimso but I have to get away from the computer for awhile. I hope I have not led you in the wrong direction. I would probably start a fresh post and see if you get any hits on it describing exactly what you are trying to do. There might be someone that has a better idea than I do about an easier way of doing it. I guess from what you are saying is that you need to first check to see if the particular line already exists in the file. What happens if it does exist? Do you have to do anything to it? Next, do you always know what line number you need to write to everytime? The procedure I gave you will insert the specific line you tell it to. If you pass line 4 then it will write what you send to it to line 4 and the original line 4 will become line 5. So you have complete control over that. Is that what you need? Or do you need to append to a line? How does CustomerID influence the whole process? You have my respect for having to figure all this stuff out when it is not really your job. It can become quite confusing really fast. Talk to you later.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top