How to set PATH variable using content in file?

I know I can set the PATH variable in Windows using e.g.

set path="%path%;c:\directoryPath"

or, permanently with setx using:

setx path "%path%;c:\directoryPath"

However, how can I set this using the input from a file, using command redirection? Say, I have a file mypaths.txt that contains my complete path setting:

C:\Program Files\myappA;C:\Program Files\myappB;...

I want to be able to do something like:

set path= < mypaths.txt

But this doesn't work. So how to do this correctly?

2

1 Answer

How do I set the PATH variable using the contents of a file?

You can do this using set /p ... or setx ... /f ...


set /p ... solution

set /p PATH=<mypaths.txt

Notes:

  • Do not put any spaces around =<.
  • Syntax set /p variable=[promptString]
  • The /p switch allows you to set a variable equal to a line of input entered by the user (which can be redirected to come from a file).
  • The Prompt string can be empty.

setx ... /f ... solution

setx PATH /f mypaths.txt

Notes:

  • /f FileName The file that you want to read.
  • /f supports the parsing of plain text files only, (with CR/LF line endings).

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • setx - Set environment variables permanently, SETX can be used to set Environment Variables for the machine (HKLM) or currently logged on user (HKCU).
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like