Jump to content

Delete Files from Multiple Folders


deef99
 Share

Recommended Posts

I am writing an File Remover that I can use to delete files X days old from several folders. Got the basic delete code working, but I can't figure out how to loop through the other folders now. I think I need to set this as a function, but I am confused how to set that up. Can anyone post some insight please - greatly appreciate it!

;FIRST FOLDER

$sourceFolder = @ScriptDir & '\'

;Gather files into an array

$fileList = _FileListToArray($sourceFolder, "*", 1)

While 9=9

Dim $found[1]

;Loop through array

For $X = 1 To $fileList[0]

;Retrieve creation time of file

$Date = FileGetTime($sourceFolder & "\" & $fileList[$X], 1, 0)

;Format date for use with Date UDF

$fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0], $Date[1], $Date[2], $Date[3], $Date[4], $Date[5])

;Calculate age, remove files older than seven days

If _DateDiff('d', $fDate, _NowCalc()) > 2 Then ; the time

;FileDelete($sourceFolder & "\" & $fileList[$X])

MsgBox(1, "Files deleted:", $fileList[$X], 1)

EndIf

$found[0] = UBound($found)

Next

SECOND FOLDER

$sourceFolder = "\\files01\common\CMS_IBxto\"

$fileList = _FileListToArray($sourceFolder, "*.txt", 1)

THIRD FOLDER

$sourceFolder = "\\files01\common\JD_TEMP\"

$fileList = _FileListToArray($sourceFolder, "*.txt", 1)

WEnd

Link to comment
Share on other sites

You can search for "recursive search" for more examples, but here is a simple one:

#include <Array.au3>
#include <File.au3>

Global $sourceFolder = @ScriptDir

Global $avFolders = _ListFoldersRecurse($sourceFolder)
If @error Then
    ConsoleWrite("Error = " & @error & @LF)
Else
    _ArrayDisplay($avFolders, "$avFolders")
EndIf

Func _ListFoldersRecurse($sDir)
    Local $avSubDir
    Local $avDir = _FileListToArray($sDir, "*", 2) ; 2 = folders only
    If @error Then
        Return SetError(@error)
    Else
        For $n = 1 To $avDir[0]
            $avDir[$n] = $sDir & "\" & $avDir[$n]
            $avSubDir = _ListFoldersRecurse($avDir[$n])
            If @error Then
                ContinueLoop
            Else
                _ArrayDelete($avSubDir, 0)
                _ArrayConcatenate($avDir, $avSubDir)
                $avDir[0] = UBound($avDir) - 1
            EndIf
        Next
        Return SetError(0, 0, $avDir)
    EndIf
EndFunc

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Oh boy...now I am more confused than ever! Although I GREATLY appreciate your reply PsaltyDS, I can't follow this without seeing the vars that I set up. I guess I will have to keep searching and keep trying... Thanks.

You can search for "recursive search" for more examples, but here is a simple one:

#include <Array.au3>
#include <File.au3>

Global $sourceFolder = @ScriptDir

Global $avFolders = _ListFoldersRecurse($sourceFolder)
If @error Then
    ConsoleWrite("Error = " & @error & @LF)
Else
    _ArrayDisplay($avFolders, "$avFolders")
EndIf

Func _ListFoldersRecurse($sDir)
    Local $avSubDir
    Local $avDir = _FileListToArray($sDir, "*", 2) ; 2 = folders only
    If @error Then
        Return SetError(@error)
    Else
        For $n = 1 To $avDir[0]
            $avDir[$n] = $sDir & "\" & $avDir[$n]
            $avSubDir = _ListFoldersRecurse($avDir[$n])
            If @error Then
                ContinueLoop
            Else
                _ArrayDelete($avSubDir, 0)
                _ArrayConcatenate($avDir, $avSubDir)
                $avDir[0] = UBound($avDir) - 1
            EndIf
        Next
        Return SetError(0, 0, $avDir)
    EndIf
EndFunc

:D

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...