fRequEnCy Posted May 31, 2008 Posted May 31, 2008 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.
Paulie Posted May 31, 2008 Posted May 31, 2008 (edited) 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 May 31, 2008 by Paulie
duckling78 Posted May 31, 2008 Posted May 31, 2008 Maybe something like this? expandcollapse popup; 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
fRequEnCy Posted May 31, 2008 Author Posted May 31, 2008 Thanks Paulie! That worked great and so simple. Also thanks to you as well duckling78. Will dig more into your code to see what you did. Many thanks guys!
plgii Posted January 17, 2009 Posted January 17, 2009 How did you get this to work, as when I try I get: Expected a "=" operator in assignment statement.: $Array[0] = 3 $Array^ ERROR
4nt1h4cker Posted January 17, 2009 Posted January 17, 2009 You have to use: Dim $Array[4] $Array[0] = 3 $Array[1]="Dir\Filename1.ext" $Array[2]="Dir\Filename2.ext" $Array[3]="Dir\Filename3.ext"
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now