Jump to content
Sign in to follow this  
olo

Loop throught a folder of files?

Recommended Posts

olo

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()
 
 

Share this post


Link to post
Share on other sites
water

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.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
olo

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.

Share this post


Link to post
Share on other sites
water

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

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
olo

haha so just gotta loop through everything using those functions you listed.

Share this post


Link to post
Share on other sites
water

Correct.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Melba23

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

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
jdelaney

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
  • Like 1

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.

Share this post


Link to post
Share on other sites
olo

Thanks guys ill go away and see what I come back with in a few. :)

Share this post


Link to post
Share on other sites
water

Melba,

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


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Melba23

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

  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
water

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

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
olo

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"

Share this post


Link to post
Share on other sites
water

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

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
olo

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

Share this post


Link to post
Share on other sites
water

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

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
olo

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
 

Share this post


Link to post
Share on other sites
water

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

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
olo

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

Share this post


Link to post
Share on other sites
water

I see.

Set

Local $file = "BCU101C_DSI002.XML"

to

Global $file = "BCU101C_DSI002.XML"

and set

Local $file = FileFindNextFile($search)

to

$file = FileFindNextFile($search)
  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  

×