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.txtBut this doesn't work. So how to do this correctly?
21 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.txtNotes:
- Do not put any spaces around
=<. - Syntax
set /p variable=[promptString] - The
/pswitch 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.txtNotes:
/f FileNameThe file that you want to read./fsupports 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).