splitting filename

This topic contains 8 replies, has 2 voices, and was last updated by Profile photo of ertuu85 ertuu85 4 months, 2 weeks ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #39036
    Profile photo of ertuu85
    ertuu85
    Participant

    Hello, I'm writing a larger script, but one of the pieces I need is to split the file name into 2 parts:

    This File would need split into 2 parts, the first part being the saved to like:

    $date = "06-12-07"
    $vendor = "Vendor Name"

    06-12-07 Vendor Name.pdf 
    

    And to complicate things, sometimes the file name would be just like

    Vendor Name.pdf
    

    Without the year information, which is ok. I'm not sure exactly how to split it up, I cant use split as the filename can have multiple spaces in it.

    Any help would be greatly appreciated.

    #39039
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I'd use regular expressions for this, personally:

    #39040
    Profile photo of ertuu85
    ertuu85
    Participant

    Thank you Dave, that works perfectly! I wish I knew regex a lot better (than 0). Do you have any helpful links or books you use?

    #39041
    Profile photo of ertuu85
    ertuu85
    Participant

    Ah...sorry one more thing...I noticed a date using underscores...

    12_14_12_ – Vendor name.pdf

    How could I edit your regex to include these and maybe any other chars users can find and plug in?

    #39043
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    That's going to get ugly fast 🙂 I've updated the gist's pattern, give it a try.

    I learned Regex years ago from this site: http://www.regular-expressions.info/ . What I like about it is that it's got the general "applies to everything" information, but it's also got sections specific to many different implementations. So you can get data about perl, java, .NET, PowerShell, VBScript, whatever. 🙂

    #39046
    Profile photo of ertuu85
    ertuu85
    Participant

    Thank you again, works great! My code on the other hand leaves a lot to be desired I imagine 😉 But your regex is working flawlessly. Thanks again!

    #39048
    Profile photo of ertuu85
    ertuu85
    Participant

    Crap...last Time I will bug you about this...

    The name can also be like

    10_10_10 F. last.pdf
    

    All files will end in pdf, any way to allow for .'s in the vendor name and just exclude the extension?

    #39049
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Oops, there's supposed to be a $ sign at the very end of the pattern.

    #39050
    Profile photo of ertuu85
    ertuu85
    Participant

    Thanks!

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.