|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How to change text into a txt file
Well,what I want to do is to open a txt file,find a certain place within the file and change its content.Then save the file,and maybe create a second file(like a record) containing the date and the change made by the user.Could you assist me?I have included a part of the file's content I want to interfere with.
.................................................. .................................................. .................................... # ServerRoot: The top of the directory tree under which the server's # configuration, error, and log files are kept. # # NOTE! If you intend to place this on an NFS (or otherwise network) # mounted filesystem then please read the LockFile documentation (available # at <URL:http://httpd.apache.org/docs-2.0/mod/mpm_common.html#lockfile>); # you will save yourself a lot of trouble. # # Do NOT add a slash at the end of the directory path. # ServerRoot "C:/Program Files/Apache Group/Apache2" # # ScoreBoardFile: File used to store internal server process information. # If unspecified (the default), the scoreboard will be stored in an # anonymous shared memory segment, and will be unavailable to third-party # applications. # If specified, ensure that no two invocations of Apache share the same # scoreboard file. The scoreboard file MUST BE STORED ON A LOCAL DISK. # #ScoreBoardFile logs/apache_runtime_status # # PidFile: The file in which the server should record its process # identification number when it starts. # PidFile logs/httpd.pid # # Timeout: The number of seconds before receives and sends time out. # Timeout 300 .................................................. .................................................. .................................... Let's suppose that I want to change the file root "C:/Program Files/Apache Group/Apache2" found next to ServerRoot or the value 300 next to Timeout.How can I do this kind of changes within my file,without of course the risk of erasing all the other data?I just want to have these certain parts changed at will.Can you make it posible?Thanks again! |
|
#2
|
|||
|
|||
|
In general to edit a file from code you'll open the file for reading and open another file for writing, read in from the original file and copy what you read out to the destination file, makeing any changes to the output you want from your code. When the file is completely processed, you either delete the original and rename the new file to the old name, or simply rename the new file to some other filename leaving the original file intact.
__________________
====== Doug G ====== I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain |
|
#3
|
|||
|
|||
|
Some kind of VB code maybe?
Well could show how to do so in code?I understood your thinking but I would be grateful if you showed me how to proceed.You can take the part of the file I included in my msg above as a basis.Let's suppose I just want to interfere with ServerRoot(by changing the text inside the " "),Pidfile(the same way as ServeRoot )and Timeout (change the value 300 next to it).Please assist me!
|
|
#4
|
|||
|
|||
|
Do it one step at a time. Create some small VB code that can read and write a text file. If you have problems doing that post your problem code and describe what's wrong. There are example codes in the VB online help, and at many sites such as www.mvps.org
|
|
#5
|
|||
|
|||
|
Well,could you check the code I'm sending you and point me my mistakes?I can't make it work properly.Thanks a lot!
.................................................. .................................................. ............................................... Dim oldtext, newtext As String Dim OldT, NewT As String Dim testfile, abg Dim root Dim sr As Boolean 'Public Type Record ' vbDate 'End Type Public Function ReplaceInFile(Inputfile As String, Outputfile As String, OldT As String, NewT As String) Dim Fnum As Integer ' get a FreeFile number Dim FileLength As Long 'Just in case its really big Dim TheString As String 'I open the file as binary to avoid some 'complications with reading in a full file. 'This just so I can skip some trouble shooting with 'some special characters in certain exe files. 'It might be that character you wish to replace. Fnum = FreeFile Open Inputfile For Binary As #Fnum ' Open file. FileLength = LOF(Fnum) ' Get length of file. TheString = Input(FileLength, #Fnum) Close #Fnum ' Close file. 'OldT = Whatever text you send form a form or function to search for 'NewT = Whatever text you wish to replace OldT with 'Call the function to replace text Call ReplaceText(TheString, OldT, NewT) 'Now print the result to a new file 'so you dont overwrite your original Fnum = FreeFile Open Outputfile For Output As #Fnum ' Open file. Print #Fnum, ReturnValue ' the ReturnValue from the replacement Close #Fnum ' Close file. End Function Public Function ReplaceText(CleanThis As String, oldtext As String, newtext As String) 'To get the len of the string Dim StrLn As Long 'To split the string Dim PartA As String, PartB As String 'If search string is found, get its start position Dim FoundP As Long Dim OldLn As Long On Error GoTo ErrHandle 'Set a value for the len of the old text to be replaced OldLn = Len(oldtext) StrLn = Len(CleanThis) 'Loop through the string until all occurences are eliminated Do While InStr(1, CleanThis, oldtext) <> 0 FoundP = InStr(1, CleanThis, oldtext) 'Get PartA of the string (before found occurance) PartA = Left(CleanThis, FoundP - 1) 'Get PartB of the string (after found occurance) PartB = Right(CleanThis, StrLn - FoundP - OldLn + 1) '*NOTE* '************************************************* ***** '+ 1 to avoid a skip in adding found len and old len 'The previous line could also be written ' 'PartB = Right(CleanThis, StrLn - (FoundP + OldLn - 1)) '************************************************* ***** 'REBUILD THE STRING BEFORE NEXT LOOP 'This adds a space before and after NewText 'and trims out unneccessary spaces too! CleanThis = Trim(PartA) & " " & Trim(newtext) & " " & Trim(PartB) 'GET NEW LEN BEFORE LOOPING StrLn = Len(CleanThis) Loop 'Set result of the function ReplaceInString = CleanThis 'Avoid error handling Exit Function 'Error Handler ErrHandle: Select Case Err.Number Case Err.Number MsgBox "Your function executed with an error " & Err.Number & vbCrLf & vbCrLf & Err.Description, vbExclamation, "Error " & Err.Number Err.Clear End Select Resume Next End Function Private Sub ChkSection1_Click() Load frmSection1 frmSection1.Show End Sub Private Sub ChkSection2_Click() Load frmSection2 frmSection2.Show End Sub Private Sub ChkSection3_Click() Load frmSection3 frmSection3.Show End Sub Private Sub cmdLoadFile_Click() testfile = root If Len(testfile) > 1 Then Open testfile For Input As #1 Debug.Print "to arxeio anoikse" Do While EOF(1) = False abg = Input$(1, #1) Loop Close #1 End If If Len(testfile) = 0 Then MsgBox "Please select a file to open.", vbExclamation, "Text Editor & Search machine" End If End Sub Private Sub cmdViewRecords_Click() Dim filenumber As Integer Dim vrec As String Dim fname Dim saved As Boolean filenumber = FreeFile With dlgRecords .Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt|Conf Files(*.conf)" .FilterIndex = 2 .ShowOpen End With fname = root Open fname For Output As #filenumber Do While EOF(filenumber) = False vrec = Input$(LOF(filenumber), #filenumber) Loop Close #filenumber If saved = True Then Load frmRecord frmRecord.Show 'txtRecord.Text = fname End If End Sub Private Sub File1_Click() root = File1.Path + "\" + File1.FileName End Sub Private Sub Form_Load() Drive1.Drive = "C:\" Dir1.Path = "C:\Program Files\Apache Group\Apache2" File1.Path = "C:\Program Files\Apache Group\Apache2\conf" File1.Pattern = "*.txt" End Sub Private Sub cmdApply_Click() If sr = True Then Call ReplaceInFile("c:\checkin'.txt", "c:\checkin'2.txt", OldT, NewT) sr = False End If End Sub Private Sub cmdCancel_Click() Unload frmSection1 Unload frmSection2 Unload frmSection3 Unload Me End Sub Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub Private Sub ChkListenPort_Click() If ChkListenPort.Enabled = True Then LblListenPort = "Yes" Text2.Visible = True End If End Sub Private Sub Text2_Change() Text2.Enabled = False End Sub Private Sub TxtServerRoot_Change() sr = True With sr = True OldT = "ServerRoot" & "C:/Program Files/Apache Group/Apache2" NewT = "ServerRoot" & TxtServerRoot.Text End With End Sub |
|
#6
|
|||
|
|||
|
What doesn't work right?
You should be able to isolate a problem with the code by stepping through your code with the VB debugger. |
|
#7
|
|||
|
|||
|
Well I did so.I checked the code using the debbuger and the program keeps crashing at the point where I "call" the replaceinfile function(that's the cmdApply button's job).The debbuger points that there's a problem with the oldtext variablewhen I call the function.I really have no idea why this happens.Can you sort this out?Thanks!
|
|
#8
|
|||
|
|||
|
What, does the entire VB IDE crash?
Otherwise post the actual error(s). You should be able to use the locals window and the immediate window to easily determine what the problem is. |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > How to change text into a txt file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|