1

I am trying to find a way to convert a path like this: "%APPDATA%\xyz\Logs\Archive" to this: "C:\Users\abcUser\AppData\Roaming\xyz\Logs\Archive".

I am on Windows platform. I use Unicode character set. I can use C++17 if required. I can use boost libraries if required.

In my search so far, I came across the SHGetKnownFolderPath() function. And there are StackOverflow references that explain how to resolve %APPDATA% to its actual path:

How do I get the application data path in Windows using C++?

C++ CreateDirectory() not working with APPDATA

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Humayun Khan
  • 63
  • 1
  • 5
  • 1
    You could read the environment variable for APPDATA, but the best practice is to use the known folder path API to get the path. You've linked a lot of solutions but it isn't clear why none of them are working for you. – Retired Ninja Dec 10 '21 at 22:53
  • 5
    [ExpandEnvironmentStringsW](https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-expandenvironmentstringsw) – Mark Tolonen Dec 10 '21 at 22:54
  • `%APPDATA%` is usually expanded by the shell, os that by the time it reaches your app (e.g. via the command line arguments) it is already expanded. – bolov Dec 11 '21 at 00:11
  • 1
    @bolov - sometimes people like to put them in configuration files for their app, then the app needs to expand them. – davidbak Dec 11 '21 at 00:12
  • @davidbak yes. That's why I said "usually". – bolov Dec 11 '21 at 00:46
  • @MarkTolonen : Thanks for the reference. It worked. Add it as an answer to the question; I will select it as the answer. – Humayun Khan Dec 11 '21 at 22:44

1 Answers1

1

The Win32 API that expands environment variable references of the form %variable% in strings is ExpandEnvironnmentStrings.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251