2

How to: filter files by name and contents in Windows 7?

I need to find files named bob.xml recursively and leave (in the search results) only those that contain the phrase <gold>100</gold>

Can it be done under Windows 7 w/o using any external software (that I have to download and install)?

Best solution would use only Windows Explorer's search queries (the input in the upper-right corner)

Breakthrough
  • 34,227
  • 10
  • 105
  • 149
Queequeg
  • 123
  • 4
  • This would be easy to do from the command line: `findstr /s "100" bob.xml` or using a better search utility such as [AstroGrep](http://superuser.com/a/512391/138343). It *might* be possible via Windows Search if the XMLs [are indexed](http://superuser.com/questions/483492/possible-to-get-windows-8-search-to-look-into-files-other-than-txt-and-xml), but I'm not sure. Windows Search [sucks](http://superuser.com/questions/514331/how-to-search-only-the-filename-for-a-substring-in-windows) for [sure](http://superuser.com/questions/538670/how-to-search-in-multiple-folders-in-windows-7)! – Karan Mar 06 '13 at 05:08
  • Out of interest, why the exclusion of external tools? – snowdude Mar 13 '13 at 13:05

1 Answers1

4

I'm not sure what your restrictions are in terms of 'external software' but you could use PowerShell, which is included on Win7. To open it you can just type 'PowerShell' in the Start search bar, or find it (I think) under Start > All Programs > Accessories > Windows PowerShell.

This set of commands will recursively find files named 'bob.xml' and will only return the names of those which contain the text '100'.

dir -filter bob.xml -recurse | select-string -pattern '<gold>100</gold>' -list | select-object -unique 'Path'

If you want to output those results to a text file so you can do something with it, this will do the trick.

dir -filter bob.xml -recurse | select-string -pattern '<gold>100</gold>' -list | select-object -unique 'Path' | out-file c:\temp\bob_search.txt

fussmonkey
  • 166
  • 1
  • 5
  • PowerShell is just fine :) This works wonderfully! **Can it be done using only Windows Explorer?** – Queequeg Mar 05 '13 at 14:31
  • I haven't been able to find a way to get Windows Explorer to do that. From what I've read (see [link](http://answers.microsoft.com/en-us/windows/forum/windows_7-files/how-do-i-add-search-filters/e84f8397-aa0b-44f3-b8b2-a094a84e7aa0)) the filters available depend on the type of file(s) showing in the results. Based on the answers in the link above, it doesn't look like file content is an option. That said, there could be a way, I'm just not familiar with it. As you might guess, I tend to use PS or other external tools when I need stuff like this. Good luck though! – fussmonkey Mar 05 '13 at 14:44