PixInsight Forum (historical)

Software Development => PCL and PJSR Development => Topic started by: bitli on 2015 February 01 07:49:20

Title: How to find the full directory of a file ?
Post by: bitli on 2015 February 01 07:49:20
I need to get the full directory (with drive and whatever) of a file (this is to initialize the initialPath of FileDialog). It seems that on windows File.extractDirectory does not return the drive part.  Is there a method to get this information which is OS independent and does not rely on parsing the path ?
Thanks
-- bitli
Title: Re: How to find the full directory of a file ?
Post by: mschuster on 2015 February 01 22:35:31
Hi Bitli,

In previous scripts I used File.extractDrive(name) + File.extractDirectory(name). Seems to work on Win and Mac, don't know about other platforms.

In new script I am working on I don't bother, as PI seems to remember and use a directory by default most of the time. However if the saved directory no longer exists PI reverts to its install directory, which I find annoying. I wish PI would search up the directory path for an existing directory, and stop there. IMO this would save navigation time.

Mike
Title: Re: How to find the full directory of a file ?
Post by: bitli on 2015 February 02 03:38:24
Thanks Mike, this will do (I will check on Windows)
This is to store the 'configurations' (rule sets) for FITSFileManager, so they are usually in a directory different from the images, so it would make sense to have a different default  I will see how it works,
-- bitli
Title: Re: How to find the full directory of a file ?
Post by: Juan Conejero on 2015 February 02 03:48:56
The correct way to implement this is as follows:

Code: [Select]
function getDirectoryWithDriveLetter( a_path )
{
   let path = File.windowsPathToUnix( a_path );
   return File.extractDrive( path ) + File.extractDirectory( path );
}

This function is portable to all supported platforms. A drive specification "<u>:" will be included if it is present in the specified path. UNIX directory separators "/" will be used on all platforms.