{"id":714,"date":"2016-08-20T15:34:09","date_gmt":"2016-08-20T20:34:09","guid":{"rendered":"http:\/\/www.lorenzcom.com\/?p=714"},"modified":"2020-12-03T07:27:07","modified_gmt":"2020-12-03T13:27:07","slug":"pathparser","status":"publish","type":"post","link":"https:\/\/lorenzcom.com\/?p=714","title":{"rendered":"PathParser"},"content":{"rendered":"<h3>PathParser ( thePath , returnElementPHFNE )<\/h3>\n<p>PathParser returns the path directory, home directory, full filename and extension, filename, or extension from a folder and\/or file path string.<\/p>\n<ul>\n<li>Easily extract path elements from thePath.<\/li>\n<li>Preserves FileMaker Pro file reference prefixes (file:, filemac:, and filewin:).<\/li>\n<li>Omit directories forwards or backwards from thePath.<\/li>\n<li>The home or ~ (tilde) element request returns the home path &#8211; path without Get ( SystemDrive ) prepended.<\/li>\n<li>The path and filename element separation is set to the leftmost path separator in thePath.<\/li>\n<li>Works with the thePath string as passed &#8211; it does not format thePath, except to prepend a missing path separator when returning directory path or home requests.<\/li>\n<li>Use with the <a href=\"http:\/\/www.lorenzcom.com\/software\/filemaker-pro\/pathmaker\/\">PathMaker<\/a> custom function for added path formatting control.<\/li>\n<\/ul>\n<h4>Parameters<\/h4>\n<ul>\n<li>thePath &#8211; A folder and\/or file path.<\/li>\n<li>returnElementPHFNE &#8211; The element in thePath to return. Determined from the first letter of the passed parameter or the passed number:\n<ul>\n<li>p (path) &#8211; Returns the path directory, without the filename and extension.<\/li>\n<li>h or ~ (home) &#8211; Returns the home directory path, without the system drive information.<\/li>\n<li>f (fullfilename) &#8211; Returns just the filename and extension, without the path directory.<\/li>\n<li>n (name) &#8211; Returns just the filename, without the path directory and without the file extension.<\/li>\n<li>e (extension) &#8211; Returns just the file extension, without the path directory and without filename.<\/li>\n<li>+\/-# (omit directories) &#8211; Returns the directory path after omitting the specified number of directories.\n<ul>\n<li>A positive number omits directories forwards from the left of thePath.<\/li>\n<li>A negative number omits directories backwards from the right of thePath.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>Examples<\/h4>\n<ul>\n<li>PathParser ( &#8220;file:&#8221; &amp; Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;p&#8221; ) = file:\/Macintosh HD2\/Users\/user\/Documents\/<\/li>\n<li>PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;h&#8221; ) = \/Users\/user\/Documents\/<\/li>\n<li>PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;fullfilename&#8221; ) = Untitled.pdf<\/li>\n<li>PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;n&#8221; ) = Untitled<\/li>\n<li>PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;e&#8221; ) = pdf<\/li>\n<li>PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;2&#8221; ) = \/user\/Documents\/<\/li>\n<li>PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;-1&#8221; ) = \/Macintosh HD2\/Users\/user\/<\/li>\n<\/ul>\n<h4>Help<\/h4>\n<p>Passing &#8216;?&#8217; as the only text in returnElementPHFNE returns this help text: PathParser ( thePath , returnElementPHFNE: [p]ath, [h]ome or [~], [f]ull name and extension, [n]ame, [e]xtension), [+\/-#] omit # of directories ).<\/p>\n<p><strong>PathParser code:<\/strong> Copy the selected code (click on Select All to highlight all the code) to the clipboard and paste it into a newly created Custom Function. <a href=\"\/docs\/FileMaker\/PathParser.zip\">Download the code in a text file from here.<\/a><\/p>\n<form><textarea id=\"&lt;?php\"> style=&#8221;width:100%; height:180px; font-family:Arial,Helvetica; font-size:.90em; line-height:normal;&#8221;&gt;&lt;!&#8211;raw&#8211;&gt;Let ( [&lt;br \/&gt;<br \/>\n\/\/PATH: Default to NULL if thePath is empty.&lt;br \/&gt;<br \/>\np =  If ( IsEmpty ( Trim ( thePath ) ) ; &#8220;&#8221; ; thePath ) ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/FM PREFIX: Preserve file reference prefix.&lt;br \/&gt;<br \/>\nfrp = Middle ( p ; 1 ; Position ( p ; &#8220;:&#8221; ; 1 ; 1 ) ) ;&lt;br \/&gt;<br \/>\nfrn = GetAsBoolean ( PatternCount ( &#8220;xfile:xfilemac:xfilewin:xfmnet:&#8221; ; &#8220;x&#8221; &amp; Trim ( frp ) ) ) ;&lt;br \/&gt;<br \/>\np = If ( frn ; Replace ( p ; 1 ; Length ( frp ) ; &#8220;&#8221; ) ; p ) ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/REQUESTED ELEMENT OR OMIT NUMBER: Get returnElementPHFNE request (PHFNE~ or #).&lt;br \/&gt;<br \/>\nr = Left ( Trim ( returnElementPHFNE ) ; 1 ) ;&lt;br \/&gt;<br \/>\nn = Int ( GetAsNumber ( If ( r = &#8220;?&#8221; ; &#8220;&#8221; ; returnElementPHFNE ) ) ) ;&lt;br \/&gt;<br \/>\nr = If ( IsEmpty ( n ) ; r ; &#8220;#&#8221; ) ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/OS PATH REQUEST: Is home path requested?&lt;br \/&gt;<br \/>\nosp = GetAsBoolean ( PatternCount ( &#8220;~h&#8221; ; r ) ) ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/SYSTEM PLATFORM. -1 = OS 9, 1 = OSX, -2 = Windows.&lt;br \/&gt;<br \/>\nsp = Abs ( Get ( SystemPlatform ) ) ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/PATH SEPARATOR: Get path separator based on platform: if Mac and not &#8220;:&#8221; use &#8220;\/&#8221; for Unix. Allow &#8220;\/&#8221; or &#8220;\\&#8221; for Windows.&lt;br \/&gt;<br \/>\nps = Left ( If ( sp = 1 ; Filter ( p ; &#8220;:\/\\\\&#8221; ) ; Filter ( p ; &#8220;\\\/&#8221; ) ) ; 1 ) ;  \/\/Precedence is important.&lt;br \/&gt;<br \/>\ns = If ( IsEmpty ( ps ) ; &#8220;\/&#8221; ; ps ) ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/PATH VALUES: Create the path list.&lt;br \/&gt;<br \/>\npl = Substitute ( p ; s ; &#8220;\u00b6&#8221; ) ;&lt;br \/&gt;<br \/>\n\/\/Count of elements. Add 1 if rightmost character is a path separator, no filename.&lt;br \/&gt;<br \/>\nplc = ValueCount ( pl ) + ( Right ( thePath ; 1 ) = s ) ;&lt;br \/&gt;<br \/>\n\/\/Path is the left values of pl if the count is greater than 1, else only a filename was passed.&lt;br \/&gt;<br \/>\npv = If ( plc &gt; 1 ; LeftValues ( pl ; plc &#8211; 1 ) ; &#8220;&#8221; ) ;&lt;br \/&gt;<br \/>\n\/\/Count the path values.&lt;br \/&gt;<br \/>\npvc = ValueCount ( pv ) ;&lt;br \/&gt;<br \/>\n\/\/Adjust pv for OS home path request.&lt;br \/&gt;<br \/>\npvi = Case ( osp and ( GetValue ( pv ; 2 ) = Substitute ( Get ( SystemDrive ) ; &#8220;\/&#8221; ; &#8220;&#8221; ) )  ; 3 ;  osp ; 2 ; 1 ) ;&lt;br \/&gt;<br \/>\n\/\/Adjust the path values with index.&lt;br \/&gt;<br \/>\npv = MiddleValues ( pv ; pvi ; pvc ) ;&lt;br \/&gt;<br \/>\n\/\/Adjust the path values by the omit number, if present.&lt;br \/&gt;<br \/>\npv = Case ( r \u2260 &#8220;#&#8221; or n = 0 ; pv ; n &lt; 0 ; LeftValues ( pv ; pvc + n ) ; n &gt; 0 ; RightValues ( pv ; pvc &#8211; ( n + GetAsNumber ( IsEmpty ( GetValue ( pv ; 1 ) ) ) ) ) ; &#8220;&#8221; ) ; &lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/PATH STRING:&lt;br \/&gt;<br \/>\np = Substitute ( pv ; &#8220;\u00b6&#8221; ; s ) ;&lt;br \/&gt;<br \/>\n\/\/Prepend file reference prefix and add the separator to the left if path not empty.&lt;br \/&gt;<br \/>\np = If ( frn ; frp  ; &#8220;&#8221; ) &amp; If ( IsEmpty ( p ) or Left ( p ; 1 ) = s ; &#8220;&#8221; ; s ) &amp; p ;&lt;br \/&gt;<br \/>\n\/\/p = If ( frn ; frp  ; &#8220;&#8221; ) &amp; p ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/FILE NAME: The last value of pl. Assumed if only one value in pl.&lt;br \/&gt;<br \/>\n\/\/File value separated by the extension separator.&lt;br \/&gt;<br \/>\nfv = Substitute ( GetValue ( pl ; plc ) ; &#8220;.&#8221; ; &#8220;\u00b6&#8221; ) ;&lt;br \/&gt;<br \/>\n\/\/Get count of extension separators, adding one if last character is extension separator.&lt;br \/&gt;<br \/>\nfc = ValueCount ( fv ) + ( Right ( thePath ; 1 ) = &#8220;.&#8221; ) ;&lt;br \/&gt;<br \/>\n\/\/Filename is all values left of the rightmost &#8220;.&#8221; if it exists.&lt;br \/&gt;<br \/>\nf = Substitute ( If ( fc &gt; 1 ; LeftValues ( fv ; fc -1 ) ; fv ) ; &#8220;\u00b6&#8221; ; &#8220;.&#8221; ) ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/EXTENSION SEPARATOR: Store 0 or 1, based on presence of decimal separator at end of file name.&lt;br \/&gt;<br \/>\nes = GetAsNumber ( GetAsBoolean ( Right ( f ; 1 ) = &#8220;.&#8221; ) ) ;&lt;\/p&gt;<br \/>\n&lt;p&gt;\/\/EXTENSION: Get extension if it exists.&lt;br \/&gt;<br \/>\ne = If ( fc &gt; 1 ; GetValue ( fv ; fc ) ; &#8220;&#8221; )&lt;\/p&gt;<br \/>\n&lt;p&gt;] ;&lt;\/p&gt;<br \/>\n&lt;p&gt;Case (&lt;br \/&gt;<br \/>\nPatternCount ( &#8220;ph~#&#8221; ; r ) ; p ;  \/\/Path only.&lt;br \/&gt;<br \/>\nr = &#8220;f&#8221; ; f &amp; e ;  \/\/Full filename and extension.&lt;br \/&gt;<br \/>\nr = &#8220;n&#8221; ; Left ( f; Length ( f ) &#8211; es ) ;  \/\/Filename only (without extension).&lt;br \/&gt;<br \/>\nr = &#8220;e&#8221; ; e ;  \/\/Extension only.&lt;br \/&gt;<br \/>\nr = &#8220;?&#8221; ; &#8220;PathParser ( thePath , returnElementPHFNE: [p]ath, [h]ome or [~], [f]ull name and extension, [n]ame, [e]xtension), [+\/-#] omit # of directories )&#8221; ;&lt;br \/&gt;<br \/>\n&#8220;&#8221; )  \/\/Return nothing is user requested nothing.&lt;\/p&gt;<br \/>\n&lt;p&gt;)&lt;\/p&gt;<br \/>\n&lt;p&gt;\/* PathParser ( thePath , returnElementPHFNE )&lt;\/p&gt;<br \/>\n&lt;p&gt;PathParser returns the directory path, home directory, full filename and extension, filename, or extension from a folder and\/or file path string.&lt;\/p&gt;<br \/>\n&lt;p&gt;- Easily extract path elements from thePath.&lt;br \/&gt;<br \/>\n&#8211; Preserves FileMaker Pro file reference prefixes (file:, filemac:, and filewin:).&lt;br \/&gt;<br \/>\n&#8211; Omit directories forwards or backwards from thePath.&lt;br \/&gt;<br \/>\n&#8211; The home or ~ (tilde) element request returns the home path &#8211; path without Get ( SystemDrive ) prepended.&lt;br \/&gt;<br \/>\n&#8211; Works with the thePath string as passed &#8211; it does not format thePath, except to prepend a missing path separator when returning path or home requests.&lt;br \/&gt;<br \/>\n&#8211; Uses the leftmost path separator in thePath to delineate path elements.&lt;br \/&gt;<br \/>\n&#8211; Use with the PathMaker custom function for added path formatting control.&lt;\/p&gt;<br \/>\n&lt;p&gt;Parameters:&lt;\/p&gt;<br \/>\n&lt;p&gt;- thePath &#8211; A folder and\/or file path.&lt;br \/&gt;<br \/>\n&#8211; returnElementPHFNE &#8211; The element in thePath to return. Determined from the first letter of the passed parameter or the number:&lt;br \/&gt;<br \/>\n   &#8211; [p]ath: Returns the directory path, without the filename and extension.&lt;br \/&gt;<br \/>\n   &#8211; [h]ome or ~: Returns the home directory path, without the system drive information.&lt;br \/&gt;<br \/>\n   &#8211; [f]ullfilename: Returns the filename and extension, without the path directory.&lt;br \/&gt;<br \/>\n   &#8211; [n]ame: Returns the filename, without the path directory and without the file extension.&lt;br \/&gt;<br \/>\n   &#8211; [e]xtension: Returns the file extension, without the path directory and without filename.&lt;br \/&gt;<br \/>\n   &#8211; [+\/-#]: Returns the path after omitting the specified number (#) of directories from the left or right of thePath.&lt;br \/&gt;<br \/>\n       &#8211; A positive number omits directories forwards from the left.&lt;br \/&gt;<br \/>\n       &#8211; A negative number omits directories backwards from the right.&lt;\/p&gt;<br \/>\n&lt;p&gt;Examples:&lt;\/p&gt;<br \/>\n&lt;p&gt;- PathParser ( &#8220;file:&#8221; &amp; Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;p&#8221; ) = file:\/Macintosh HD2\/Users\/user\/Documents\/&lt;br \/&gt;<br \/>\n&#8211; PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;h&#8221; ) = \/Users\/user\/Documents\/&lt;br \/&gt;<br \/>\n&#8211; PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;fullfilename&#8221; ) = Untitled.pdf&lt;br \/&gt;<br \/>\n&#8211; PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;n&#8221; ) = Untitled&lt;br \/&gt;<br \/>\n&#8211; PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;e&#8221; ) = pdf&lt;br \/&gt;<br \/>\n&#8211; PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;2&#8221; ) = \/user\/Documents\/&lt;br \/&gt;<br \/>\n&#8211; PathParser ( Get ( DocumentsPath ) &amp; &#8220;Untitled.pdf&#8221; , &#8220;-1&#8221; ) = \/Macintosh HD2\/Users\/user\/&lt;\/p&gt;<br \/>\n&lt;p&gt;Help:&lt;\/p&gt;<br \/>\n&lt;p&gt;- Passing &#8216;?&#8217; as the leftmost text in returnElementPHFNE returns this help text: PathParser ( thePath , returnElementPHFNE: [p]ath, [h]ome or [~], [f]ull name and extension, [n]ame, [e]xtension), [+\/-#] omit # of directories ).&lt;br \/&gt;<br \/>\n*\/&lt;!&#8211;\/raw&#8211;&gt;<\/textarea><br \/>\n<input form=\"\" type=\"text\" \/> type=&#8221;button&#8221; value=&#8221;Select All&#8221; \/&gt; <input type=\"reset\" value=\"Reset\" \/><\/form>\n","protected":false},"excerpt":{"rendered":"<p>PathParser ( thePath , returnElementPHFNE ) PathParser returns the path directory, home directory, full filename and extension, filename, or extension from a folder and\/or file path string. Easily extract path elements from thePath. Preserves FileMaker Pro file reference prefixes (file:, <span class=\"excerpt-dots\">&hellip;<\/span> <a class=\"more-link\" href=\"https:\/\/lorenzcom.com\/?p=714\"><span class=\"more-msg\">Continue reading &rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,14],"tags":[],"_links":{"self":[{"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/posts\/714"}],"collection":[{"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=714"}],"version-history":[{"count":12,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/posts\/714\/revisions"}],"predecessor-version":[{"id":874,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=\/wp\/v2\/posts\/714\/revisions\/874"}],"wp:attachment":[{"href":"https:\/\/lorenzcom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lorenzcom.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}