Skip to content

Instantly share code, notes, and snippets.

@voidbar
Last active August 25, 2022 20:31
Show Gist options
  • Save voidbar/1a9ff778703c175b241e2aa488dd144b to your computer and use it in GitHub Desktop.
Save voidbar/1a9ff778703c175b241e2aa488dd144b to your computer and use it in GitHub Desktop.
Using std::filesystem trick to convert windows short path into long path
#include <filesystem>
#include <iostream>
#include <format> // C++20 Required
namespace fs = std::filesystem;
std::string to_long_path(const std::string& short_path) {
return fs::canonical(short_path).string();
}
int main() {
std::string short_path{"C:\\PROGRA~1"};
auto long_path = to_long_path(short_path);
std::cout << std::format(R"(Short Path: "{}". Long Path Using std::fs: "{}")", short_path, long_path);
// Output: Short Path: "C:\PROGRA~1". Long Path Using std::fs: "C:\Program Files"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment