Expand/Shrink

get_proper_dir

Definition: string path = get_proper_dir(string filepath, bool remove_slash=false)
Description: Return the full directory path of the supplied file with the name removed.
pwa/p2js: Implemented by hand in p2js.js - see technicalia below.
Comments: Result includes a trailing path separator, unless the optional parameter remove_slash is 1.
Note that when a directory is specified the presence or otherwise of a separator at the end of the input string directly affects the output, as shown in the example below.

Both forwardslash ('/') and backslash ('\\') are supported on all platforms, and converted appropriately in the output.

Also as shown below, get_proper_dir(".") may sometimes be a slightly better alternative to current_dir().

The result should not be taken as confirmation of the actual existence of a directory.
Examples:
constant fullpath = `C:\Program Files (x86)\Phix\p.exw`
printf(1,"`%s`\n",get_proper_dir(fullpath))     -- prints `C:\Program Files (x86)\Phix\`
printf(1,"`%s`\n",get_proper_dir("p.exw"))      -- prints `C:\Program Files (x86)\Phix\`        (`` on p2js)
printf(1,"`%s`\n",get_proper_dir("demo"))       -- prints `C:\Program Files (x86)\Phix\`        (   ""     )
printf(1,"`%s`\n",get_proper_dir(`demo\`))      -- prints `C:\Program Files (x86)\Phix\demo\`   (demo\ p2js)
printf(1,"`%s`\n",get_proper_dir("."))          -- prints `C:\Program Files (x86)\Phix\`        (`` on p2js)
printf(1,"`%s`\n",get_proper_dir(".",true))     -- prints `C:\Program Files (x86)\Phix`         (   ""     )
printf(1,"`%s`\n",current_dir())                -- prints `C:\Program Files (x86)\Phix`         ( not p2js )

The use of a named parameter when setting remove_slash is recommended, to make the intent clear and the code easier to read. As previously noted above only compatible/consistent under p2js when a full path is provided.
Implementation: See builtins\pgetpath.e (an autoinclude) for details of the actual implementation.
See Also: get_proper_path, current_dir
Expand/Shrink