Jump to content

Recommended Posts

Posted (edited)

I want to remove the folder from the C drive. It was left behind by the old application. Folder name is 'videocodec'. It is everywhere on the drive including in the user profiles that logged on these 23 windows 7 desktop computers. I want to run a script to search and delete those folders and its contents for good. How to get this done? Can you help? Thanks! 

Edited by aiuse
Posted (edited)

That is probably a bit daunting so I altered the script to return a list of all folders recursively

;#RequireAdmin
#include <Array.au3> ;Only for _ArrayDisplay()

$sPath = "c:\"

$Results = _FolderFind($sPath)
If @error = 3 Then
    MsgBox(48, "Attention", "No folders found.")
Else
    _ArrayDisplay($Results)
EndIf

;On Failure set @error as following:
;   1 - $sPath is not a dir or it not exists (in this case returned -1).
;   2 - $sPath is empty dir.
;   3 - No folders found in $sPath dir.
;   4 - Recurse depth exceeded.
;
;On Success return array with all child folders.

Func _FolderFind($sPath, $iRecurseDepth = -1)
    Local $aResults[100], $iUbound = 0
    Local $sFindNextFile, $sCurrentPath, $aRecurse
    
    If Not StringInStr(FileGetAttrib($sPath), "D") Then Return SetError(1, 0, -1)
    If $iRecurseDepth = 0 Then Return SetError(4, 0, -1);Check if the recurse depth has been exceeded

    $iUbound = UBound($aResults)

    $sPath = StringRegExpReplace($sPath, '\\+ *$', '\') ;Replace double back slashes with single \

    Local $hSearch = FileFindFirstFile($sPath & "\*")
    If @error = 1 Then Return SetError(2, 0, 0)    ;Empty Folder
    If $hSearch = -1 Then Return SetError(3, 0, 0) ;Nothing Found

    While True
        $sFindNextFile = FileFindNextFile($hSearch, 1)
        If @error = 1 Then ExitLoop
        If Not StringInStr(@extended, "D") Then ContinueLoop ; Not a directory try again
        $sCurrentPath = $sPath & "\" & $sFindNextFile & "\"
        ;ConsoleWrite($sCurrentPath & @CRLF)

        $aResults[0] += 1
        If $aResults[0] >= $iUbound Then
            $iUbound *= 2 ;Double the size of the array so we don't have to redim again for awhile
            ReDim $aResults[$iUbound]
        EndIf
        $aResults[$aResults[0]] = $sCurrentPath

        ;Recurse into sub directories
        $aRecurse = _FolderFind($sCurrentPath, $iRecurseDepth - 1)
        If Not @error And IsArray($aRecurse) And $aRecurse[0] > 0 Then
            If $aResults[0] + $aRecurse[0] >= $iUbound Then
                $iUbound += $aResults[0] + $aRecurse[0] + 1
                ReDim $aResults[$iUbound]
            EndIf
            ;Copy the data from the Recursed array
            For $i = 1 To $aRecurse[0]
                $aResults[0] += 1
                $aResults[$aResults[0]] = $aRecurse[$i]
            Next
        EndIf

    WEnd

    FileClose($hSearch) ;Close the search handle from FileFindFirstFile

    If $iUbound = 0 Then Return SetError(3, 0, 0) ;No directories found!
    Return $aResults
EndFunc   ;==>_FolderFind

 

A few notes:

You'll probably need to uncomment #requireAdmin to get satisfactory results

 

There is a limit to just how big arrays in AutoIt can be (16,000,000 elements or so IIRC)

    so you might need to filter the results if your machines have more directories than that

 

You can parse the directories to find which ones to delete but I will warn you

    that before you do the actual delete you should pop up a screen and verify what is going to be deleted

 

I added $iRecurseDepth so you can limit how deep in the directory structure you wish to go

Edited by Bilgus

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
×
×
  • Create New...