Author Topic: How to find the full directory of a file ?  (Read 4768 times)

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
How to find the full directory of a file ?
« 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

Offline mschuster

  • PTeam Member
  • PixInsight Jedi
  • *****
  • Posts: 1087
Re: How to find the full directory of a file ?
« Reply #1 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

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: How to find the full directory of a file ?
« Reply #2 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

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: How to find the full directory of a file ?
« Reply #3 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.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/