VBA loop into txt files and append a text line

asenav1

New Member
Joined
Jan 16, 2013
Messages
5
I need to add text string in the end, to all files on a folder
For example, on the folder on the path and called C:\folder\ I have a random number of txt files, with text. I want to add a line for example "text" on each of the text files on the folder
I have little knowledge of vba programming, but for what I have read I can use append, but I need something that loop on the files on the folder, and modify them.

So far I tried this:

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">

Sub
footer()
Dim FolderPath AsString
Dim FileName AsString
Dim wb As Excel.Workbook

FolderPath
="C:\folder\"
FileName
= Dir(FolderPath)


DoWhile FileName <>""

Open FileName
For Append As#1
Print
#1,"test"
Close
#1
FileName
= Dir
Loop
EndSub

</code>But seems that its not looking into the files, or appending the text.

All the files on the folder are created by another vba code, and the issue I have is that unless I do like a list of those files, I have not figure it out how to append the text without knowing the list.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
This should work:
Rich (BB code):
Sub footer()

  Dim FolderPath As String
  Dim FileName As String
  
  FolderPath = "C:\folder\"
  If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
 
  FileName = Dir(FolderPath & "*.TXT")
 
  Do While FileName <> ""
    Open FolderPath & FileName For Append As #1
    Print #1, "test"
    Close #1
    FileName = Dir
  Loop

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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