Jump to content

AutoIt OpenProgram, OpenFile, Run .exe's, CloseFile/Program, Repeat on a new file

Recommended Posts

So, I haven't programmed in years. AutoIt has brought back my interest. I am working to automate many processes. The current process is opening PDFs and converting them to Excel. I don't want the Array to worry about the file name, nor the amount of files in the folder. I built many small macros with the recorder. I want to run these macros on one file, then loop and do the reset in the folder. It works to do one file, however it will not move to the next file. Ive tinkered around with $i + 1 etc, but obviously don't have it in the proper location. I have been researching this site, other sites, and the help files. I find many options, but vast the variety of options, leave me with code that does not increment to the next file. I could delete the first file so that the next file is the "new first file" when it loops, however I am not able to capture the file name so that the code knows the file to remove. Then I thought, maybe I ought to not delete anything, and just get it working so that it moves to the next file. Ive looked at FileNextFile etc, but have had a bunch of issues. So, I figured I would ask the pros. I am most likely over-complicating it, but I have learned a lot about autoit in the process. Please someone throw me a bone here.....

Be nice, and thanks in advance :-)


#include <file.au3>
#include <array.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

$extension    = "pdf"

$monarch = "C:\Program Files\Datawatch Monarch 13\DPS\DWDataPrepStudio.exe"
$path = "C:\PDF"
$choosePDF = "C:\Macros\3_ChoosePDF.exe"
$chooseColumns = "C:\Macros\4_ChooseColumns.exe"
$chooseMemberColumn = "C:\Macros\5_ChooseMemberColumn.exe"
$dataPrep = "C:\Macros\6_DataPrepPreviewLoadTables.exe"
$nulls = "C:\Macros\7_Nulls.exe"
$export = "C:\Macros\8_Export.exe"
$closeMonarch = "C:\Macros\9_CloseMonarch.exe"
$deleteFirstPdfFile = "C:\Macros\10_DeleteFirstPdfFile.exe"


; ########################################
;~ Example()  ; just trying something
;~ Func Example()
;~ While $FileArray <> 0
;~ _ArrayPop($FileArray)
;~ _ArrayDisplay($FileArray)
;~ EndFunc
; ########################################

$FileArray = _FileListToArray($path, "*." & $extension,1) ; As long as a file exists in C:\PDF - get the list of files

For $i = 1 To $FileArray[0]

Run($monarch  & ' "' & $path &  $FileArray[$i] -1 & '"') ;Runs Monarch

Run($choosePDF) ;Runs $choosePDF











Link to post
Share on other sites
  • Moderators

First question, once you do your _FileListToArray line, does an _ArrayDisplay command show you what you would expect (i.e. the full path name of all the files in the directory)?

Secondly, on your run line:

Run($monarch  & ' "' & $path &  $FileArray[$i] -1 & '"') ;Runs Monarch

Have you output that to the Console to confirm you're not missing any /'s?


Beyond those two simple checks, and if the process is successful all the way through your first For iteration, you're going to have to explain more about what these embedded executables are doing. The problem may lay with one of them, rather than your main script.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to post
Share on other sites

Thank you for the response!

1. An _ArrayDisplay does give me the list of the array. If I add or remove files, the array does adjust correctly.


This is what I currently get when using _ArrayDisplay:

Row|Col 0


****Every time I run the original code, the code always picks the CAPT3662015not.pdf file. I need it to move to the next file each time it loops. It currently loops but never moves from the last file.

2. Output to Console passes successfully.

3. The process is successful all the way through for the first file that is chosen. As you mentioned maybe the issue is with my embedded .exes. I am choosing the file with a recorded macro, called (choosepdf.exe). I just recorded myself choosing a file in C:\PDF. So I was thinking to remove that file, so the next time I loop through, it will pick the next file. I would like know how to choose the file without a recorded macro. FileOpen doesnt really double click and my most frustrating issue is that it does not change files.

4. What is a better way to choose a file and loop through that folder? I am looping through all of the processes with one file (my choosepdf.exe picks the first file in the list). But I need to loop through that folder.

****Heres the code for the choosepdf.exe file. Maybe this is the culprit. Its just a recorded macro...:

AutoItSetOption("MouseCoordMode", 0)

;WinWait("Untitled") ;waits for the window to open
WinActivate("Untitled") ;activates the window
MouseClick("primary", 464, 524, 1, 0) ;

MouseClick("primary", 363, 583, 1, 0)

MouseClick("primary", 537,99, 1, 0) ;clicks on address bar
MouseClick("Primary", 493, 191, 2, 0) ;chooses the first file in the list by double-clicking







Link to post
Share on other sites

What happens if you try something like:

#include <File.au3>

Opt('ExpandVarStrings', 1)

Global $hMonarch = "C:\Program Files\Datawatch Monarch 13\DPS\DWDataPrepStudio.exe"
Global $sPath = "C:\PDF"
;~ ... <rest of your code here>

Global $aFileList = _FileListToArrayRec($sPath, '*.pdf', 1, 1, 0, 2)
If @error Then Exit

For $i = 1 To $aFileList[0]
    Run('"$hMonarch$" "' & $aFileList[$i] & '"');Runs Monarch
;~ ... <rest of your code here>


Link to post
Share on other sites
19 minutes ago, DarylDixon said:

This is what I currently get when using _ArrayDisplay:

Row|Col 0


****Every time I run the original code, the code always picks the CAPT3662015not.pdf file. I need it to move to the next file each time it loops. It currently loops but never moves from the last file.

Why the -1?

Run($monarch  & ' "' & $path &  $FileArray[$i] -1 & '"')



_FileListToArray($path, "*." & $extension,1)

could simply be:

_FileListToArray($path, "*.pdf", 1)



Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples


Link to post
Share on other sites
  • 3 weeks later...

Thanks to everyone that commented.

To give you an update, I realized I am looping all of my steps so that they repeat, but not looping inside of the folder so that it applies to each file.

I ended up adding a script so that after the first file is complete, it is deleted. When the scripts start over, it will pick the new first file. Cheesy, I know...its working though. I would like to get it to work properly. So if anyone has a link to an example of looping through several scripts/functions, AND looping through .pdf/.xls files in a folder....that'd be grrreaaattt.  If I recall correctly from all my old college courses, that would be a nested loop?

I appreciate all of the suggestions. Most of the code posted before came from examples, so that would be why I used the syntax, that I did, in my code. Once I get this working on my PC, nicely, I will look to fine tune the code.

My New issues are:

1. Nested Loops (Same issue as above)

2. Getting the macros and clicks to work on someone else's computer! Becaussseeee it isn't working else where.....(Window Size an issue?). What are your experiences and fixes for this issue? ControlClick vs Send the issue when running code on another pc? We both remote into a Virtual PC and we are both running this code on that virtual pc, not our own pc. Is it screen size??

3. Reading text from a single cell in an already open excel file, and do an if statement off of the text in that one cell. If Cell A1 = "member" do this, else do this...

I know, I know, post a new question. I will search the site before I start a new feed. But if someone has the answers to a nested loop, post below, or send me a message please! You would help me greatly!

Thanks again!

Link to post
Share on other sites
  • Moderators



looping through .pdf/.xls files in a folder

This post of mine from yesterday might be of interest.


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:


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 post
Share on other sites

@DarylDixon - Have you looked at Monarchs Built-in Automation?  If you were to build a Model you could then use the command line options within Monarch to apply these models to your files http://docs.datawatch.com/monarch/commandline_guide/Datawatch_Monarch_Command_Line_User_Guide.pdf

This would be the most resilient way for managing your documents rather than send keys and mouse movments.



Edited by Subz
Accidental 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...