+ Reply to Thread
Results 1 to 5 of 5

Count Number Of lines in text files and append values to beginning of file

  1. #1
    Registered User
    Join Date
    01-11-2005
    Posts
    3

    Count Number Of lines in text files and append values to beginning of file

    Hello,

    I have 50 text files in a directory called "AllFiles"

    I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and append this information to the beginning of each text file.

    For example, lets say i have 5 files in a directory in the "AllFiles" directory:

    file1.txt ---has 500 lines
    file2.txt ---has 600 lines
    file3.txt ---has 550 lines
    file4.txt ---has 700 lines
    file5.txt ---has 480 lines


    The result of the program should be this:

    The first line of file1.txt should read: "there are 400 lines in this file and 100 bonus lines"
    The first line of file2.txt should read: "there are 400 lines in this file and 200 bonus lines"
    The first line of file3.txt should read: "there are 400 lines in this file and 150 bonus lines"
    The first line of file4.txt should read: "there are 400 lines in this file and 300 bonus lines"
    The first line of file5.txt should read: "there are 400 lines in this file and 80 bonus lines"

    Is there any way that i can do this using the Microsoft Excel VB editor?

    I don't have visual studio, but i do have the Microsoft office Visual Basic editor, and i have tried a few basic things to do this with no success.

    Any ideas would be greatly appreciated.

    If it matters, my computers operating system is Windows 7.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Count Number Of lines in text files and append values to beginning of file

    I don't know if Excel will do this for you. Have you thought of trying another language like Java? I know that Java can easily open .txt files and do all sorts of things within the .txt file, including count lines and append new string data.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Count Number Of lines in text files and append values to beginning of file

    Hi motoxeryz125,
    adapt this code

    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Count Number Of lines in text files and append values to beginning of file

    oops.. just read you need to insert it in the first line of file so..
    Please Login or Register  to view this content.
    Last edited by pike; 04-27-2011 at 03:02 AM. Reason: add option explicit and dims

  5. #5
    Registered User
    Join Date
    01-11-2005
    Posts
    3

    Re: Count Number Of lines in text files and append values to beginning of file

    Thanks for the help Pike

    I will implement your code and let you know how things work out.

    Again, thanks a lot

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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