+ Reply to Thread
Results 1 to 11 of 11

Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each File

  1. #1
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each File

    Hello,

    I have a folder full of txt files and I want to rename them using a vba sub routine. I want each file to be renamed to a string that is constructed from the contents of the first line of each file. I am planning on concatenating strings using the Mid() function on the first line of each file. I just don't know how to loop through the files, single out the first line, and rename the file. Any help would be much appreciated.

    Cheers!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    Hello bruppert,

    Can you post one the text files as an example along with what in the first line is to be used for the new name?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    Hi Leith,

    Unfortunately I cannot post any of the files. The first line is a string of dates and other characters. Here is the code that I use to create the file name from the first line

    id = Mid(firstlineoffile, 1, 3)
    date1 = Mid(firstlineoffile, 5, 8)
    date2 = Mid(firstlineoffile, 15, 8)

    FileName = id & date1 & date2 & ".txt"
    Last edited by bruppert; 07-02-2019 at 04:22 PM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    Hello bruppert,

    Thanks for the update. Will the folder always the same and if so then what is the path? If not I can add a folder selector to the macro.

  5. #5
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    I also need to get contents from the second line of the file. Here is the updated filename constructor code.



    id = Mid(firstlineoffile, 1, 3)
    date1 = Mid(firstlineoffile, 5, 8)
    date2 = Mid(firstlineoffile, 15, 8)
    signal = Mid(secondlineoffile, 1, 3)

    FileName =signal & id & date1 & date2 & ".txt"

  6. #6
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    Hi Leith,

    The folder changes. New data comes in every month and we keep separate folders for each month.

  7. #7
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    If possible, I would like to keep the folder the same. For example, I select a folder from the selector which contains all the txt files. The files are then renamed but are kept in the same folder.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    Hello bruppert,

    You can change the path from "C:\Test" to where your files are located.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    Hi Leith,

    This is not the first time you've helped me out. This solves many issues. Many thanks for the timely replies!

    Cheers,
    bruppert

  10. #10
    Registered User
    Join Date
    05-03-2019
    Location
    San Francisco
    MS-Off Ver
    7
    Posts
    28

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    How do I mark as solved?

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Renaming Multiple .Txt Files in a Folder to Specific Contents of First Line of Each Fi

    Hello bruppert,

    You're welcome.

    Select Thread Tools from the menu link above and mark this thread as SOLVED.

+ 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. Replies: 3
    Last Post: 12-28-2015, 08:14 AM
  2. Copying and Renaming a Folder with Multiple Files
    By pecos1970 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-26-2015, 05:12 PM
  3. [SOLVED] VBA Code required for renaming files in a specific folder
    By yoursamrit2000 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-24-2015, 05:11 PM
  4. [SOLVED] Creating new folder, copying files and renaming files in folder
    By LionelNZ in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-24-2015, 04:18 PM
  5. [SOLVED] Extract specific cells from multiple excel files in same folder to new summary file
    By Derek Holden in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-02-2014, 01:43 AM
  6. [SOLVED] Renaming Multiple Excel Files with part of file name
    By shridhar.dash in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-23-2013, 04:21 PM
  7. Renaming multiple files in a Folder
    By csmithee in forum Excel General
    Replies: 1
    Last Post: 06-11-2009, 02:36 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