Sign in to follow this  
Followers 0
Dana

how to determine is a file is open by another program

4 posts in this topic

This one has me stumped.

I'm working on a routine that, among other things, exports a .pdf file from KeyCreator (a CAD package). I can do that part no problem, but I've been unable to determine when the pdf export process is done.

I use Send to start the pdf export, WinWaitActive to wait for the "Save As" dialog, stuff the desired filename into the dialog, click OK, etc., but then I need to wait until the export is complete before continuing. Unfortunately, once the "save as" dialog closes, there is nothing in the main KeyCreator window that's visible to indicate that the export process is still running or complete (there is a visible text on the screen, but the AutoIt window info doesn't see it; presumably it's a non standard window).

The next step is to open the pdf file in Acrobat reader or other program, using ShellExecute with the pdf filename. However, ShellExecute returns 1 whether or not the file opens. Same for Run. I'm guessing that the returned 1 is coming from Foxit Reader (which I'm using in place of Acrobat Reader) since the reader opened successfully, but Foxit reports "file not found".

I've tried looking at the output file itself. FileExists doesn't work because it's created as soon as the export process starts. I tried "Open" in binary mode, but it always returns an error even if the export is complete.

At this point the only way I can get the program to work is to use a message box for the operator to close after he sees the export process is done. Then everything works, but it's not a good solution.

What I need is a way for AutoIt to determine if another program has a file open, but I can't find it. Any suggestions

Share this post


Link to post
Share on other sites



Dana,

A quick Search (the facility is at top right of the page :P ) threw up this. I hope it is of some use. :x

M23


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

Thanks... I searched before posting, I really did, but used the wrong terms, I guess, didn't search on "pdf". Then I searched again, for pdf and found the same thread you posted. My bad, all is well now.

Share this post


Link to post
Share on other sites

; FileInUse with Info using WMI

; example one with process
Local $proc = "AutoIt3.exe"
Local $rtn = FileInUseInfo($proc, 1)
If (Not @error) And $rtn Then MsgBox(0, 'FileInUseInfo(' & $proc & ', 1)', $rtn)

; example two with file
Local $file = @ScriptName
Local $rtn = FileInUseInfo($file, 2)
If (Not @error) And $rtn Then MsgBox(0, 'FileInUseInfo(' & $file & ', 2)', $rtn)


;##############################################################################################
; FileInUseInfo - Modify it for your needs
;
; example $mode = 1; FileInUseInfo("AutoIt3.exe", 1) ;returns file(s) used by process
;
; example $mode = 2; FileInUseInfo("SomeFile.txt", 2) ;returns process(s) using file
;##############################################################################################
Func FileInUseInfo($sInput, $mode = 1, $strComputer = "localhost")
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    If @error Then Return SetError(-1)
    Local $output = '', $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", 0x10 + 0x20)
    If Not IsObj($colItems) Then Return SetError(-2)
    For $objItem In $colItems
        If $objItem.CommandLine = '' Or StringLeft($objItem.CommandLine, 1) = '\' Then ContinueLoop
        If $mode = 1 And $objItem.Name = $sInput Then
        ElseIf $mode = 2 And StringInStr($objItem.CommandLine, $sInput) Then
        Else
            ContinueLoop
        EndIf
        $output &= "PID: " & $objItem.ProcessId & @CRLF
        $output &= "CommandLine: " & $objItem.CommandLine & @CRLF & @CRLF
    Next
    Return $output
EndFunc


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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  
Followers 0