+ Reply to Thread
Results 1 to 4 of 4

Amending a text file

Hybrid View

  1. #1
    Registered User
    Join Date
    11-18-2011
    Location
    Dorset
    MS-Off Ver
    Excel 365
    Posts
    37

    Amending a text file

    I have a bunch of routines to create a plain text data file from a spreadsheet.

    The files comprise literally thousands - or even tens of thousands - of lines, with usually only a few characters per line.

    I want to run through a file - "myFile.txt" - i.e. read in every line, - and if it comes across, say, "ABC", then replace it with, say, "XYZ"

    I *could* read in each line, and then write each line into another file, changed as necessary; then delete the old file and rename the new one with the name of the old file.

    But is there a quicker / easier way ?

    There may not be any occurrence, or there might be two or three - never more - and usually near the end of the file.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Amending a text file

    Just read the file as one giant string. Then run replace.

    It isn't clear what your exact requirement is... but something like below for single file read, replace string(s) and write back.
    Sub Demo()
    Dim iFile As String: iFile = "C:\Test\full-20190501.txt"
    Dim intFF As Integer: intFF = FreeFile()
    Dim textData As String
    
    Open iFile For Input As #intFF
    textData = Input$(LOF(intFF), #intFF)
    Close #intFF
    
    textData = Replace(textData, "Original String", "New String", , , vbBinaryCompare)
    Open iFile For Output As #intFF
    Print #intFF, textData
    Close #intFF
    End Sub
    EDIT: FYI - Tested on 34Mb log file with 250k+ rows. Took about 3~4 sec. Combine it with DIR() function to iterate over multiple files if needed.
    Last edited by CK76; 07-09-2019 at 02:12 PM.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    11-18-2011
    Location
    Dorset
    MS-Off Ver
    Excel 365
    Posts
    37

    Re: Amending a text file

    CK76 -
    Thank you !
    That is splendid -
    JUST the job.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,432

    Re: Amending a text file

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

    By the way, are you really still using Excel 2003? If not, please update your user profile.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. SUMPRODUCT + TEXT (Month 27th) - Help amending this formula?
    By domgilberto in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 05-22-2014, 06:18 PM
  2. Help with VBA amending
    By sideshow1987 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-26-2011, 07:32 AM
  3. Excel 2007 : Amending data in a text file
    By Steve1973 in forum Excel General
    Replies: 1
    Last Post: 04-26-2011, 08:48 PM
  4. Changing/Amending Text String In Cell
    By Kumara_faith in forum Excel General
    Replies: 10
    Last Post: 02-19-2011, 10:44 AM
  5. looping, Open text file, copy text, close text file
    By jwilds1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-18-2009, 05:12 PM
  6. Replies: 0
    Last Post: 01-19-2006, 05:10 AM
  7. Amending a text file
    By DHallam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-19-2005, 07:19 AM

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