Jump to content

[SOLVED] FileDelete in array - Problems


Recommended Posts

Hi guys, i have this simple script:

#Include <File.au3>
#Include <Array.au3>
#include <RecFileListToArray.au3> ; External UDF

$FileList=_RecFileListToArray(@Workingdir & "\Test", "*", 2, 1, 0, 2, "", "") ; Only Folder, all subfolder, full path
If @Error=1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
For $i = 1 to $FileList[0]
    If StringInStr(FileGetAttrib($FileList[$i]),"D") Then
       If DirGetSize($FileList[$i]) = 0 Then
            If DirRemove($FileList[$i]) Then
                ConsoleWrite("Dir removed: " & $FileList[$i] & @crlf)
             Else
                ConsoleWrite("Could not remove (empty?) dir: " & $FileList[$i] & @crlf)
            Endif
        Else
            ConsoleWrite("Not removed (contains files): " & $FileList[$i] & @crlf)
        Endif
    EndIf
Next
MsgBox(0,"Results", "Empty Removed")

I have this problem. If i have a situation like this:

EmptyDirMaster\EmptyDir\EmptyDir

The array remove only the subfoder and don't the EmptyDirMaster. On ConsoleWrite i have:

Could not remove (empty?) dir: Path\EmptyDirMaster

If i start again it will delete the folder, but why don't process the first time?

Thanks for support

Edited by johnmcloud
Link to comment
Share on other sites

Hello johnmcloud,

while you're looping your array the first subfolder get's deleted. The first one not because it's in the array before the subfolder:

[0] = 3
[1] = C:temp
[2] = C:tempDir1

So basically C:temp is not empty but 0 in size before C:tempDir1 get's deleted.

Edit:

By the way, why do you check whether your Path is a Directory ( StringInStr(FileGetAttrib($FileList[$i]),"D") ) id you're a) using _RecFileListToArray() with the "Folder only" function and :) DirRemove() ?

Edited by hannes08
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

#Include <File.au3>
#Include <Array.au3>
#include <RecFileListToArray.au3> ; External UDF
 
$FileList = _RecFileListToArray(@Workingdir & "Test", "*", 2, 1, 0, 2, "", "") ; Only Folder, all subfolder, full path
If @Error = 1 Then
MsgBox(0, "", "No FilesFolders Found.")
Exit
EndIf
 
While 1
For $i = 1 to $FileList[0]
   If DirGetSize($FileList[$i]) = 0 Then
    If DirRemove($FileList[$i]) Then
     ConsoleWrite("Dir removed: " & $FileList[$i] & @crlf)
    Else
     ConsoleWrite("Could not remove (empty?) dir: " & $FileList[$i] & @crlf)
    Endif
   Else
    ConsoleWrite("Not removed (contains files): " & $FileList[$i] & @crlf)
   Endif
Next
WEnd
 
MsgBox(0, "Results", "Empty Removed")

Work but...Where i need to put the exitloop? I can't stop it :)

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

Try using a flag like this (I have not tested it): ;)

#include <File.au3>
#include <Array.au3>
#include <RecFileListToArray.au3> ; External UDF

$FileList = _RecFileListToArray(@WorkingDir & "Test", "*", 2, 1, 0, 2, "", "") ; Only Folder, all subfolder, full path
If @error = 1 Then
    MsgBox(0, "", "No FilesFolders Found.")
    Exit
EndIf

Do
    ; Set a flag
    $fNoProbs = True
    For $i = 1 To $FileList[0]
        If DirGetSize($FileList[$i]) = 0 Then
            If DirRemove($FileList[$i]) Then
                ConsoleWrite("Dir removed: " & $FileList[$i] & @CRLF)
            Else
                ConsoleWrite("Could not remove (empty?) dir: " & $FileList[$i] & @CRLF)
                ; Clear the flag
                $fNoProbs = False
            EndIf
        Else
            ConsoleWrite("Not removed (contains files): " & $FileList[$i] & @CRLF)
        EndIf
    Next
    ; If the flag is set then there were no problems
Until $fNoProbs

MsgBox(0, "Results", "Empty Removed")

Does it work for you? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Yes Melba, sorry for this later answer.

I need to learn this use of flag :)

Thanks

EDIT: I'm talk to early, not work. In my case i have:

Func_Files() ;Delete files
Func_Empty() ;Delete empty folder
MsgBox(64, "OK", "Completed")

The code with flag work on empty folder that was empty at start, but not with "new" empty folder

Example:

TopFolder/Folder_1/test.Txt

TopFolder/Folder_2( empty)

First function delete the .txt, the second it will delete empty folder, but...

TopFolder/Folder_1/test.Txt ---> Test.txt deleted, the folder is empty but not delated

TopFolder/Folder_2( empty) ---> Deleted

So i have the Folder_1 and the TopFolder, together empty but not deleted

I have make a sleep between the two function but the result is the same

EDIT: I have verify again, it will not delete the first folder, never. Example:

C:Test123asd

The result is

C:Test

Why the "Test" folder is not processed?

EDIT: Solved with another DirGetSize on the first folder. Thanks for support

Edited by johnmcloud
Link to comment
Share on other sites

  • Moderators

johnmcloud,

Do you understand now how the flag works? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...