how to set environment variable from file contents?
Just use: set /P PATH=< dat
You must note that echo %PATH% > dat
insert an additional space after %PATH% value; that space may cause problems if an additional path is later added to PATH variable. Just eliminate the extra space this way: echo %PATH%> dat
.
echo %PATH%
will fail if the PATH contains an unquoted & or ^ (this is not likely, but certainly possible)
A more reliable solution is to use:
setlocal enableDelayedExpansionecho !path!>dat
Then you can use Aacini's suggested method of reading the value back in
set /p "PATH=" <dat
This might be evil but on Windows I am using this:
for /F %%g in (dat) do set PATH=%%g
and this to write the file because I had trouble with spaces
echo>dat %PATH%