LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Extracting File Extension From String

    Employee
    Posted 10-19-2016 10:47

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: nvirdi

    Hi, I need to extract a file extension from a string, but the problem is, there can sometimes be multiple dots within the string name, such as file.file2.xls or file.s..d.doc, etc... My final output would just contain .xls and .doc. I know I can use the strFind function to find the position of the dot, and then take everything to the right, but when there are multiple dots within the file name, it is returning everything after the occurrance of the first dot.

    Ex:
    Input string: joe.file.xls
    output: .xls

    Thanks


  • 2.  RE: Extracting File Extension From String

    Employee
    Posted 10-19-2016 11:46

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: stonysmith

    The simplest way is to use split and len.

    s=split(FileName,".")      #build an list (array) of the parts of the name
    Extension = s[len(s)-1]   #get the last (rightmost) array element


  • 3.  RE: Extracting File Extension From String

    Employee
    Posted 10-19-2016 11:56

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: AdamParker

    Stony's way is the method I would use. It would easily allow getting the path and filename as separate fields if necessary.

    But, there are other ways to do it. For instance:

    _fileName = 'FileName'                  # Variable for the filename field
    _fileExtension = _fileName.right(4)     # Get 4 characters from right of string