Append file name to each record in file

goss

Active Member
Joined
Feb 2, 2004
Messages
372
Hi all,
I have several files in a folder.
I would like to append the file name to the front of each record in each file
Something like;
Code:
Sub AppendNameRecord()
Dim strFileName as string


For each file in folder
strFileName = file.name

Do While Not EOF
For each rec in file
strRecord = strFileName & "," & rec
Next
Next
End sub
Anyone ever done something like this?
 

Some videos you may like

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

ChrisM

Well-known Member
Joined
Jun 11, 2002
Messages
2,129
Yes, you can do that, sorry can't duplicate code off top of my head, but the excel help file has good enough documentation to get you through this.

Use the FileSystemObject to loop through all the files in a folder, the help file has an example to do exactly that. There are drive, directory, and file objects. You basically set the drive and directory yourself, then loop through all the files it returns.

Use the TextStreamObject, or just old fashioned reads/writes, to edit the records. Help file should get you through using the TextStream object too.
 

goss

Active Member
Joined
Feb 2, 2004
Messages
372
Thanks Chris,

My code below appears to be working.
Appreciate any/all comments for improvements.

Thanks
goss

Code:
Sub ProcessProductFiles()
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Process Product Files
    'Written by Doofenschmirtz
    'Date: 12/13/2009
    'Abstract: Open each product file, append file name to front of each record
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Dim fso As New FileSystemObject
    Dim folder As folder
    Dim file As file
    Dim strFileName As String
    Dim strReadLine As String
    Dim txtFileOut As TextStream
    Dim txtFileIn As TextStream

    Set fso = New FileSystemObject
    Set folder = fso.GetFolder("C:\tmp")
   
    For Each file In folder.Files
        strFileName = file.Name
        Set txtFileIn = fso.OpenTextFile("C:\tmp\" & strFileName, ForReading)
        Set txtFileOut = fso.CreateTextFile("C:\tmp\n - " & strFileName & ".txt", True)
        
        While Not txtFileIn.AtEndOfStream
            strReadLine = ""
            strReadLine = txtFileIn.ReadLine
            strReadLine = strFileName & "," & strReadLine
            txtFileOut.WriteLine (strReadLine)
        Wend
        
        txtFileIn.Close
        txtFileOut.Close
    Next
    
    MsgBox "The process is complete", vbInformation, "Process Product Files"

    Set fso = Nothing
    Set folder = Nothing
    Set txtFileIn = Nothing
    Set txtFileOut = Nothing

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,576
Messages
5,596,987
Members
414,115
Latest member
SFUser

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
Top