Jump to content

Loop throught a folder of files?


Go to solution Solved by water,

Recommended Posts

Hey Guys,

Ive a file that I open and do all sorts of things with it. But my script only works when I do it for 1 file.

How would I get it to: rerun through the tasks after Ive finished dealing with the first file. With about say 3 files of .XML extension on the desktop?

This is my main script with the majority of functions and actions taking place inside the #include files.

 
AutoItSetOption("SendKeyDelay", 50)

#include <.\KSM-500\kWorkbench\kWorkbench_Open.au3>
#include <.\KSM-500\kWorkbench\kWorkbench_Close.au3>
#include <.\KSM-500\kSetup\kSetup_Close.au3>

#include <..\Desktop\LoadFilekWB.au3>
#include <..\Desktop\Setup.au3>
#include <..\Desktop\Verify.au3>
#include <..\Desktop\Program.au3>

#include <.\Library\Error_log.au3>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Local $file = "BCU101C_DSI002.XML"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

HotKeySet("{PAUSE}", "Quit")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func kWBOpen($installDir = "c:\acra", $timeout = 4)
    Return kOpenTheCane("kWorkbench.exe", "TkWBForm", $installDir, $timeout)
EndFunc

Func LDfiletask()
    Return LoadfileWB("TkWBForm", "Open", "TForm1", $file)
EndFunc

Func SnVnPTest()
    Return SnVnP("TDockingHolder", 10)
EndFunc

Func ksetClose($timeout = 4)
    Return KstpClose("TDockingHolder", $timeout)
EndFunc

Func VerFile($timeout = 20)
    Return Verify("TResultsForm", $timeout)
EndFunc

Func Prog($timeout = 20)
    Return Program("TResultsForm", $timeout)
EndFunc

Func kWorkClose()
    Return kWBClose("TkWBForm")
EndFunc

Func Quit()
    Exit
EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Func Main()

    $gInteractiveLog = 1

    ;initialize log
    ACRAOpenLog("kWorkbench Task.log", 2)

    kWBOpen()

    LDfiletask()

    SnVnPTest()
    ksetClose()

    VerFile()
    Prog()

    kWorkClose()

    ; close the log
    ACRACloseLog()
EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Main()
 
 
Link to comment
Share on other sites

There are many ways to skin this cat.

I would pack the processing into a function. And then loop through the list of files using FileFindFirstFile, FileFindFirstFile, FileClose and call the function for every file found.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hmm I see what you mean. But its a bit all over the place, my script that is.

The basic run down of the script is as follows:

1) Open program called kWorkbench

2) Load an XML file into it.

3) Click a button in the kWorkbench which runs a program to setup stuff. After that is finished it closes and returns to kWorkbench

4) Click a button in kWorkbench that verifies the XML file. (Basically an info window) 

5)Click a button in kWorkbench that programs the XML file. (Basically an info window)

6) Close kWorkbench

Now I have a script for each of those steps that are assembled into the one above.

Then the steps are run by calling the main function.

Link to comment
Share on other sites

"If you can make it there, you make it anywhere".

Sorry, it's not NY, it's AutoIt. But if you can make it for a single file it's easy to make it for many files.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Correct.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

olo,

And do not use FileFindFirst/NextFile, use the _FileListToArray function to get an array of the files - or my RecFileListToArray UDF if you need more than one extension or to search recursively. :)

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

If at some point your XML collection becomes huge, I'd suggest reading up on the xml.dom to speed up your output.

edit: although, I don't know exactly what your app does...it's still a possiblity to reduce time for output

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Melba,

what's wrong with FileFindFirst/Next :huh: _FileListToArray does the same under the covers.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

water,

Nothign worng with them at all - but in my opinion is is better for newer coders to use existing functions that wrap more complex concepts.  There is much less chance of the script failing for reasons unconnected with the actual reason the script is being written - and it is much easier for us to debug when needed.  Besides FileFindFirst/NextFile are themselves actually wrappers of the API calls - should we suggest the OP thinks about coding at the DllCall level?  ;)

When coding in any level language I feel that you should take advantage of the libraries that are available until you feel confident enough to look deeper. But as I said that is a personal opinion - no-one is forced to follow it. :)

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

I just wanted to keep it as simple as possible. I've often seen users asking this kind of questions having problems with arrays as well.

But as I mentioned above: There are many ways to .... :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Heys guys ok Ive got it sort of working.

The script runs etc and it detects there are 3 XML files in the desktop.

So it runs 3 times, however it loads the same XML file 3 times.

Now the XML file is located on the desktop along with the others.

Is there any way to instead of set 1 specific file have it load the next XML file each time.?

Local $file = "BCU101C_DSI002.XML"
Link to comment
Share on other sites

Can you post the code where you enumerate the XML files and do the processing?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Well here is now what my main looks like along with the loading script:

#include-once
#include <C:\Documents and Settings\User\Desktop\Library\Error_log.au3>

Func LoadfileWB($MainWindowClassName, $ExternalTitle, $MsgSvrclass,  $file)

    WinWaitActive("[CLASS:" & $MainWindowClassName & "]")
    WinSetState("[CLASS:" & $MsgSvrclass & "]", "", @SW_MINIMIZE)

    ControlClick("[CLASS:" & $MainWindowClassName & "]", "", "TButton1")
    Sleep(500)

    ControlCommand("[TITLE:" & $ExternalTitle & "]", "", "ToolbarWindow322", "SendCommandID", "41061")
    Sleep(500)

    If (ControlSend("[TITLE:" & $ExternalTitle & "]", "", "Edit1", $file)) Then

        If (ControlGetText ( "[TITLE:Open; ]", $file, 1148 )) Then
            ControlClick("[TITLE:" & $ExternalTitle & "]", "&Open", "Button2", "left", 1)
            Sleep(500)
        Else
            ACRAOutputError("Function not entered correctly via keyboard.")
            Exit
        EndIf
    Else
        Sleep(2000)
        ControlCommand("[TITLE:" & $ExternalTitle & "]", "OK", "[CLASS:Button; INSTANCE:1;ID:2 ]", "Check", "")
        ACRAOutputError("Function timed out while trying to load the file!")
        Exit
    EndIf
    ;Return False
EndFunc

;Func LDfiletask()
;    Return LoadfileWB("TkWBForm", "Open", "TForm1", $file)
;EndFunc

;LDfiletask()
Local $file = "BCU101C_DSI002.XML"
Local $Stats_TotalFiles = 0
Local $path = "C:\Documents and Settings\User\Desktop\"
Func Main()

    $gInteractiveLog = 1

    ;initialize log
    ACRAOpenLog("kWorkbench Task.log", 2)

    Local $search = FileFindFirstFile($path & "*.XML")

    ; Check if the search was successful
    If $search = -1 Then
        ACRAOutputError("No files/directories matched the search pattern")
        Exit
    EndIf

    While 1
        Local $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $Stats_TotalFiles = $Stats_TotalFiles +1

        kWBOpen()

        LDfiletask()

        SnVnPTest()
        ksetClose()

        VerFile()
        Prog()

        kWorkClose()


        Sleep(1000)
    WEnd

    ; Close the search handle
    FileClose($search)

    ShowStats()
            #cs
                kWBOpen()

                LDfiletask()

                SnVnPTest()
                ksetClose()

                VerFile()
                Prog()

                kWorkClose()
            #ce
    ; close the log
    ACRACloseLog()
EndFunc
 
Edited by olo
Link to comment
Share on other sites

Where do you call LoadfileWB and which parameters do you pass?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Where do you call LoadfileWB and which parameters do you pass?

I have this function part of the main executable scipt. So the below function is just a few lines above the main function that I posted earlier.

Func LDfiletask()
    Return LoadfileWB("TkWBForm", "Open", "TForm1", $file)
EndFunc
 
Link to comment
Share on other sites

I see nothing wrong with your code.

You could inserte some debugging statements like

ConsoleWrite("x: " & $file & @CRLF)

in your script to verify that the content of $file is as expected. Replace "x: " with the location in your script (e.g. function name or whatever).

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

no no the script executes fine.

But I have 3 XML files on the desktop. Each of a different name.

This script executes 3 times but each time using the same name.

What I want to do is fine a way for the variable $file to look for the other XML files and not just the one below.

local $file = "BCU101C_DSI002.XML"
Edited by olo
Link to comment
Share on other sites

  • Solution

I see.

Set

Local $file = "BCU101C_DSI002.XML"

to

Global $file = "BCU101C_DSI002.XML"

and set

Local $file = FileFindNextFile($search)

to

$file = FileFindNextFile($search)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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