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?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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