0

Similar question to How to keep from duplicating path variable in csh and How to keep from duplicating path variable in ksh. But this time using Windows command line (cmd.exe).

PATH=C:\path\to\bin;%PATH%

How do I remove duplicates from PATH? It would like to do this in an automated way: as part of our build process a specific path is added to the environment variable PATH; when done 20+ times that path is present 20+ times. I want to avoid that.

Community
  • 1
  • 1
parvus
  • 5,706
  • 6
  • 36
  • 62
  • Copy all the values of the Environment variable in a Textpad or Notepad++ and sort it. Then remove the duplicates. After that replace the existing Environment Variable values with the non redundant values. – kvivek Jun 23 '14 at 06:54
  • It would be nice to be able to do this automatically, i.e. in a makefile, or as part of a batch file. – parvus Jun 23 '14 at 06:58
  • Similar question on SU: https://superuser.com/questions/1223976/how-do-i-keep-each-path-entry-only-once/1541042#1541042 (How to remove PATH duplicates) – Niko Föhr Apr 11 '20 at 18:46

1 Answers1

0

My solution is similar to https://stackoverflow.com/a/586748/911550:

echo %PATH% | tr ; \n | awk "!($0 in a) { a[$0]; print }" | paste -sd; - > TEMPORARY.FILE
set /P PATH=< TEMPORARY.FILE
del TEMPORARY.FILE

tr, awk and paste can be downloaded from http://gnuwin32.sourceforge.net. (Actually, the first I do on any new Windows workstation is using the Automated gnuwin32 download tool)

Community
  • 1
  • 1
parvus
  • 5,706
  • 6
  • 36
  • 62