Tuesday, September 15, 2009

mp3 Woes: Finding a missing mp3

So I don't like to hunt for things manually. I especially don't like things that I can't just "Fire And Forget" when it comes to searching, so I wrote the following little script to hunt my hard drive for a particular file I wanted to find.

Because a search like this can take a while, and will basically just run in the background until either cancelled via Task Manager (look for wscript.exe), or it completes a scan of EVERY folder on your hard drive, I don't usually write things like this without starting and ending messages.


Set FSO = CreateObject("Scripting.FileSystemObject")
StartingLoc = "C:\"
LookingFor = "String To Search For"

Sub Display_Fs(Folder)
For Each Subfolder in Folder.SubFolders

' Uncomment to display a popup message indicating where we are
'wscript.Echo "Getting: " & Subfolder.Path

' First, show the files in the local folder
    Set F = FSO.GetFolder(Subfolder.Path)
    Set FilesCollection = F.Files
For Each File in FilesCollection
    if File.Type = "MP3 Format Sound" then
        if InStr(file.name, LookingFor) > 0 then
            wscript.Echo file.path
        end if
    end if
Next

' Call the function so we dive into the current folder
    Display_Fs Subfolder
Next
End Sub

wscript.echo "Starting"

' Because the top folder is assumed to have no higher branch we can reference,
' we display the currenly sitting files first
Set F = FSO.GetFolder(StartingLoc)
Set FilesCollection = F.Files
For Each File in FilesCollection
if File.Type = "MP3 Format Sound" then
    wscript.Echo File.path
end if
Next

' Having displayed everything local, we begin our trip down the tree
Display_Fs FSO.GetFolder(StartingLoc)

wscript.echo "Ending"