Windows whereis Equivalent
Search for file patterns from the command line in Windows
whereis is useful on Unix. Windows offers where. An example of finding where Chrome is installed from command prompt:
where /R "C:\Program Files (x86)" chrome.exe
Below, run_where opens a shell and executes this command. One limitation I noticed with where is that a wildcard is not allowed for the starting directory.
For instance, Chrome could be in "Program Files" or "Program Files (x86)".
Created: 2020-06-16
import subprocess
def run_where(starting_dir, pattern, recursive=True):
args = ['where']
if recursive:
args.append("/R")
args.extend([starting_dir, pattern])
with subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) as p:
result, _ = p.communicate()
return result.decode().strip()
Python's glob module is much simpler to use. Let's compare timings:
%timeit -r 50 run_where(r"C:\Program Files (x86)", "chrome.exe")
1.4 s ± 300 ms per loop (mean ± std. dev. of 50 runs, 1 loop each)
import glob
%timeit -r 50 glob.glob(r"C:\Program Files (x86)\**\chrome.exe", recursive=True)
1.38 s ± 303 ms per loop (mean ± std. dev. of 50 runs, 1 loop each)