Jump to content
Sign in to follow this  
GIS007

Script Only Works the Second Time It's Run

Recommended Posts

GIS007

Hello All-

I am new to AutoIT, this is my first script. It is supposed to open a program, get a menu option, then open a file on the network. I was able to get it to run however now that the exe is out on the network, when my boss opens it the first time it runs successfully until it reaches the part where it’s supposed to browse out & grab the file. Then it returns an error that the file is not present & it leaves off the first several letters of the file name (should be “PdxBatch09122012.pbt” – see screenshot, it leaves off the beginning & calls it “09122012.pbt"). The weird thing is, when he re-runs the script it finishes successfully. Can anyone help me out with the cause of this weird behavior? As long as he runs the script twice it performs as it should. Here is my code for the tool:

; This tool will open Change Analysis, & run the shapefile
; maintenance batch file located on the L drive.

Run("C:\Program Files (x86)\Pictometry\ChangeAnalysis2.7\ChangeAnalysis.exe")
WinWaitActive("ChangeAnalysis -")
;Get the window by using keystrokes
Send("!a")
Send("{UP}")
Send("{Enter}")
WinWaitActive("Shapefile/DBF Maintenance")
ControlClick("Shapefile/DBF Maintenance", "Browse...", "[ID:2333]")
WinActivate("Open", "2")
;Get the file off the L drive
Send("L:\PdxBatch09122012.pbt")
WinActivate("Open", "10")
Send("{Enter}")
WinWaitActive("Shapefile/DBF Maintenance")
ControlClick("Shapefile/DBF Maintenance", "Run", "[ID:2334]")
WinWaitActive("Shapefile/DBF Maintenance", "List1", "30")

And I'm attaching a screenshot of the error message: ScriptError.bmp

Thank you in advance for any guidance you give!

Share this post


Link to post
Share on other sites
jdelaney

My suggestions:

Use handles rather than text to id controls/windows

Use error checking/validation to make sure a step succeeds prior to blindly moving to the next one

Use ControlSend rather than send

Log all actions to a file, so you can see at what point the script actually breaks, so you can add extra steps just prior to that step to ensure control and window states are as expected

Use WinMenuSelectItem rather than sends

example:

$bContinue = True
$pid = Run("C:\Program Files (x86)\Pictometry\ChangeAnalysis2.7\ChangeAnalysis.exe")
If $pid > 0 Then
 ; Log result to file that you were able to launch app
Else
 $bContinue = False
 ; Log result to file that you were UNable to launch app
EndIf 
...script...
If $bContinue Then
 $hControl = ControlGetHandle("Shapefile/DBF Maintenance", "", 2333)
 If IsHWnd($hControl) Then
  ; Log result to file that you were able to get
 Else
  $bContinue = False
  ; Log result to file that you were UNable to get the handle
 EndIf
EndIf
...script...
; Close app
If ProcessExists ($pid) Then ProcessClose ($pid)
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.

Share this post


Link to post
Share on other sites
GIS007

Wow, ok, thanks jdelaney! Guess I still have a lot to learn about AutoIT syntax. I'll take your example - thanks so much for that - & try to duplicate that.

Thank you!

Share this post


Link to post
Share on other sites
Tripredacus

Since it works fine the second time, and you are dealing with network drives, you may be trying to do things too fast. You may need to put some Sleep() statements in there.

Share this post


Link to post
Share on other sites
jdelaney

Rather than sleeps, which are relative to the slowness of the station you run on, do loops until you find your expected window/control...cap the loop time to what is expected on any station (incase the application fails).

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.

Share this post


Link to post
Share on other sites
jdelaney

ah, it just occured to me what is might be happening...and when you implement the logging, it will probably be confirmed.

some things that you click cause windows to open, which (not sure of the technical reason) will cause your script to stop executing until the window is closed...something about being modal, or windows not returning something that autoit expects, or something.

If you find that to be the case, use a second process to do the click: helpfile for 'Command Line PArameters', example Form 4(Execute one line of code.)

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.

Share this post


Link to post
Share on other sites
GIS007

Ok, I am trying JDelaney's suggestion about the handles - however I think error trapping & log writing is beyond me at this point. Can you please elaborate a bit on your final post here? And remember I am a real newbie so if you could dumb it down quite a bit, I would appreciate it.

This little exe is supposed to open the program & do everything for the user, so they should not be clicking on anything & trying to open or use any windows until it's finished. Thank you again!

Share this post


Link to post
Share on other sites
GIS007

Since it works fine the second time, and you are dealing with network drives, you may be trying to do things too fast. You may need to put some Sleep() statements in there.

So, I should put a sleep statement before the Shapefile/DBF Maintenance window is opened?

Edited by GIS007

Share this post


Link to post
Share on other sites
Tripredacus

Well one thing I think (in case jdelaney is right about using sleeps) is that you are having the trouble opening the program and the file. But does this program support arguments to open the file without automating the GUI? For example, the following from "Run" works...

notepad.exe c:\test\document.txt

So would this also work?

"C:\Program Files (x86)\Pictometry\ChangeAnalysis2.7\ChangeAnalysis.exe" L:\PdxBatch09122012.pbt

Share this post


Link to post
Share on other sites
jdelaney

Oh, what I meant was, you would need to overestimate the sleeps to compensate for, example, when the cpu is over utilized and applications are slow to respond. With the loops, you won't have to wait a hard coded 10 seconds, but instead, keep checking up to 10 seconds within the loop. As far as I know, the sleep times are consistent on all stations?


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
GIS007

Well one thing I think (in case jdelaney is right about using sleeps) is that you are having the trouble opening the program and the file. But does this program support arguments to open the file without automating the GUI? For example, the following from "Run" works...

notepad.exe c:\test\document.txt

So would this also work?

"C:\Program Files (x86)\Pictometry\ChangeAnalysis2.7\ChangeAnalysis.exe" L:\PdxBatch09122012.pbt

Well, when I run that from the cmd line, I get a message saying L:\PdxBatch09122012.pbt was not found. Is that because I am using the mapped drive? Edited by GIS007

Share this post


Link to post
Share on other sites
Tripredacus

Well, when I run that from the cmd line, I get a message saying L:PdxBatch09122012.pbt was not found. Is that because I am using the mapped drive?

Well I didn't know if it worked at all, apparently it does but even the app can't find it right away. It could be possible that the app is looking in the wrong place for that file. You could try attaching ProcMon to the ChangeAnalysis.exe process, then try to execute the command and see where the app is looking for the file. I've had problems automating some apps and keeping files (or binaries) on the network drive. So it is helpful to understand exactly what the app is trying to do, so that you can tackle the issue correctly.

Share this post


Link to post
Share on other sites
GIS007

Thanks for your input. Here's what I'm trying to do:

  • Open Change Analysis.
  • Browse to the menu item named "Annotate."
  • Select the "Shapefile/DBF Maintenance" submenu. The next step is to process the files (I'm sorry I don't really know much about what that does, it might be building some kind of index since these files get re-written every night).
  • Then I need to click the browse button. The program defaults to the last place that the files were processed from. It is on the L drive which from what I can figure out, this is a virtual drive, I think the actual folder resides on one server but when the L drive was mapped it was mapped to another server. I can't get anyone to really drill down & explain the nuts & bolts to me but I believe the actual folder is on a different box, and the L drive is mapped to a 2nd server. Don't know if that is significant or not.
  • I have to select the pbt file which is some kind of batch file that was created by the person who set up the program. Apparently it gets changed from time to time but the last time it was modified was 2012.
  • Once the pbt file is selected, have to click the "Run" icon & it 'processes' these files, which takes about 25 minutes.
  • Then the window closes & the main program window is ready.
It consistently bails out the first time it's run but if I (or my boss) just go right ahead & click it again, it works without a hitch. He wants to put this on a schedule so that it runs at night. Should I just suggest to him to schedule it twice? (I realize that is probably hilariously stupid to suggest but sometimes stupid works, right?)

Thanks in advance!

Share this post


Link to post
Share on other sites
Tripredacus

I would put the effort in determining why the program fails rather than using a workaround such as "running it twice" if it were me. :)

Share this post


Link to post
Share on other sites
GIS007

I'm getting pulled off this project so another programmer can look at it. I still do want to try to fix this, so I can learn it myself as opposed to letting this senior person take it and run with it. Thanks for all your help.

Share this post


Link to post
Share on other sites
careca

Hello, got some questions.

1 - If the .pbt file is some kind of batch file, why don't you run those commands directly in autoit?

2 - Does that application have command line? if so, try to import the file that way.

3 - PdxBatch09122012.pbt is the only .pbt in the drive? If so, you can find all files with that extension,

using a udf, like "RecFileListToArray", grab whatever it's name is, and write that name in the application

where you then run or open it or whatever, this means you wouldn't have to bother if the filename is the same

or exists, as long as there is only one with that extension.

EDIT RecFileListToArray

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Au3Record.exe

Share this post


Link to post
Share on other sites
GIS007

Hello, got some questions.

1 - If the .pbt file is some kind of batch file, why don't you run those commands directly in autoit?

2 - Does that application have command line? if so, try to import the file that way.

3 - PdxBatch09122012.pbt is the only .pbt in the drive? If so, you can find all files with that extension,

using a udf, like "RecFileListToArray", grab whatever it's name is, and write that name in the application

where you then run or open it or whatever, this means you wouldn't have to bother if the filename is the same

or exists, as long as there is only one with that extension.

EDIT RecFileListToArray

Thanks for the input.

1-My boss wants this to run as a scheduled job so I felt an exe would be the best thing to deliver to him.

2-No, Change Analysis is strictly all GUI.

3-Yes it is the only file w/that extension. I'll try your suggestion.

I have been randomly asking people about this issue & someone suggested that the connection to the network drive is being lost, which is why the program has to run twice - the first attempt will re-establish the connection. He said to use a Refresh command to get the network connection re-established, but I don't see anything like this in AutoIT. Any ideas?

Thanks!

Share this post


Link to post
Share on other sites
jdelaney

DriveMapGet

DriveStatus

or even FileExists for the actual file.

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.

Share this post


Link to post
Share on other sites
GIS007

What about just using ping? It appears that the connection to the network drive is getting dropped, perhaps that is why the first attempt fails but the second one, which hits the network after the connection has been refreshed, works? So, if I just ping that server won't that refresh the connection?

I inserted a "ping( <network IP>)" statement and ran it, & it appears to work, but won't be able to say for sure until the jobs run tonight. Am I on the right track at all?

  • Like 1

Share this post


Link to post
Share on other sites
Tripredacus

I inserted a "ping( <network IP>)" statement and ran it, & it appears to work, but won't be able to say for sure until the jobs run tonight. Am I on the right track at all?

I think so. This may not be an ideal solution if at any point a firewall rule is changed on the server or elsewhere in the network that blocks the ping in the future. As jdelaney posts, you could use FileExists multiple times... check once, if not found, wait a couple sections, check again. If it then exists, then execute your code.

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  

×