Extract file name from URL

  • 21 November 2014
  • 8 replies
  • 6 views

Badge +2

What is the expression to extract the file name from a URL?

 

e.g. http://site/subsite/library/filename.docx

 

Thanks

Steve


8 replies

Badge +10

hsiungst,

 

Without knowing your plans for the file, or the remainder of the solutin you are building, a regular expression comes to mind.

 

Regards,

 

Mike

Userlevel 3
Badge +10

If you have a predictable URL, i.e. the same amount of '/' characters you could do multiple Find() and Right() methods to strip out the text.  Otherwise you could build a web service that parses and returns string data and hook up to an IPC event or possible pass in parameters via URL.

Userlevel 3
Badge +10

Another note: AFAIK SmartForms and inline expressions do not have recursion functionality, thus you'd have to build a very pattern-specific expression instead of a nice recursive parsing function.

Badge +10

This should match on the filename out of an arbirtrary URL.  Also takes into account any arguments passed beyond that in the URL and strips em out (ex:  http://www.somesite.com/folder/file.docx&someargument=nothingwecareabout

This REQUIRES http:// or https:// to be in the string to work, as something.com can look like a filename.  You could modify it to be more specific if that's in your requirements by having it skip anything ending in .com/.net/.co.uk

 

 

(w+)(.w+)+(?!.*(w+)(.w+)+)

 

Badge +10

Would it be simpler and more relaible to write a service object or even a web service that u pas the url as an input 

 

Then just use the .NET library to maybe get the read adfter the lastindex of "/" character.

 

Then retrun the filename as a an output.

 

Userlevel 3
Badge +10

@s0m3one Yeah, you're right.  A simple web method with Request.Uri.AbsolutePath will do it. [WebMethod] public string StringParse(string inputString) {      return Path.GetFileName(new Uri(inputString).AbsolutePath); }

Badge +2

This should match on the filename out of an arbirtrary URL.  Also takes into account any arguments passed beyond that in the URL and strips em out (ex:  http://www.somesite.com/folder/file.docx&someargument=nothingwecareabout

This REQUIRES http:// or https:// to be in the string to work, as something.com can look like a filename.  You could modify it to be more specific if that's in your requirements by having it skip anything ending in .com/.net/.co.uk

 

 

(w+)(.w+)+(?!.*(w+)(.w+)+)

 Mike, how do I use this in an expression?

 

Thanks

Steve

Badge +10

@hsiungst

 

You would use one of the inline functions dealing with regexp. Documentation is here:  http://help.k2.com/onlinehelp/k2blackpearl/userguide/current/webframe.html#Regular_Expressions.html

 

Again, it depends really how/where you need to perform this logic, but, if it's in one of the places where inline functions are available you should be good to go.  Note that I did not heavily test that regular expression, so you may find an edge case or two that breaks it.

 

I'd also suspect that .NET uses a regular expression to figure out where the last "/" character is in the URL in the web service approach, which is also workable, and may be useful in areas where an inline function cannot be used.

 

Regards,

 

Mike

Reply