Jump to content

How to loop through an array


Nunos
 Share

Go to solution Solved by czardas,

Recommended Posts

I am trying to write a script that will find all of the .xml files in a specific folder and then delete them without performing any tasks on any other files or folders in the directory. Below is what I tried but I am not getting it to delete them. Thank you in advance for your time.

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

Local $Folder = "C:\Afolder"
Local $sFilter = "*.xml"
Local $FileList = _FileListToArray($Folder,$sFilter)
If @error = 1 Then
    MsgBox(0, "", "No Folders Found.")
    Exit
EndIf
If @error = 4 Then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

For $i = 0 to UBound($FileList) -1
    FileDelete($FileList[$i])
Next
Link to comment
Share on other sites

Nunos,

Added some diag msg's and ran it like this...

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

Local $Folder = @scriptdir
;Local $Folder = "C:\Afolder"
Local $sFilter = "*.xml"
Local $FileList = _FileListToArray($Folder,$sFilter)

if not isarray($FileList) then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

If @error = 1 Then
    MsgBox(0, "", "No Folders Found.")
    Exit
EndIf
If @error = 4 Then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

For $i = 0 to UBound($FileList) -1

    if FileDelete($FileList[$i]) = 1 then
        ConsoleWrite('Deleted file = ' & $FileList[$i] & @LF)
    Else
        ConsoleWrite('Delete Failed for file = ' & $FileList[$i] & @LF)
    endif

Next

I suspect that you are not finding any files in the Dir that you are referencing.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Nunos,

Added some diag msg's and ran it like this...

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

Local $Folder = @scriptdir
;Local $Folder = "C:\Afolder"
Local $sFilter = "*.xml"
Local $FileList = _FileListToArray($Folder,$sFilter)

if not isarray($FileList) then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

If @error = 1 Then
    MsgBox(0, "", "No Folders Found.")
    Exit
EndIf
If @error = 4 Then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

For $i = 0 to UBound($FileList) -1

    if FileDelete($FileList[$i]) = 1 then
        ConsoleWrite('Deleted file = ' & $FileList[$i] & @LF)
    Else
        ConsoleWrite('Delete Failed for file = ' & $FileList[$i] & @LF)
    endif

Next

I suspect that you are not finding any files in the Dir that you are referencing.

kylomas

Thank you for your time and assistance with this kylomas,

The files do exist in the directory but it is still not deleting them. I am not sure where it is hanging up because none of the @errors are tripping it that were added in the version you posted above.

Link to comment
Share on other sites

Nunos,

Run this and post the entire console log

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

Local $Folder = @scriptdir
;Local $Folder = "C:\Afolder"
Local $sFilter = "*.xml"
Local $FileList = _FileListToArray($Folder,$sFilter)

if isarray($FileList) then ConsoleWrite('Number of files returned = ' & $FileList[0] & @LF)

if not isarray($FileList) then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

If @error = 1 Then
    MsgBox(0, "", "No Folders Found.")
    Exit
EndIf
If @error = 4 Then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf

For $i = 1 to UBound($FileList) -1      ; offset 0 has the file count

    if FileDelete($FileList[$i]) = 1 then
        ConsoleWrite('Deleted file = ' & $FileList[$i] & @LF)
    Else
        ConsoleWrite('Delete Failed for file = ' & $FileList[$i] & @LF)
    endif

Next

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:UsersMEDesktopScirptingAutoITIn Progress and ResearchingDelete XML.au3" /autoit3dir "C:Program FilesAutoIt3" /UserParams   
+>23:15:31 Starting AutoIt3Wrapper v.2.0.1.24    Environment(Language:0409  Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X86)
>Running AU3Check (1.54.22.0)  from:C:Program FilesAutoIt3
+>23:15:31 AU3Check ended.rc:0
>Running:(3.3.8.1):C:Program FilesAutoIt3autoit3.exe "C:UsersMEDesktopScirptingAutoITIn Progress and ResearchingDelete XML.au3"   
Number of files returned = 3
Delete Failed for file = 1 - Copy (2).xml
Delete Failed for file = 1 - Copy.xml
Delete Failed for file = 1.xml
+>23:15:32 AutoIT3.exe ended.rc:0
>Exit code: 0    Time: 3.727
 

Link to comment
Share on other sites

Your code is running correctly (assuming that there are three files in the folder you pointed to).

You might try checking file attributes and folder permissions.

edit: and if these files are in use

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

 

You need to add the path to the folder to locate the files.

FileDelete($Folder & "\" & $FileList[$i])

Thank you czardas and kylomas this seems to have fixed my problem. I guess I didn't understand that I wasn't deleting the items from the array but needed to reference the path first? I am still trying to learn all of the ins and outs.

Thank you both again for all of your time and help. :D

Edited by Nunos
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...