Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only the directory portion of the current executable's path?

I want to read files from a config folder at the directory where the executable is located. I do that using the following functions:

use std::env;

// add part of path to te path gotten from fn get_exe_path();
fn get_file_path(path_to_file: &str) -> PathBuf {
    let final_path = match get_exe_path() {
        Ok(mut path) => {
            path.push(path_to_file);
            path
        }
        Err(err) => panic!("Path does not exists"),
    };
    final_path
}

// Get path to current executable
fn get_exe_path() -> Result<PathBuf, io::Error> {
    //std::env::current_exe()
    env::current_exe()
}

In my case, get_exe_path() will return C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe.

With get_file_path("Config\test.txt"), I want to append Config\test.txt To the above path. Then I get the following path to the file: C:\Users\User\Documents\Rust\Hangman\target\debug\Hangman.exe\Config\test.txt

The problem is that std::env::current_exe() will get the file name of the executable also and I do not need that. I only need the directory where it is located.

Question

The following the following function call should return C:\Users\User\Documents\Rust\Hangman\target\debug\Config\test.txt:

let path = get_file_path("Config\\test.txt");

How can I get the path from the current directory without the executable name like above example? Are there any other ways to do this than using std::env::current_exe()

like image 792
Timon Post Avatar asked Oct 14 '17 21:10

Timon Post


People also ask

How do I get the full path of the current file directory?

The pwd command displays the full, absolute path of the current, or working, directory.

How do I get the current directory in VB net?

You can use WinDir variable to get the current directory. The Environment. GetEnvironmentVariable method can be used for that. Here is the code snippet that gets the current directory using VB.NET.

How do I find the path to an executable?

Find the executable's path and its directory by using System.Reflection.Assembly.GetExecutingAssembly ().Location. This will return the path to the executable.

How to get current folder path in PowerShell?

Using pwd and Get-Location cmdlet in PowerShell, you can get current folder path, get script directory path, print current directory. Set-Location cmdlet used in article, to set working directory.

How to get the path of the current directory in Python?

I n this tutorial, we are going to see how to get the path of the current directory in Python. The current directory is nothing else than the folder from where your script is executed. To perform this task, we will use the “os” module in Python. It has a method called getcwd () which will return the current directory.

How to get the current directory path of a backup folder?

In the above PowerShell script, get current directory full path C:\Backup\01-Sept\ and assign it to variable $curDir. Second command returns parent directory path as C:\Backup using Split-Path cmdlet.


1 Answers

PathBuf::pop is the mirror of PathBuf::push:

Truncates self to self.parent.

Returns false and does nothing if self.file_name is None. Otherwise, returns true.

In your case:

use std::env;
use std::io;
use std::path::PathBuf;

fn inner_main() -> io::Result<PathBuf> {
    let mut dir = env::current_exe()?;
    dir.pop();
    dir.push("Config");
    dir.push("test.txt");
    Ok(dir)
}

fn main() {
    let path = inner_main().expect("Couldn't");
    println!("{}", path.display());
}

There's also the possibility of using Path::parent:

Returns the Path without its final component, if there is one.

Returns None if the path terminates in a root or prefix.

In your case:

fn inner_main() -> io::Result<PathBuf> {
    let exe = env::current_exe()?;
    let dir = exe.parent().expect("Executable must be in some directory");
    let mut dir = dir.join("Config");
    dir.push("test.txt");
    Ok(dir)
}

See also:

  • How to get the name of current program without the directory part?
like image 104
Shepmaster Avatar answered Oct 18 '22 09:10

Shepmaster