• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Change a specific line in a txt file

Annas_12

New Member
Hi

I want to Change a specific line in a ".txt" file in folder

I have a text file, where different forms of content exist, data or strings or symbols, i need to change a specific line (it's only a number), i know its position (8 Line).
in sheet, the file name ".txt" in column A and column B is the number to change.

Is there any way to do so?

Thank you
 

Attachments

Can you please tell us what line and from and to value's
Is it always the same lines or what is the logic to find the specific lines and text
 
yes, is it always the same lines. its position (eighth).
in the example file that I attached, there are number "1" on the eighth line.
I want to change the number "1" based on file name in column A and column B is the number to change.
 

Attachments

Can you test below code and see if it works for you?
Code:
Private Sub CommandButton2_Click()
Dim objFSO As Object 'FileSystemObject
Dim rng As Range
Dim varCont, fOut
Dim strFileName As String
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Set objFSO = CreateObject("Scripting.FileSystemObject")
Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row).ClearContents
'\\ Loop through all possible files
For Each rng In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
    '\\ Build file name
    strFileName = Sheets("Sheet1").Range("E1").Value & Application.PathSeparator & rng.Value
    '\\ See if file exists before processing
    If objFSO.FileExists(strFileName) Then
        '\\ Read contents, replace 8th line, write back
        varCont = Split(objFSO.opentextfile(strFileName, ForReading).readall, vbCrLf)
        varCont(7) = Replace(varCont(7), Trim(varCont(7)), "") & rng.Offset(0, 1).Value
        objFSO.opentextfile(strFileName, ForWriting).Write Join(varCont, vbCrLf)
        rng.Offset(0, 2).Value = "Value Replaced!"
    Else
        rng.Offset(0, 2).Value = "File Not Found!"
    End If
Next
End Sub

I am attaching the file with code.
 

Attachments

program running as I wanted. Amazing :)
Thanks for the help and the solution.
I can't say anything, other than thank you very much @shrivallabha

I'm sorry I asked again
is there a code so that the file name in the range A does not need to use the file extension, so read only file name with the file name and do not need the file extension.
Thanks
 
Yes. I want to replace P-212.txt with P-212 (do not include the file extension).
in column A I want to put the file name only (P-212)
 
You need to change following line to do this from:
Code:
strFileName = Sheets("Sheet1").Range("E1").Value & Application.PathSeparator & rng.Value
to:
Code:
strFileName = Sheets("Sheet1").Range("E1").Value & Application.PathSeparator & rng.Value & ".txt"
 
Back
Top