Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up help with extracting file name from a browse box input

    Does anyone know how one would go about extracting the file name from a browse box input?
    Like this:
    c:\images\pictures\gifs\filename.gif

    How would you get "filename.gif" out of that knowing everyone has a different directory structure on their computer?
    I want to use a browse box as the input, and "automatically" get the file name from it.

    If anyone knows that would be great I am new to Perl and am learning as I go teaching myself. Sometimes you need help from someone that actually knows what they are doing!!

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If $filename held the filename with the full path you could use this:

    $filename =~ m/([^\/\\]+)$/;
    $filename = $1;

    To get the actual name bit, minus the path.

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    That worked!!

    Mouldy Goat thanks for the help it seems to work fine. Thanks again.
    If it isn't a big long explaination could you tell me how this works?
    If it is too long forget it I don't want to impose on your time.
    Thanks again!

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure I'll do it in bits for you, there's a good variety of regex bits in it..

    The first line means do a match on the string $filename and save the bit it matches for later use.

    This is the regex:
    Code:
    /([^\/\\]+)$/;
    The parentheses (), mean save everything matched in the variable $1 so that it can be used later.

    The square brackets mean a character class, i.e. a selection of characters, so that [aeiou] would match one single vowel.

    A caret ^ at the very beginning of a character class means that the character class is negated, so that [^aeiou] would match anything that isn't a vowel - i.e. any one consonant.

    Inside the actual character class there's basically just a forward slash and a backward slash.. but because these are both special characters they have to be preceded by a backslash themselves in order to actual match.

    The + means match one or more of whatever precedes it - i.e. anything that isn't a forward or backward slash.

    The $ at the end of the string means match the expression at the very end.

    So all in all it means this: match one or more non-slash character at the end of the string, i.e. everything after the last slash.

    I've made it sound a lot more complicated than it is.. hope I helped anyway.

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    By the way, since you're new to Perl I should explain that regex means regular expression which means pattern matching essentially.

    They might seem a little convoluted and perhaps even superfluous at first, but they're really incredibly useful once you get the hang of them. You introduce a regular expression with a =~, for example:

    Code:
    $phrase =~ /monkey/;
    Will check to see if the variable $phrase contains the word monkey, and if it does it will return true.

    More complicated regular expressions can do things like validating email addresses, checking in text to automatically detect website addresses, making user input safe from a security point of view and parsing HTML documents.

  • #6
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Mouldy_Goat,

    I appreciate your time to explain it.
    It works perfectly!!

  • #7
    New Coder
    Join Date
    Aug 2002
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thx also.

    Regex's always give me @%$# and this is one of the best explanations I've seen yet.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •