+ Reply to Thread
Results 1 to 12 of 12

Renaming long file to first subset identified by "_"

  1. #1
    Registered User
    Join Date
    06-04-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    19

    Renaming long file to first subset identified by "_"

    I have a list of files where the file name is "851113_Mail_132013_11886808.pdf" and need to rename it to "851113.pdf". I have found a couple other codes, but nothing seems to work. I have even tried a work around of delimiting the column by _, copy pasting column a into column B and using the below code

    Please Login or Register  to view this content.
    However, even with the correct file name "851113.pdf" in Column B and "851113_Mail_132013_11886808.pdf" in Column A, it claims file not found... This would have to be the "workaround method where I have macros running to pull out the first set of numbers and add on ".pdf", but even this code isn't working.

    Optimally, I would like a code in one to read just up to the _ and rename the files in the folder as the first 6-10 digits.

    I haven't worked with pulling out the beginning of a string before in vba...only excel and SAS.

    Any assistance would be greatly appreciated.
    Last edited by JBeaucaire; 03-21-2013 at 09:08 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Renaming long file to first subset identified by "_"

    Assuming your pathname is correct, you basically want to rename every file IN that path to whatever string leads up to the first _ character?

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 02-11-2013 at 01:36 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-04-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Renaming long file to first subset identified by "_"

    I have encountered an issue with this when there will be duplicate records. I get the error "File already exists". Is there a way to either skip these all together? So "If file exists, then +1"?

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Renaming long file to first subset identified by "_"

    Is this better:

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 03-13-2013 at 07:38 PM.

  5. #5
    Registered User
    Join Date
    06-04-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Renaming long file to first subset identified by "_"

    Negative. That produces results below. I realize my error as above I asked "if file exists, then +1"... You produced exactly what was asked...the fault is mine

    Before:
    851516_From_Me_Now
    851516_From_Me_Now_2
    851516_From_Me_Now_3

    After
    851516
    8515162
    8515163

    Desired effect is either skip the duplicates all together ("851516", "851516_From_Me_Now_2", "851516_From_Me_Now_3") or add "_#" ("851516", "851516_1", "851516_2")

    We need to retain the first segment of 6 as a whole without affecting the name.

    Thank you for all your help! I will be "nothing says 'thanks' like a steak dinner" for all your help!
    Last edited by draconius; 03-13-2013 at 02:49 PM.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Renaming long file to first subset identified by "_"

    Take out the - 1 in the macro and it will keep the _ character.


    If that takes care of your original query, please select Thread Tools from the menu above and mark the thread as solved. Thanks.

  7. #7
    Registered User
    Join Date
    06-04-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Renaming long file to first subset identified by "_"

    That wouldn't work because then the loop would see an _ on the just fixed file and try to change it again...

    It creates a never ending loop within a loop that doesn't go beyond the second file if there is an _.

    Disregarding the desire of _ or +1, is there a simple "if exists, then skip"? Thanks again

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Renaming long file to first subset identified by "_"

    perhaps
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Renaming long file to first subset identified by "_"

    Nice, JP. the OP originally had not wanted the extensions. Does including them resolve the issue?

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Renaming long file to first subset identified by "_"

    the first post said nothing about removing extensions that I saw-what did I miss?

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Renaming long file to first subset identified by "_"

    Optimally, I would like a code in one to read just up to the _ and rename the files in the folder as the first 6-10 digits.
    I may have misunderstood this request.

  12. #12
    Registered User
    Join Date
    06-04-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Renaming long file to first subset identified by "_"

    Thank you JP! On a small trial run, it looks like it is working perfectly. However, when I step through i can go indefinitely. I'll return with the results.

    Again...HUGE THANK YOU TO YOU BOTH!

+ 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