Add text to every text file in a folder

HotLanta

Board Regular
Joined
Nov 3, 2005
Messages
176
Hi,

I want to take a macro that I have "to the next level". We have some measurement equipment that exports data collected as a .txt file into a folder. When the next test is performed, a second text file is placed in the same folder.

The problem is, the text files just contain the test data. What I want to do is have information in my spreadsheet starting in cell C5 down to cell C13 copied and pasted into each text file in the folder.

Specifically I would like for it to work like this:

Open text file
Delete the very first line of the text file
Copy cell C5 in the very first line
Copy cell C6 in the next line
Continue to cell C13
Loop through all of the *txt files in the folder

The delete the very first line in the text file is important...

FYI The cells in the spreadsheet - I'm want to have the part name, type of test, name of the tester, etc.
The idea is when those parameters change I just update the spreadsheet, run the macro, and I have updated text files.

Thank you in advance for your help
 
look for a line in the text file that begins with "FILNAM/", if the line that begins with "FILNAM/" exists can it add "$$ " to the line.

Try this:
VBA Code:
Sub update_txt()
  Dim textline
  Dim myPath As String, myFile As String
  Dim i As Long, n As Long
  Dim c As Range
  Dim ntime As Double
  
  With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then Exit Sub
    myPath = .SelectedItems(1) & "\"
  End With
  
  myFile = Dir(myPath & "*.txt")
  ntime = TimeValue(Hour(Now) & ":00:00")
  
  Do While myFile <> ""
    On Error Resume Next: Kill myPath & "tmp.txt": On Error GoTo 0
    Open myPath & myFile For Input As #1
    Open myPath & "tmp.txt" For Output As #2
    n = 0
    While Not EOF(1)
      n = n + 1
      Line Input #1, textline
      If n = 1 Then
        For Each c In Range("C5:C13")
          Print #2, c.Value
        Next
      Else
        If InStr(1, textline, "Time=", vbTextCompare) > 0 Then
          ntime = ntime + TimeValue("00:00:01")
          Print #2, "Time=" & Format(ntime, "HH:MM:SS")
        ElseIf UCase(Left(textline, 7)) = "FILNAM/" Then
          Print #2, "$$ " & textline
        Else
          Print #2, textline
        End If
      End If
    Wend
    Close #1
    Close #2
    Kill myPath & myFile
    Name myPath & "tmp.txt" As myPath & myFile
    myFile = Dir()
  Loop
End Sub
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,214,985
Messages
6,122,603
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top