In Windows cmd, how do I run an executable in the current directory (instead of one with the same name in %PATH%) without referring to the full path?

slackwing picture slackwing · May 4, 2014 · Viewed 11.3k times · Source

I'm trying to run an executable foobar from a directory, but Windows also happens to have an executable (or command) named foobar. In UNIX, I'd just write

./foobar

but Windows cmd doesn't seem to understand that. Given that I don't want to add this directory to my %PATH%, is there another way to run the current directory's foobar without typing the path explicitly?

Answer

paul picture paul · May 4, 2014

Windows always looks in the current directory first before searching the path. If you are trying to run a command from a program, try "cd"ing to the directory first like so:

copy con run_foobar.bat
cd c:\myfoobardirectory
foobar
"<CTRL> + Z" 

A special case is if you're trying to execute a file that matches the name of an internal command of cmd.exe, such as 'date', in this case, the internal 'date' command will be executed even if you have a local 'date.exe' executable file in the current directory.

You can force the executaion of the local program file by typing the full name 'date.exe' in the current directory, this will override the internal 'date' command.

Notice also that in PowerShell, the behavior is different to Cmd shell, so even if you type in PowerShell in the local directory a command like 'java' or 'java.exe', then the path command will be executed even if there is a local file with the same name. To force the execution of the local file, we would use the linux style './java' or './java.exe'.