Jump to content

Help with Array


Recommended Posts

Hello all,

Sorry for such a noob question. I sometimes do more complicated stuff and the easy stuff is what stumps me. LOL. I have an array that lists files. What's an easy way to delete those files based off that array? Or maybe a way to exclude some files from being deleted if need be.

Link to comment
Share on other sites

Say your array looks like this:

$Array[0] = 3
$Array[1]="Dir\Filename1.ext"
$Array[2]="Dir\Filename2.ext"
$Array[3]="Dir\Filename3.ext"

Then this would let you decide what to do with them all:

For $i = 1 to $Array[0]
    $ans = MsgBox(3, "Delete", "Do you want to delete this file?"&@CRLF&$Array[$i])
    Switch $Ans
        Case 2
            Exit
        Case 6
            $test = FileDelete($Array[$i])
            If $Test = 0 then MsgBox(0,"Error", "The file "&$Array[$i]&" could not be deleted. It may not exist or may be in use by another program.") 
    EndSwitch
Next

Good luck!

Edited by Paulie
Link to comment
Share on other sites

Maybe something like this?

; Create a 2D array that has those files in it
Local $arrayFileNames[10]

; Create some temporary files
For $i = 0 To 9
    ConsoleWrite("Creating temporary file: @TempDir\TempFile" & $i & ".txt" & @CRLF)
    FileWrite(@TempDir & "\TempFile" & $i & ".txt", "Temporary file text")
    
    ; Store file name in the one dimensional array
    $arrayFileNames[$i] = @TempDir & "\TempFile" & $i & ".txt"
Next

FileDeleteArray($arrayFileNames)

; Create a multidimensional array.
; 0 = file name
; 1 = "Delete" or null --> If value is "Delete", it will get deleted.
Dim $arrayFileNamesSpecified[10][2]

; Create some more temporary files
For $i = 0 To 9
    ConsoleWrite("Creating temporary file: @TempDir\TempFile" & $i & ".txt" & @CRLF)
    FileWrite(@TempDir & "\TempFile" & $i & ".txt", "Temporary file text")
    
    ; Store file name in the one dimensional array
    $arrayFileNamesSpecified[$i][0] = @TempDir & "\TempFile" & $i & ".txt"
Next

; Lets delete just files 3, 5 and 7
Local $filesToDelete[3] = [3, 5, 7]
For $i In $filesToDelete
    $arrayFileNamesSpecified[$i][1] = "Delete"
Next

FileDeleteArray($arrayFileNamesSpecified)

; Starting functions here...

Func FileDeleteArray($array)
    ConsoleWrite("FileDeleteArray() called..." & @CRLF)
    If Not IsArray($array) Then
        MsgBox(0, "Error", "$array is not an array.  Line: " & @ScriptLineNumber)
        Exit
    EndIf
    
    If UBound($array, 2) = 1 Then
        ConsoleWrite("One dimensional array detected." & @CRLF)
        For $i In $array
            If FileExists($i) Then
                ;Uncomment out the following line to make it actually delete:
                ;FileDelete($i)
                ConsoleWrite("FileDelete(" & $i & ") would have been executed here." & @CRLF)
            EndIf
        Next
    ElseIf UBound($array, 2) > 1 Then
        ConsoleWrite("Multi dimensional array detected." & @CRLF)
        For $i = 0 To UBound($array, 1)-1
            If $array[$i][1] = "Delete" Then
                If FileExists($array[$i][0]) Then
                    FileDelete($array[$i][0])
                    If Not @error Then
                        ConsoleWrite($array[$i][0] & " deleted." & @CRLF)
                    Else
                        ConsoleWrite($array[$i][0] & " failed to delete." & @CRLF)
                    EndIf
                Else
                    ConsoleWrite($array[$i][0] & " does not exist.  Did not delete." & @CRLF)
                EndIf
            Else
                ConsoleWrite('$array[$i][1] did not equal "Delete" so ' & $array[$i][0] & 'did not get deleted.' & @CRLF)
            EndIf
        Next
    EndIf
EndFunc
Link to comment
Share on other sites

  • 7 months later...

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