Results 1 to 5 of 5

count total number of lines in big text files and export results for each txt to sheet

Threaded View

  1. #1
    Registered User
    Join Date
    03-15-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Talking count total number of lines in big text files and export results for each txt to sheet

    Good morning, experts!

    I am trying to accomplish the following:


    Loop through a given path located in sheet1 on cell G1, get count of lines for each of the text files there for current month and shows those counts in sheet2 in Excel.


    The sheet2 needs to look like my image. (See the table)

    There maybe instances when a text file was not there on the previous month, I want to add it to the list and put the current count. If the text file was there but is not there this month I want to leave it blank and go to the next text file.


    I have some big files (over 133 million records) so this needs to be fast.

    This is the code I have now. Currently I have it pointing to sheet1 but needs to go to sheet2. Also, it shows the count for the first file, but I don't know how to show the next files (the loop).

    The other caveat is that I need to perform this every month, so there should be another column added in November to be able to put the counts for the November files.


    My code:


    
    Dim iRow
    Sub ListFiles()
    
        iRow = 2
        
        'G1 is where the path to the text files is
        Call CountLines(Range("G1"))
    
    
    End Sub
    
    
    Sub CountLines(mysourcepath)
    
    
    'Dimension Variables
    Dim ResultStr As String
    Dim FileNam As String
    Dim FileNum As Integer
    Dim CountLines As Double
    
    Set Myobject = New Scripting.FileSystemObject
    Set mysource = Myobject.GetFolder(mysourcepath)
    On Error Resume Next
    
    
    
    'Target File Extension (must include wildcard "*")
    myPath = "C:\Path1\Path2\Path3\Text File\"
    
    myExtension = "*.txt"
    
    'Target Path with Ending Extention
      FileNam = Dir(myPath & myExtension)
    
    
     'Get Next Available File Handle Number
         FileNum = FreeFile()
    
    
         Open myPath & FileNam For Input As #FileNum
    
             While Not EOF(FileNum)
    
                    'Set The CountLines to 1
                    CountLines = 1
                    'Loop Until the End Of File Is Reached
                    Do While Seek(FileNum) <= LOF(FileNum)
    
                            Line Input #FileNum, ResultStr
                            'Increment the CountLines By 1
                            CountLines = CountLines + 1
                        
                    Loop
                    
                    
    
              Wend
    
    
     For Each myfile In mysource.Files
    
            icol = 1
            Cells(iRow, icol).Value = myfile.Name
            icol = icol + 1
            Cells(iRow, icol).Value = myfile.Path
            icol = icol + 1
            Cells(iRow, icol).Value = CountLines - 1
            iRow = iRow + 1
    
     Next
    
    
    
    End Sub

    I only manage to make the count for the first file, but for the second it shows the same number as for the first one.

    Your help with this will be greatly appreciated.

    Thanks a lot in advanced for your time!


    T.G

    sheet2.jpg
    Last edited by jtammyg; 10-22-2014 at 10:07 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Count the number of rows/lines in a wrap text cell
    By ashleyhkim in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-19-2013, 05:23 PM
  2. [SOLVED] Insert blank lines to make the total number of lines 67
    By raghuprabhu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2012, 07:34 AM
  3. Count Number Of lines in text files and append values to beginning of file
    By motoxeryz125 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-27-2011, 11:14 PM
  4. Count Number of Lines in Text File
    By NicB. in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2006, 08:37 AM
  5. [SOLVED] Count number of lines of text in a range/value.
    By Jacob in forum Excel General
    Replies: 1
    Last Post: 10-18-2005, 04:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1