# How do I exclude specific directories from a Windows search?

I want to search for a file on drive C:. I know for sure it does not exist in a specific directory(s) (say Program Files). So to shorten my search time, I want Windows to exclude searching in those specific folders. How can I perform such a search (which searches in the C drive but can skip specific folders)?

I would prefer the answer to be a method for Windows search, but am not opposed to it being a script or other simple program.

-

## migrated from stackoverflow.comMar 6 '13 at 15:54

This question came from our site for professional and enthusiast programmers.

Are you writing some code to do this? If not, this would be off-topic. –  Chetter Hummin Mar 6 '13 at 14:50
No, I am not writing any code for this. So, this could be OT. Is there another forum I should be asking this question in ? –  happybuddha Mar 6 '13 at 14:57
Could fit the superuser forum perhaps? –  Chetter Hummin Mar 6 '13 at 14:58
On other thoughts, I wouldn't mind writing a batch file I can fire up whenever I have such a need. The need arises more often than less. So there can be some code around it. I have not started anything at all on this though. Since I want to be able to do it using the win explorer too. –  happybuddha Mar 6 '13 at 14:58
Superuser forum it is. I am fairly new here. Do you think there is a way to move this question ? Or should I just delete it (IDK if I can do that either) and ask it in the superuser forum ? –  happybuddha Mar 6 '13 at 14:59

In line with user117893's answer and using the dir command in a for loop. You can use robocopy or xcopy with the /L (list only) option to search for files.

## Robocopy

Just specify the starting directory C:\ and the target file name *File.txt

robocopy C:\ %Temp% *File.txt /S /XD Windows "Program Files" "Program Files (x86)" /XJ /L /NS /NC /NDL /NP /NJH /NJS


Output

C:\Users\Username\Desktop>robocopy C:\ %Temp% *Snapshot.txt /S /XD Windows "Program Files" "Program Files (x86)" /XJ /L /NS /NC /NDL /NP /NJH /NJS

C:\Temp\2013-02-13-1408-31_Snapshot.txt
C:\Temp\2013-02-13-1523-47_Snapshot.txt
C:\Temp\2013-02-13-1534-17_Snapshot.txt
C:\Temp\2013-02-13-1535-55_Snapshot.txt
C:\Temp\2013-02-13-1537-44_Snapshot.txt
C:\Temp\2013-02-18-1552-44_Snapshot.txt
C:\Temp\2013-02-18-1553-21_Snapshot.txt
C:\Temp\2013-02-18-1556-05_Snapshot.txt
C:\Temp\2013-02-18-1558-45_Snapshot.txt
C:\Temp\2013-02-18-1610-06_Snapshot.txt


## xcopy

setup takes a couple extra steps.

1. An exclude.txt text file must be created which contains the keywords to exclude in the search.
2. To prevent a cyclical copy error message, a temporary drive letter must be created or another drive must be used as the target directory.
3. Then specify the starting directory C:\ and the target file name *File.txt

exclude.txt

\Windows\
\Program Files\
\Program Files (x86)\


Commands

subst Z: %Temp%
xcopy C:\*File.txt Z:\ /S /L /EXCLUDE:exclude.txt
subst Z: \d


Output

C:\Users\Username\Desktop>xcopy C:\*Snapshot.txt Z:\ /S /L /EXCLUDE:exclude.txt
C:\Temp\2013-02-13-1408-31_Snapshot.txt
C:\Temp\2013-02-13-1523-47_Snapshot.txt
C:\Temp\2013-02-13-1534-17_Snapshot.txt
C:\Temp\2013-02-13-1535-55_Snapshot.txt
C:\Temp\2013-02-13-1537-44_Snapshot.txt
C:\Temp\2013-02-18-1552-44_Snapshot.txt
C:\Temp\2013-02-18-1553-21_Snapshot.txt
C:\Temp\2013-02-18-1556-05_Snapshot.txt
C:\Temp\2013-02-18-1558-45_Snapshot.txt
C:\Temp\2013-02-18-1610-06_Snapshot.txt
10 File(s)


## Notes

The target is needed only to use the commands (%Temp%), nothing is actually copied due to /L

Both command took under a couple seconds to run with the listed exclusions.

-
Thanks David and user1*3 both of yall are fantastic and both scripts are helpful. I havent run any of them but am sure they will both work fine. IDK which one should be marked as an answer, but just since David has been with the thread longer, I will mark this as answer ;) –  happybuddha Mar 6 '13 at 19:28

Alternatively, if you don't want to use a third-party software, you can indeed do this by command-line.

It's kind of a hassle to work with directory names that have a space in it such as C:\Program Files so it's not a simple script, but here it is, worked for me in Windows 7:

@echo off
for /f "tokens=*" %%A in ('dir /b /ad C:\') do (
if "C:\%%A"==%2 (
echo Not scanning %2
) else (
dir /s /b "C:\%%A\%1"
)
)


Usage: file.bat file_to_be_searched.extension "C:\directory\not\to\search"

Note the "" in the directory, you must use it for the script to work.

What this does is: For each line (all characters) of the output of the command dir /b /ad C:\, which lists the directories in C:\, do:

If C:\directory is your_directory don't scan it. Else, look for the file you specified in it.

Example (I named the scrip search.bat and placed a file named a.txt in C:\Program Files)

C:\Users\XXXX\Desktop>search.bat a.txt "C:\Windows"
C:\Program Files\a.txt
Not scanning "C:\Windows"


You can adapt the script so it doesn't search multiple directories, adding more if statements.

P.S.: I forgot to mention, this will only search the folder on root, that is, C:\. If you want to search the folders in a different location you'll have to adapt the script. Cheers.

-

Ditch Windows Search for FileSeek. It's freeware. You can exclude directories, search the contents of files, etc. Saved my hide on numerous occasions. Worth a try:

http://www.fileseek.ca/

-

If wanting a program, I would recommend AgentRansack by MythicSoft. It is free, fast, and powerful, with a small footprint.

-
Brandon and David, thanks for your answers. The tools are nice however not what am looking for. I do not have admin privileges on my machine so wont be able to install/download most of such available software. So am specifically looking for something that can help me achieve this locally. –  happybuddha Mar 6 '13 at 16:50

A way to do this inside the Windows search is by using -folder(C:\Path). If the folder name is unique (at least in regards to the area being searched) you can just use the folder name like so: -folder(Program Files)

If you want to include the subfolders inside that folder, you can use -foldername:(C:\Path)

See here for more info. I tested this on Windows 7 and it works. Unsure about other versions.

-