15

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

sramij
  • 4,775
  • 5
  • 33
  • 55

5 Answers5

16

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.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    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. – Russell Gallop Apr 24 '14 at 10:15
  • @RussellGallop, did you find a workaround for this limitation? – xverges May 28 '14 at 12:08
  • `set /P` can only read 1024 characters. To read more, use `for /F` as shown in SpaceMonkey answer below. – Aacini May 28 '14 at 15:41
5

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
dbenham
  • 127,446
  • 28
  • 251
  • 390
4

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%
SpacedMonkey
  • 2,725
  • 1
  • 16
  • 17
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.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
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%
efdummy
  • 684
  • 1
  • 8
  • 9