Results 1 to 7 of 7
  1. #1
    jdmeagher is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2015
    Location
    New York
    Posts
    3

    VBA Importing .txt Files WITH FileNames

    Hi all -

    I have several thousand text files I have put in a single folder I need to compile in a single workbook, which I am able to do easily using the below code.
    I am also easily able to extract the FileNames of all the files in that folder with the code below that.

    What I maddeningly can't seem to do is do both - combine the code below such that it will take extract the data in one file line-by-line and then in a NEW column at the end of each row, paste the file name.

    Essentially, I want all of the files in the folder compiled in a new workbook, but I want an additional column so I can sort/identify the data by the original .txt file from which it came.

    Unfortunately, in these original files, there is no unique identifier INSIDE the data file - the only unique identifier is in the FileName.

    Any help or strategies would be greatly appreciated.

    Extracting Files:
    Sub ReadFilesIntoActiveSheet()
    Dim fso As FileSystemObject
    Dim folder As folder
    Dim file As file
    Dim FileText As TextStream
    Dim TextLine As String
    Dim Items() As String
    Dim i As Long
    Dim cl As Range

    ' Get a FileSystem object
    Set fso = New FileSystemObject
    ' get the directory you want
    Set folder = fso.GetFolder("W:\WorkStuff_Rpts\WorkStuff_Folder" )
    ' set the starting point to write the data to
    Set cl = ActiveSheet.Cells(1, 1)


    ' Loop thru all files in the folder
    For Each file In folder.Files

    Set FileText = file.OpenAsTextStream(ForReading)

    ' Open the file
    Set FileText = file.OpenAsTextStream(ForReading)
    ' Read the file one line at a time
    Do While Not FileText.AtEndOfStream
    TextLine = FileText.ReadLine
    ' Parse the line into | delimited pieces
    Items = Split(TextLine, "|")
    ' Put data on one row in active sheet
    For i = 0 To UBound(Items)
    cl.Offset(0, i).Value = Items(i)
    Next
    ' Move to next row
    Set cl = cl.Offset(1, 0)
    Loop


    ' Clean up
    FileText.Close
    Next

    Set FileText = Nothing
    Set file = Nothing
    Set folder = Nothing
    Set fso = Nothing
    End Sub


    Extracting File Names:


    Sub GetFileNames()

    Dim xRow AsLong
    Dim xDirect$, xFname$, InitialFoldr$

    InitialFoldr
    $="G:\"'<<< Startup folder to begin searching from

    With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = Application.DefaultFilePath &"\"
    .Title ="Please select a folder to list Files from"
    .InitialFileName = InitialFoldr$
    .Show
    If.SelectedItems.Count <>0Then
    xDirect
    $=.SelectedItems(1)&"\"
    xFname
    $= Dir(xDirect$,7)
    DoWhile xFname$<>""
    ActiveCell
    .Offset(xRow)= xFname$
    xRow
    = xRow +1
    xFname
    $= Dir
    Loop
    EndIf
    EndWith
    EndSub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Right after this line in your first procedure, the one with TextStream, you should be able to access the name property of your file object.
    For Each file In folder.Files

    so
    file.name


    Also, I would not use file as a variable name. Maybe fsoFile or anything other than file.
    Dim fsofile As file

  3. #3
    jdmeagher is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2015
    Location
    New York
    Posts
    3
    Thanks, itsme.

    Using fsofile.Name (having changed file to fsoFile As file) I get "Compile error: invalid use of property".

    Any suggestions?

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by jdmeagher View Post
    ...
    Any suggestions?
    Post the code you are using and point out which line is throwing the compilation error.

  5. #5
    jdmeagher is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2015
    Location
    New York
    Posts
    3
    The line I have in bold/italicized is what generates the "Compile error: invalid use of property." Specifically, the .Name.

    Sub ReadFilesIntoActiveSheet()
    Dim fso As FileSystemObject
    Dim folder As folder
    Dim fsofile As file
    Dim FileText As TextStream
    Dim TextLine As String
    Dim Items() As String
    Dim i As Long
    Dim cl As Range

    ' Get a FileSystem object
    Set fso = New FileSystemObject
    ' get the directory you want
    Set folder = fso.GetFolder("W:\workthings")
    ' set the starting point to write the data to
    Set cl = ActiveSheet.Cells(1, 1)
    ' Loop thru all files in the folder
    For Each fsofile In folder.Files

    'Read in file name
    fsofile.Name

    ' Open the file
    Set FileText = file.OpenAsTextStream(ForReading)
    ' Read the file one line at a time
    Do While Not FileText.AtEndOfStream
    TextLine = FileText.ReadLine
    ' Parse the line into | delimited pieces
    Items = Split(TextLine, "|")
    ' Put data on one row in active sheet
    For i = 0 To UBound(Items)
    cl.Offset(0, i).Value = Items(i)
    Next
    ' Move to next row
    Set cl = cl.Offset(1, 0)
    Loop

    ' Clean up
    FileText.Close

    Next

    Set FileText = Nothing
    Set fsofile = Nothing
    Set folder = Nothing
    Set fso = Nothing
    End Sub

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Ummm, you do realize this is an Access forum, not an Excel forum..... right?

    Anyway, you have

    fsofile.Name

    You can't just have that as a line. You should do something like
    Code:
    Dim txFileName as string
    
    txFileName = fsofile.Name
    (this might not work - just an example - I don't have much experience with using File system Object. )


    The point is, you want to get the file name into a variable so you can add it to the current row, before moving to the next row. So you have a variable to hold the file name.

    Wish I could help more. I use a different method to open text/csv files. I loop through the folder using Dir() to get the files.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Here, try this code in a button click event. Obviously, you will want to change the value of strFolderPath to a valid folder path that only has a few files inside of it.
    Code:
    Dim fso As New FileSystemObject
    Dim fsoFolder As Folder
    Dim fsoFile As File
    Dim strFolderPath As String
    Dim strFileName As String
    
    strFolderPath = "C:\Test\ExcelFiles"
    Set fsoFolder = fso.GetFolder(strFolderPath)
    
    For Each fsoFile In fsoFolder.Files
        strFileName = fsoFile.Name
        MsgBox strFileName
    Next fsoFile
    Also, you missed changing the variable named file to fsoFile in one place.
    Set FileText = fsoFile.OpenAsTextStream(ForReading)

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Importing PDF Files?
    By swicklund in forum Access
    Replies: 1
    Last Post: 12-27-2013, 09:35 AM
  2. Importing pdf files
    By Road Runner in forum Import/Export Data
    Replies: 1
    Last Post: 04-02-2013, 10:00 PM
  3. importing .csv and .xls files to access
    By TheLost in forum Access
    Replies: 5
    Last Post: 02-16-2012, 08:18 PM
  4. importing Dbase files
    By dzawicki in forum Import/Export Data
    Replies: 2
    Last Post: 01-14-2010, 05:43 PM
  5. Importing multiple files at once
    By NoiCe in forum Import/Export Data
    Replies: 1
    Last Post: 04-01-2009, 10:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums