Where - DOS/Command Prompt Reference
[Windows Vista? or later] Outputs the location of files matching the pattern.
[PowerShell] By default, ‘where’ is registered as an alias for ‘Where-Object’, and using ‘where’ will result in different behavior from ‘where.exe’. If you want to use the Where command as documented on this page, you need to input ‘where.exe’ without omitting the ‘.exe’.
Syntax
where[.exe] [/F] [/Q] [/T] [/R <directory>] <patterns>...
Options
/F | When outputting the file name, encloses the name with double quotation marks ‘" "’. | ||||||
/Q | Does not output the file name. Useful when only the exit code is desired, such as in batch files. | ||||||
/T | When outputting the file name, the file size and the last modified date and time (both date and time) are output just before it. | ||||||
/R <directory> |
Specifies the directory to search for files. When using this option, the PATH environment variable is not used, and the search is conducted within the specified directory, including its subdirectories. When using the /R option, only the file name can be specified for <patterns>. |
||||||
<patterns>... |
Specifies the pattern for the file names to search. There are three ways to specify as follows, and you can specify the same or different patterns by separating them with spaces. Note that when searching for files, the extensions included in the PATHEXT environment variable are also used. If files matching patterns with these extensions added are found, they will be included in the results as well.
|
Details
About Where
This program is primarily used to determine which file is referred to when file names are complemented by the PATH environment variable and PATHEXT. It is similar to ‘which’ in Linux, but it differs in that Where ‘always outputs multiple file names’ and ‘allows the use of wildcards’.
If a file is found, the file name (including its size, etc., if the /T option is present) will be output to standard output, while messages for cases where no file is found or errors occur will be output to standard error.
Exit codes
Where returns the following exit codes. You can use If Errorlevel to write branching logic based on the results.
Exit code | Meaning |
---|---|
0 | File(s) were found |
1 | No file was found |
2 | An error occurred (e.g. if the options are incorrect) |
* If multiple search patterns are specified, a message will be output if no file is found with any of the patterns. However, if a file is found with any of the other patterns, the exit code will be 0.
Samples
Sample 1
where /T /R D:\MyDocs yume_memo.txt
Searches for files matching the filename ‘yume_memo.txt’ in the ‘D:\MyDocs’ directory and its subdirectories. Outputs the results to the screen, including information such as the last modified date and time.
Sample 2
where P:\Bin:conv*.exe Q:\Bin:conv*.exe 2> NUL
Searches for files matching the filename pattern ‘conv*.exe’ in the ‘P:\Bin’ and ‘Q:\Bin’ directories. The message printed when no files are found is hidden through redirection.
Sample 3
set "MYDIRS=D:\Scripts;E:\All Data\Commands" where $MYDIRS:post_exec
Searches for files matching the filename ‘post_exec’ in the ‘D:\Scripts’ directory and the ‘E:\All Data\Commands’ directory. To include multiple directories in the search, they are temporarily specified in environment variables.
Sample 4 (Batch file)
setlocal enabledelayedexpansion set FIND_EXE= set PATHEXT= for /f "delims=" %%F in ('where find.exe 2^> NUL') do ( if "!FIND_EXE!"=="" set "FIND_EXE=%%~fF" )
[Extensions] Searches for ‘find.exe’ in the current directory and the PATH environment variable. This script sets the full path of the first found file to the environment variable ‘FIND_EXE’. In the third line, the script negates PATHEXT to avoid matching files like ‘find.exe.exe’. In the fifth line, it uses an If statement to ensure that only the first matched filename sets the ‘FIND_EXE’ variable in case there are multiple matches. In this script, ‘( )’ is used to employ environment variables, and ‘!’ is used for this purpose. To enable this, the first line executes Setlocal.