17

On windows/cygwin, i want to be able save the PATH variable to file on one machine and load it onto the other machine;

for storing the variable i am doing:

echo %PATH% > dat

however, not sure how to load it later.

set PATH=???????

Thanks Rami

2
  • 7
    Just use: set /P PATH=< dat Commented Jan 7, 2012 at 3:28
  • 2
    post your comment as answer and I'd vote it up. Commented Jan 8, 2012 at 15:10

5 Answers 5

17

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.

Sign up to request clarification or add additional context in comments.

3 Comments

For me this only works for the first 1024 characters of the file. Sorry, I missed the fact this was on cygwin. Maybe it works better under cygwin.
@RussellGallop, did you find a workaround for this limitation?
set /P can only read 1024 characters. To read more, use for /F as shown in SpaceMonkey answer below.
6

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 enableDelayedExpansion
echo !path!>dat

Then you can use Aacini's suggested method of reading the value back in

set /p "PATH=" <dat

Comments

5

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%

1 Comment

You must use the "delims" option if you want to manage spaces: for /F "delims=" %%g in (dat)
3

Being dependent upon Cygwin, how how about putting the command in your saved file, e.g.:

echo "export PATH=$PATH" > dat

Then sourcing the script later to set the path:

. ./dat

Note that "sourcing" the script (vs. just executing it) is required for it to modify your current environment - and not just new child environments.

1 Comment

on windows i had finally to do ' echo set PATH=%PATH% > dat.bat '; thanks
0

The following sample works even with spaces and dot in the path value:

@REM Backup PATH variable value in a file
@REM Set PATHBACKUP variable with value in the file

@echo %PATH% > pathvalue.txt
@type pathvalue.txt
@for /f "delims=" %%l in (pathvalue.txt) do (
  @set line=%%l
)
@set PATHBACKUP=%line%
@echo %PATHBACKUP%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.