Expand File Names That Have Environment Variables in Their Path

ExpandEnvironmentStrings( ... ) on a x64 bit windows is expanding %programfiles% to x86 path

When a 32-bit application expands the environment variable reference %ProgramFiles%, it always gets the folder path which contains by default the program folders of 32-bit applications. But this environment variable reference expands to folder path which contains by default the program folders of 64-bit applications when expanded from a 64-bit process (such as Windows Explorer).

The environment variable reference %ProgramW6432% must be used to get the program files folder path for 64-bit applications by a 32-bit process.

This is documented by Microsoft at WOW64 Implementation Details.

It is possible to check this by yourself by opening a 64-bit and a 32-bit command prompt – type %SystemRoot%\SysWoW64\cmd.exe to open a 32-bit command prompt on a 64-bit system. Then execute in both command prompt windows the command set for a complete list of environment variables with their current values or just set prog for a list of environment variables starting case-insensitive with string prog in name and their values.

See also Wikipedia article with the predefined Windows Environment Variables.

How to retrieve an environmental variable from a path that is present in file,

If I understand your text file contains \$SOMENAME/ as part of the line, and you do NOT know what SOMENAME will be beforehand, you can still extract what is between \$...../ and send that to getenv to obtain the contents of the environment variable SOMENAME -- even though you don't know that name to start with.

You can do this by reading each line and using size_t begin = line.find("$"); and then locating the end with end = line.find("/", begin); You can then use .substr() to extract the environment variable name from line before calling getenv(). For example you could do something similar to:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

int main (int argc, char **argv) {

std::string line, envpath;

if (argc < 2) {
std::cerr << "error: filename required as 1st argument.\n";
return 1;
}

std::ifstream f (argv[1]);

while (getline (f, line)) { /* read each line */
size_t begin = line.find("$"); /* search for '$' */
if (begin != std::string::npos) { /* contains '$'? */
size_t end = line.find("/", begin); /* find following '/' */
/* extract environment variable from line */
std::string envvar = line.substr (begin + 1, end - 2);
/* get from environment (NULL returned if it does not exist) */
if (const char *envtmp = std::getenv(envvar.c_str()))
envpath = envtmp;
else {
std::cerr << "error: $" << envvar << " not in environment.\n";
continue;
}
/* replace variable name with contents in line */
line.replace (begin - 1, end - begin + 1, envpath);
break;
}
}

if (envpath.length())
std::cout << "absolute path: " << line << '\n';
else
std::cerr << "error: environment variable not found.\n";
}

(note: if you have multiple variable names in the file, e.g. \$...../ you can adjust the logic to fit your needs.)

Example Input File

Using your input line contained in a file with a few other lines around it:

$ cat dat/envinpath.txt
some lines of text
\$ALLO/abc/bcd/cde.txt
more lines of text

Example Use/Output

Exporting ALLO as my home directory path and then running the program successfully extracts ALLO from the line and then uses getenv() to get the environment variable contents and then .replace() to substitute the contents for the variable name before outputting the results, e.g.

$ export ALLO=$HOME

$ ./bin/genvfrompath dat/envinpath.txt
absolute path: /home/david/abc/bcd/cde.txt

Look things over and let me know if this is what your were intending. If not, let me know and I'm happy to help further.

How to Traverse directories with vim file name completion in insert mode with path environment variables?

This works by default in vim..

How to expand path containing environment variable in C++

I ended up writing a regex and then expanded the matches

new File() with environment variables in Java

I know this Question is a little bit old, but no Answer yet.

In another Question about environment variables there is a simliar Problem and they have a sample code for regex:

System Variable Regex

This code replaces all system vars that are in the format ${ENV_VAR}.

Hope that helps,

Kalasch

Evaluating environment variables in github actions workflow

Since these parameters are passed through to the s3-upload-action, the behaviour depends on whether the action expands shell parameters or not, but the input value will be literally

${GITHUB_WORKSPACE}/build/${FILE_NAME}.zip

i.e., unexpanded.

You can use expressions to work around this:

      file-path: ${{ github.workspace }}/build/${{ env.FILE_NAME }}.zip

You maybe have assumed that environment variables expand everywhere as they do when evaluated by a shell, such as in a run: step, but they don't.



Related Topics



Leave a reply



Submit