Poor Man’s File/Directory Name Indexer Using Windows PowerShell

The following is a script that I have set up on my dev PCs to run nightly via a scheduled task:

## CatalogFileSystem.ps1
param([
string[]]$paths)
Set-PSDebug -Strict function Main { foreach ($path in $Paths) { if (!(Test-Path $path -PathType Container)) { Write-Error "'$path' doesn't exist or isn't a directory" exit 1 } $filelist = Join-Path $path filelist.txt $dirlist = Join-Path $path dirlist.txt Remove-Item $filelist -ErrorAction SilentlyContinue Remove-Item $dirlist -ErrorAction SilentlyContinue CatalogFolder $path } } function CatalogFolder($path) { Get-ChildItem $path -ErrorAction SilentlyContinue | sort FullName | foreach { if ($_.PSIsContainer) { $_.FullName >> $dirlist CatalogFolder $_.FullName } else { $_.FullName >> $filelist } } } . Main
## End of CatalogFileSystem.ps1

My scheduled task is configured like so (and runs everyday at 4 am):

Program: C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe
Arguments: -Command C:\Bin\CatalogFileSystem C:, D:

I also set this task to run with highest privileges on Vista so it can catalog more of the nooks and crannies of my filesystem.  This script creates two files setting at the root of each path supplied.  In my case:

C:\dirlist.txt
C:\filelist.txt
D:\dirlist.txt
D:\filelist.txt

Searching for files on my filesystem is now very easy:

PS> Select-String afxwin\.h \filelist.txt | select Line
Line
----
C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxwin.h
C:\Program Files\Microsoft Visual Studio 9.0\VC\ce\atlmfc\include\afxwin.h

In fact I have created a shortcut function for the above I call Find-File (alias ff):

Set-Alias ff Find-File
function
Find-File($pattern) { $filelist = Join-Path $pwd.drive.root filelist.txt if (!(Test-Path $filelist -PathType Leaf)) { Write-Error "$filelist doesn't exist or isn't a file" } Select-String $pattern $filelist | foreach {$_.Line} }

And then this sort of filename search gets even easier:

PS> ff afxwin\.h
C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxwin.h
C:\Program Files\Microsoft Visual Studio 9.0\VC\ce\atlmfc\include\afxwin.h

Now you might be wondering why I don’t just use the search built into Vista.  Well first off, Vista’s search doesn’t index your whole hard drive or any volumes other than the contents of C: by default.  While it can be set up to index more locations I worry about the performance hit because the second issue is that the Vista search indexer runs in the background all the time.  The CatalogFileSystem script runs just once a day (or however often you schedule it) when you’re typically not using the computer.  Yeah, it could be as much as 24 hours out of date but the vast majority of the time I’m searching for files that have been on my system for a while: C++ header files, SDK header files, C runtime source files, windows DLLs, etc.

I’m sure some folks are going to think this is goofy but honestly I do search the filelist.txt file quite often (not so much the dirlist.txt file).  Do you have any similar convenience scripts like this?  If you do, please add a comment and let me know.

Advertisements
This entry was posted in PowerShell. Bookmark the permalink.

One Response to Poor Man’s File/Directory Name Indexer Using Windows PowerShell

  1. Tommy says:

    Thanks for this idea. I\’ve set up the scheduled task to see how it works.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s