This language bar is your friend. Select your favorite languages!

Idiom #107 Get folder containing current program

Assign to string dir the path of the folder containing the currently running executable.
(This is not necessarily the working directory, though.)

import System.Environment (getExecutablePath)
import System.FilePath.Windows
dir <- takeDirectory `fmap` getExecutablePath
import System.Environment (getExecutablePath)
import System.FilePath.Windows
import System.IO.Unsafe (unsafePerformIO)
dir = unsafePerformIO (takeDirectory `fmap` getExecutablePath)
import std.path;
void main(string[] args) {
    string dir = args[0].dirName;
}
1
import "os"
import "path/filepath"
programPath := os.Args[0]
absolutePath, err := filepath.Abs(programPath)
if err != nil {
	return err
}
dir := filepath.Dir(absolutePath)
$dir = realpath(dirname(__FILE__));
dir := extractfilepath(paramstr(0));
dir = __dir__

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms