# Export Windows 7 search results to a text file

In Windows 7, is there a way to get a list the files returned by the Advanced Query Syntax (AQS)?

Here is an example query:

owner:bob type:image


I want the list of files returned by that query in a text document. Can I use AQS from the command line?

-

# Possible solution

1. Press Ctrl+A to select all results.
2. Hold down the Shift key, and right-click the very first item in the list.
3. Choose Copy as path from the context menu.
4. Paste the results in a new text file.

For example, consider using the following query in the C:\Windows\System32 folder:

type:image


The output would be something like this:

"C:\Windows\System32\oobe\background.bmp"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\AppInstalled.gif"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\ClickDownExpanded.gif"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\ClickDownNormal.gif"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\Column.bmp"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\Documents.gif"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\Failure.gif"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\Programs.gif"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\System.gif"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\Users.gif"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\WindowsMail.bmp"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\WindowsMovieMaker.bmp"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\WindowsOutlookExpress.bmp"
"C:\Windows\System32\migwiz\PostMigRes\Web\base_images\WindowsPhotoGallery.bmp"


## Known limitations

• Unless you select the first item in the list, the resulting output won't be sorted properly.
• All items will be enclosed in quotes.
• Copied results always include the full path.

# Post-processing

Just some ways to overcome the limitations.

## Manual sort

If needed, you can manually sort the output by using the sort command in a command prompt.

### Example usage

sort "X:\Path\to\input.txt" /o "X:\Path\to\sorted.txt"


## Stripping quotes

The quotes might get in the way, but the batch script below can remove them for you. Just save it as StripQuotes.cmd (or whatever you like, just keep the .cmd extension). The script accepts two parameter: input/output files, respectively.

### Example usage

StripQuotes.cmd "X:\Path\to\myfile.txt" /o "X:\Path\to\stripped.txt"


### Batch script

@echo off
if "%~2" == "" exit /b 2
type nul>"%~2"
for /f "usebackq delims=" %%G in ("%~1") do echo %%~G>>"%~2"
exit /b


## Retrieving file names only

In case you don't care about the full path, you can use the batch script below. The usage isn't any different from the batch script above.

### Batch script

@echo off
if "%~2" == "" exit /b 2
type nul>"%~2"
for /f "usebackq delims=" %%G in ("%~1") do echo %%~nxG>>"%~2"
exit /b

-
I no longer have a windows computer :-( Can someone test this out and notify me if it works so that I can give and31415 credit for the solve? –  mrbinky3000 Apr 21 at 19:27

You could use the NirSoft free and versatile utility SysExporter:

SysExporter utility allows you to grab the data stored in standard list-views, tree-views, list boxes, combo boxes, text-boxes, and WebBrowser/HTML controls from almost any application running on your system, and export it to text, HTML or XML file.

With the file-search result in Explorer, SysExporter will probably already detect the search result, but if not it has a targeting icon that you can drag over to the results window. Choose the entries that you want and the required columns, sort by any of the columns, and you may then either copy the data to the clipboard or export it to a file in the format of your choice.

-
Although SysExporter works fine on Windows XP/Vista, it doesn't seem to support Windows 7 search results (at least as of version 1.62). –  and31415 Apr 21 at 21:13
@and31415: I have tested version 1.62 on Windows 7 before posting and it seemed to work fine with Explorer. What kind of search did you do that didn't work? –  harrymc Apr 21 at 21:30
I tried the same query I posted in my answer (type:image), but any other query exhibits the same issue. Basically the program doesn't show anything, just an empty list. –  and31415 Apr 21 at 21:45
@and31415: I think I got it - try to view the result in Details mode. –  harrymc Apr 22 at 5:46
In fact, the Details view is the only one that makes the program display the actual list. The reason is that Windows 7 uses a DirectUI control (supported since version 1.60) rather than a standard ListView. The DirectUI control uses a ListView object internally when switching to Details view, and SysExporter is able to capture its content. Previous Windows versions simply used a ListView control, and any view would do. To reduce the clutter, you can filter only ListView items in Windows XP/Vista, and DirectUI items in Windows 7. Columns can also be hidden/reordered. –  and31415 Apr 22 at 9:03