Sign in to follow this  
Followers 0
dwaynek

script to traverse subdirs and delete if empty

6 posts in this topic

i'd like a script that traverses a user specified directory and checks all subdirs within it. if the subdir is empty or contains a specific file or files, that subdirectory will be deleted.

any ideas how i could implement something like that?

Share this post


Link to post
Share on other sites



Try function _FileListToArray or Melba's extended version


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#include<array.au3>

#Include <Date.au3>



main()

Func main()
    Local $sParam
    Switch $cmdline[0]
        Case 0
            _ScanDrives()
        Case 1
            $sParam=$cmdline[1]
            If StringRight($sParam,1)="" Then $sParam=StringTrimRight($sParam,1)
            If FileExists ($sParam) Then
                _ScanRoot($sParam)
            Else
                _help()
                EndIf
        Case Else
            _help()
        EndSwitch


EndFunc   ;==>main
Func _help()
    ConsoleWrite("To scan all fixed and removable drives, run the script with no parameters, to scan a folder specifiy it, addeding double quotes if it contains blanks.")
EndFunc

Func _scanDrives()
        $aDrives = _getdrive()
    For $x = 1 To $aDrives[0]
        _ScanRoot($aDrives[$x])
    Next
EndFunc

Func _ScanRoot($sDrive)
    Local $sDirs

    $sDelim = "|"
    Local $sPaths = $sDrive & $sDelim
    _log("root path",$spaths)
    While 1
        $iPos = StringInStr($sPaths, $sDelim)
;~         _log("sting position",$iPos)
        If $iPos = 0 Then
            _log("Done Scanning", $sDrive)
            Return
        EndIf
        $sPath = StringLeft($sPaths, $iPos - 1)
        $sPaths = StringTrimLeft($sPaths, $iPos)
;~         _log("Scanning path",$sPath)
        _ScanDir($sPath, $sPaths, $sDelim)
    WEnd


EndFunc   ;==>_ScanDrive



Func _ScanDir($sPath,  ByRef $sPaths,$sDelim="|")
    Local $iFileCount = 0
    $hFile = FileFindFirstFile($sPath & "*.*")
    While 1
        $sFile = $sPath & "" & FileFindNextFile($hFile)
        If @error Then ExitLoop
;~         _log("found file",$sFile)
        $iFileCount += 1
        If StringInStr(FileGetAttrib($sFile), "D") Then
            $sPaths &= $sFile & $sDelim
;~             _log("appending path",$spaths)
        EndIf
    WEnd
    If $iFileCount = 0 And FileExists($sPath) Then
;~         _log("appears to be empty:", $sPath)
            If DirRemove($spath)=0 Then _log("could not delete",$spath)

    EndIf
EndFunc   ;==>_ScanDir

Func _getdrive()
    Local $aDrives = DriveGetDrive("fixed")
    Local $aDriveRemovable = DriveGetDrive("removable")
    If UBound($aDriveRemovable) > 3 Then
        _ArrayConcatenate($aDrives, $aDriveRemovable, 2)
        $aDrives[0] = UBound($aDrives) - 1
    EndIf

    Return ($aDrives)

EndFunc   ;==>_getdrive

Func _log($sComment, $sValue = "")
    ConsoleWrite(@LF & _Now() & " -- ")
    If $sValue <> "" Then
        $sComment &= ": "& $sValue
    EndIf
    ConsoleWrite($sComment & @LF)
EndFunc   ;==>_log

Edited by DicatoroftheUSA
1 person likes this

Share this post


Link to post
Share on other sites

holy @#$@#$, DicatoroftheUSA!! i wasn't expecting full code!! kudos!!!!!!!!

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  
Followers 0