Jump to content
Sign in to follow this  
Squirrely1

_FindFilesByType("bmp", @MyDocumentsDir)

Recommended Posts

Squirrely1

I had use for a function that would help me clean up my system of junk files.

This function returns an array of the full filepaths of all files under the passed folderpath, of the passed file type. Pass it just the file extension, and it returns all files of that type on the machine; or add a folderpath, and it returns an array of all files of type "bmp" or "tmp" or whatever, under that folder.

#include-once


#region test...
;#cs
#include <File.au3>
Opt("MustDeclareVars", 1)
Global $resultsFile
Global $myPathArray
;
$resultsFile = @MyDocumentsDir & "\myFind results01.txt"
FileDelete($resultsFile)
$myPathArray = _FindFilesByType("john")
_FileWriteFromArray($resultsFile, $myPathArray)
_FileWriteToLine($resultsFile, 1, "", 1)
$resultsFile = '"' & $resultsFile & '"'
Run(@ComSpec & " /c " & $resultsFile, "", @SW_HIDE)
;
$resultsFile = @MyDocumentsDir & "\myFind results02.txt"
FileDelete($resultsFile)
$myPathArray = _FindFilesByType("bmp", @MyDocumentsDir)
_FileWriteFromArray($resultsFile, $myPathArray)
_FileWriteToLine($resultsFile, 1, "", 1)
$resultsFile = '"' & $resultsFile & '"'
Run(@ComSpec & " /c " & $resultsFile, "", @SW_HIDE)
;
;
$resultsFile = @MyDocumentsDir & "\myOtherFind results03.txt"
FileDelete($resultsFile)
$myPathArray = _FindFilesByType("bas")
_FileWriteFromArray($resultsFile, $myPathArray)
_FileWriteToLine($resultsFile, 1, "", 1)
$resultsFile = '"' & $resultsFile & '"'
Run(@ComSpec & " /c " & $resultsFile, "", @SW_HIDE)
;
Exit
;#ce
#endregion  test...


;=====================================================================================================
; Name:     _FindFilesByType
; Purpose:   Quickly finds all the files on a system, of a given file extension; even searches the
;               CD drives and the floppy; optionally filters the results, limit to those in 
;               a given folder.
;
; Syntax:   _FindFilesByType ( strFileExtension [,  strFolderpath ] )
; Note(s):  
;           Parameters are not case sensitive and filepath results are in all lower case.
;           strFolderpath cannot have a trailing backslash and must be a fully qualified filepath -
;               (it cannot be a 'GetShortName()' filepath)
; Usage:
;           _FindFilesByType("TMP")
;           _FindFilesByType("bmp", $myFolderpath)
;
; Return(s):  An one-dimensional array whose first element has the number of results;
;               all other elements each contain a fully qualified filepath of file search results,
;               (in all lower-case characters.)
;
; Requires: An AutoIt version with COM support; WMI support on the machine.
; Author(s):
;           Basic vbs is from the book "Windows Server Cookbook" by Robbie Allen
;               Publisher: O'Reilly Media; ISBN: 0-596-00633-0
;               Book web site: http://rallenhome.com/books/winsckbk/code.html
;           Script conversion from vbs to an AutoIt function - squirrely1
;=====================================================================================================
Func _FindFilesByType($sFileExt, $sFolderpath = "My Computer")
    Local Const $strComputer = "."
    Local $i
    Local $objWMI
    Local $colFiles
    Local $objFile
    Local $pLength
    Local $retArr
    Local $limit
    Local $x
    Dim $FpathArr[2000]
    $objWMI = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
    $colFiles = $objWMI.ExecQuery("Select * from Cim_DataFile where extension = '" & $sFileExt & "'")
    $i = 1
    For $objFile In $colFiles
        $FpathArr[$i] = $objFile.Name
        $i += 1
    Next
    $objWMI = ""
    $colFiles = ""
    $objFile = ""
    $FpathArr[0] = $i - 1
    ReDim $FpathArr[$i]
    ; saves time ...
    If $FpathArr[0] = 1 Then
        Return $FpathArr
    EndIf
    ; return if searching the whole computer ...
    If $sFolderpath = "My Computer"  Then
        Return $FpathArr
    EndIf
    ;       filter results elsewise ...
    Dim $retArr[2000]
    $sFolderpath &= "\"
    $pLength = StringLen($sFolderpath)
    $i = 1
    $x = 1
    $limit = UBound($FpathArr) - 1
    Do
        If StringLeft($FpathArr[$i], $pLength) = $sFolderpath Then
            $retArr[$x] = $FpathArr[$i]
            $x += 1
        EndIf
        $i += 1
    Until $i > $limit
    $retArr[0] = $x - 1
    ReDim $retArr[$x]
    Return $retArr
EndFunc   ;==>_FindFilesByType

This include file is ready to test. But get rid of the two semi-colons - at the beginning of line 5 and line 37 - after you test the script, if you use the version in the 'code' box above. :)

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
weaponx

Not trying to sound negative but there are a bunch of these already.

randallc - http://www.autoitscript.com/forum/index.ph...filelisttoarray

myself - http://www.autoitscript.com/forum/index.ph...filelisttoarray

Smoke_N - http://www.autoitscript.com/forum/index.ph...t=0&start=0

Maybe I will get around to benchmarking this version so I can add it to my list.

Edited by weaponx

Share this post


Link to post
Share on other sites
Squirrely1

Not trying to sound negative but there are a bunch of these already.

randallc - http://www.autoitscript.com/forum/index.ph...filelisttoarray

myself - http://www.autoitscript.com/forum/index.ph...filelisttoarray

Smoke_N - http://www.autoitscript.com/forum/index.ph...t=0&start=0

Maybe I will get around to benchmarking this version so I can add it to my list.

Thanks - I would have been a long time trying to find these functions, weaponx. I should have read the latest version of the AutoIt help file.

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×