tags:

views:

312

answers:

2
+4  Q: 

DOS system path

Is there a way to programmatically, through a batch file (or powershell script), put all folders in c:\Program Files into the system variable PATH? I'm dependent on the command line and really want to just start a program from the command line.

Yes, I'm jealous of Linux shells.

+6  A: 

Passing in "C:\Program Files" as a parameter into this batch file:

@echo off

FOR /D %%G IN (%1\*) DO PATH "%%G";%path%
Greg Hurlman
Can you say "DLL Hell"?
Rob Williams
Or "gaping security hole?" See my comment to Rob William's answer for more...
BQ
+5  A: 

Doing this is very likely to break your computer, in the sense of invoking DLL Hell. As you invoke each executable, the OS will look through each directory in PATH to find each DLL or even EXE referenced by that executable. It becomes highly likely that the OS will find the wrong ones as you add more directories to the PATH.

So, a best practice is to avoid increasing the PATH, and even to decrease it. Rather than implicit dependencies, make them explicit.

Instead, I recommend this approach:

  1. Create a bin directory within your user home directory
  2. Add that bin directory to your user PATH variable
  3. Create a Windows CMD script in the bin directory for each application that you want to invoke from the command line (same name as the executable that you would type)
  4. In each script, invoke SetLocal, add the application's install directory (under %ProgramFiles%) to the PATH, then invoke the executable with the arguments from the command line
  5. Remove the relevant directory from the PATH, so that this script becomes the only way to invoke the executable
Rob Williams
This has the added benefit of not creating the huge security hole that the original question's goal would create. Just create C:\Program Files\AAAVeryBadDirectory\ with some malware in it as cmd.exe/devenv.exe/etc, and you're hosed when you run any of them.
BQ
Excellent point, BQ!
Rob Williams