Extracting a file name from a string variable

lonnyo
lonnyo used Ask the Experts™
on
I have a soap call that is returning data in the following format:

ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG}

I need to extra the directory and file name from the following section:
Path={directory/filename}

The id number can be any number of characters, so starting x number of characters in is not an option.

What is the best method for extracting the direcotry/filename.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
this regex will do it:

Path=\{([^/]+)/([^\}]+)}

Open in new window


group 1 holds the directory
group 2 holds the filename

(assuming coldfusion can do regular expressions...)
<cfset theString = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG} ">

<cfset fullpath = listLast(theString,"{}")>


#getDirectoryFromPath(fullPath)#

#getFileFromPath(fullPath)#
<cfset v = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG} ">


<cfset x = listlast(v, "{")>
<cfset x = replace(x, "}", "")>
<cfoutput>#x#</cfoutput>
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

gd see we're on the same page but - listLast(theString,"{}")> wouldn't work would it?

that would imply delimiters of {}

ID={}43054{}|Path={}C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG{}
another way to do is to use the regex from basicinsticnt and use reMatchNocase

<cfset v = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG}">


<cfset x = rematchnocase("Path=\{([^/]+)/([^\}]+)}",v)>
<cfoutput>
<cfloop array="#x#" index="i">
      #i#
</cfloop>
</cfoutput>

because reMatch returns an array you have to output it in a cfloop

but you'd still have to clean it up

Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG}

so the listlast is probably less lines of code
> gd see we're on the same page but - listLast(theString,"{}")> wouldn't work would it?

Hey Sid -
Many list functions can take multiple delimiters, such as cfloop.  I believe this is one of them.   Since listLast doesn't return empty values, specifying both should get the string from between them..
Most Valuable Expert 2015

Commented:
<cfset fullpath = listLast(theString,"{}")>

That's what I would've tried. For some reason it returns an empty string under CF9, but works if you use listGetAt or getToken() instead.

      <cfset fullpath  = getToken(theString, 4, "{}")>
Most Valuable Expert 2015
Commented:
  <cfset v = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG}(space here)">


Never mind the trailing space is the culprit ;-)  Remove it and it works as expected.
cool, thanks..

so just throw in a trim or add space as another delimiter


cfset theString = "ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG} ">

<cfset fullpath = listLast(trim(theString),"{}")>


#getDirectoryFromPath(fullPath)#

#getFileFromPath(fullPath)#
oops that trailing space was mine in my test code so my test worked the same as agx's ie: it didn't

I didn't know about the multiple delimiter thingy - nice..

Author

Commented:
Sorry, I don't think I explained it right.  In the value that I get:

ID={43054}|Path={C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG}

I only want to use:

C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG

I tried playing with these options, but I can't get it right.

Thanks
mine produces

C134/e1e4c0cf-6927-4414-835d-aa1e84600a9d.JPG

as does gd's & agx's ??
well, gd's splits' it up if you want the whole thing just output



#fullpath#

Author

Commented:
Sorry about that, SidFishes. I pasted the code again and it worked perfect.<br /><br />gdemaria - first code did cause a problem because of the trailing space.
> gdemaria - first code did cause a problem because of the trailing space.

... so trim it...

Note that Sid's code leaves the trailing space connected to your resulting string, so you have to trim it no matter what.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial