Jump to content

script to traverse subdirs and delete if empty


Recommended Posts

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?

Link to comment
Share on other sites

Try function _FileListToArray or Melba's extended version

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...