Friday, September 3, 2010

Searching in Windows for NOT those things!

I recently have began cataloguing and cleaning up my media library to enable the new media unit at home to more easily serve us the pics, music and video that I have amassed over the years...

After renaming many files with prefixes to allow for faster searching, and getting all the mp3 tags correct and orderly, I wanted to check through my directories for any files that were NOT meant to be in the respective folders. IE in my music directory, for any files that were NOT .mp3, and in my pictures, any files that were NOT .jpg, etc.

I found out you cant do this with Windows search. DOH!

I found out (on Eileen's Lounge - thanks Hans!) that a command line will do this. Yay!
For instance:

dir "C:\Documents and Settings\\My Documents\My Music" /s | find /v /i ".mp3" | C:\result.txt

will pipe a LOT of info into a text file, detailing any files not of the type .mp3 in the music directory. There is a lot of verbatim with this method to sift through though.

I think Google desktop search could probably do this with less verbatim, but I don't have this app (no major use for it) and didn't want to install it. I would rather write a nice little script...

I called it the NOT search. I have posted it for download with an .exe version on 'Eileen's Lounge (formerly Woodies Lounge)' here.

It basically prompts you for the directory to search on, then for the file types to exclude, ie NOT search on, and then offers folder recursion, ie search through all subfolders. Once complete a nice little report is presented to you in Notepad, detailing any found files and their paths. I found this very helpful indeed and was able to clean up and move any files to where they should be.

The code is below:

'// pmatz's file extension NOT search - sept 01 2010. v 1.1
'// script to search for all files with extension's NOT specified
'//---------------------------------------------------------------

Public strList

main

Sub main
strList = ""
Dim strFileExtensions()
strPath = InputBox ("Enter path to search on." & vbCr & _
"(For root drives please use driveletter" & vbCr & _
"followed by :\ )" ,"pmatz's file extension NOT search")
If IsEmpty(strPath) Then
closeMessage1
Exit Sub
End If
strGetFileExts = InputBox ("Enter filetype/s NOT to search for." & vbCr & _
"(seperate file extensions with commas and leave no spaces" & vbcr & _
"e.g. .mp3,.jpg,.gif,.tar,.iso)","pmatz's file extension NOT search")
bRecurse = (MsgBox("Recurse through all subfolders?",vbYesNo) = vbyes)
strTypes = strGetFileExts
iExtensions= 0
Do
ReDim Preserve strFileExtensions(iExtensions)
iComma = InStr(1,strGetFileExts,",")
If iComma > 0 Then
strFileExtensions(iExtensions) = Left(strGetFileExts,iComma-1)
strGetFileExts = Right(strGetFileExts,Len(strGetFileExts)-iComma)
iExtensions=iExtensions+1
End If
Loop Until iComma = 0
strFileExtensions(iExtensions) = strGetFileExts
search strPath, strFileExtensions, bRecurse
createOutput strPath,strTypes
End Sub

Sub search(searchFolder,excludeTypes,recurse)
On Error Resume Next 'this stops crash for issues with no access etc.
bFound = False
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(searchfolder) Then
closeMessage1
Exit Sub
End If
Set fFolder = fso.GetFolder(searchFolder)
Set fSubfolders = fFolder.SubFolders
Set fFiles = fFolder.Files
For Each fFile in fFiles
For iExtension = 0 To UBound(excludeTypes)
For iStop = Len(fFile.Name)To 0 Step -1
If Mid(fFile.Name,iStop,1) = "." Then
iStop = Len(fFile.Name)-iStop + 1
Exit For
End If
Next
If UCase(Right(fFile.Name,iStop)) = UCase(excludeTypes(iExtension)) Then
bFound = True
End If
Next
If Not bFound Then
strList = strList & vbNewLine & fFile.Path
Else
bFound = False
End If
Next
If recurse Then
For Each fSubfolder In fSubfolders
search fSubfolder.Path,excludeTypes,True
Next
End If
Set FSO = Nothing
Set fFolder = Nothing
Set fSubfolders = Nothing
Set fFiles = Nothing
End Sub

Sub createOutput(strPath,StrTypes)
Set oShell = CreateObject("WScript.Shell")
strOut = oShell.SpecialFolders("Desktop")
Set fso = CreateObject("Scripting.FileSystemObject")
strOut = strOut & "\pmatzFileExtensionNOTsearch_" & getStamp & ".txt"
Set f = fso.CreateTextFile(strOut)
f.WriteLine "pmatzFileExtensionNOTsearch performed on " & Now()
f.WriteLine "Searched through " & strPath & ", excluding types " & strTypes
f.WriteLine "__________________________________________________"
f.WriteLine
f.Write strList
f.Close
oShell.Run("%systemroot%\notepad.exe " & strOut)
Set oShell=Nothing
Set fso = Nothing
Set f = Nothing
End Sub

Function getStamp
strStamp = FormatDateTime(Now(),2)
strStamp = Replace(strStamp,"/","-")
strStamp = strStamp & "_" & FormatDateTime(Now(),4)
strStamp = Replace(strStamp,":","-")
getStamp = strStamp
End function

Sub closeMessage1
MsgBox "Need to enter a legitimate path!",vbinformation,"pmatz's file extension NOT search"
End Sub

No comments:

Post a Comment