When typing a command in Windows shell, it first looks for the program in current directory and then from PATH directories. Is it possible to somehow (eg by a environment variable) exclude current directory from the search? Something like SafeDllSearchMode but for executables instead of DLL-s?

In other words -- if I have one foo.exe in current directory and second one in another directory in PATH, how can I execute the one in PATH directory without specifying its full path?

  • As a workaround, you could do a cd \somwhere\else to a directory where there are no executables. – Aganju Jul 15 '16 at 12:27

I don't know of any way to make cmd exclude the current path. As a close approximation, though, you could use mkdir to make an empty directory in the current directory, cd into it, run your command, cd back up and remove the directory:

SET _tempdir=%date%%time%
MKDIR %_tempdir%
CD %_tempdir%
REM run commands
CD ..
RM %_tempdir%

The empty directory can be anywhere you have write access, of course; if it's somewhere other than in that same directory, though, it will probably be easier to use pushd to go into it and popd to get back out before removing it. Using an empty directory you just created seems like a better idea than cd-ing to a random directory, though--the new directory will still be first on the path, so it's better to use one you know is empty.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.