Sign in to follow this  
Followers 0
Diana (Cda)

Not process exists or file exists ... if a certain file

8 posts in this topic

I ran into this today. Rather than having AI launch a process only if it wasn't already, or deleting a file if it existed (both cases I know how to deal with), how can we deal with the situation when we request AI to open up a file only if it isn't open already?

I have a script that opens 2 documents but if I don't realize either is already up and running, it opens either of them up a second time so I have 2 instances of either to deal with. Not good.

I looked in the help file but couldn't find anything.

Thanks! :)

Share this post


Link to post
Share on other sites



Maybe...

; ********** set-up

$File = "C:\Test.txt" 
$Copy = "C:\Copy_Test.txt" 

FileWriteLine($File, "This is a test")

FileDelete($Copy)

$Open = FileOpen($File, 0)

If $Open = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

Sleep(1000)

; ********** end set-up

;*********** actual checking code

If Not FileMove($File, $Copy, 1) Then
    MsgBox(0x0, "error", "The file is in use")
Else
    MsgBox(0x0, "copied", "The file was copied... it was not open????")
    FileMove($Copy, $File, 1) ; put it back
EndIf

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

On a server you are supposed to have a utility called openfiles. On a unix system you have lsof. But searching for lsof.exe did not give me any good result making me wonder if it is possible to list open files (that is files with a file handle pointing at them?) in windows.

@valuater's solution is a brute force workaround but I think it will fail in many cases where AutoIt is not the application opening the file but rather a script to have another application open a file.

Share this post


Link to post
Share on other sites

Do you need the file open in AutoIt for reading or writing or both? I assume the file is being written to because having the file open for reading by more than one process wouldn't cause a problem. It would cause a problem if it was open by two processes for writing the and processes decided to write to the file at the same time.

- The Kandie Man ;-)


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

works with word

; ********** set-up

$File = "C:\Test.txt" 
$Copy = "C:\Copy_Test.txt" 

FileWriteLine($File, "This is a test")

FileDelete($Copy)

Run("C:\Program Files\Microsoft Office\Office\WINWORD.EXE" & " " & $File)

Sleep(10000)

; ********** end set-up

;*********** actual checking code

If Not FileMove($File, $Copy, 1) Then
    MsgBox(0x0, "error", "The file is in use")
Else
    MsgBox(0x0, "copied", "The file was copied... it was not open????")
    FileMove($Copy, $File, 1) ; put it back
EndIf

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

If it is the same script that is opening the files more than once, then you would only need a simple fix like this:

Global $gb_DB1_Open = False
Global $gb_DB2_Open = False

Dim $sFile = @ScriptDir & "\DocumentToLock.txt"

Dim $hFile = _FileOpenDataBase1($sFile,0);Use the same paramters as FileOpen()
ConsoleWrite("+> $hFile="&$hFile &@LF)

Dim $hFile2 = _FileOpenDataBase1($sFile,0)
If @error Then
    ConsoleWrite("!> Error.  Unable to open the file because it is already open. @error="&@error&@LF&"!> $hFile2="&$hFile2&@LF)
EndIf

_FileCloseDataBase1($hFile)


Func _FileOpenDataBase1($sFile,$iMode)
    If $gb_DB1_Open Then
        Return SetError(1,0,-1)
    EndIf
    $gb_DB1_Open = True
    Return FileOpen($sFile,$iMode)
EndFunc

Func _FileOpenDataBase2($sFile,$iMode)
    If $gb_DB2_Open Then
        Return SetError(1,0,-1)
    EndIf
    $gb_DB2_Open = True
    Return FileOpen($sFile,$iMode)
EndFunc

Func _FileCloseDataBase1($hFile)
    $gb_DB1_Open = False
    Return FileClose($hFile)
EndFunc

Func _FileCloseDataBase2($hFile)
    $gb_DB2_Open = False
    Return FileClose($hFile)
EndFunc

By writing custom functions that open and close the files, you can use global variables to determine if you already opened the files. If your custom functions determine that they have already opened the files, they will return -1, just like the normal FileOpen() function and they will set @error = 1. This should prevent you from opening the files more than once.

Of course, if you are using more than one script to access the files, then you are going to need to use a different method. I have already written functions to deal with this, let me know if this is the problem.

- The Kandie Man ;-)


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Oh, geez. I'm really sorry, I didn't explain myself well enough. I should have included the code. It's just that we're having heavy snowfall and I was trying to get the message posted otherwise I'd probably forget till tomorrow so was rushing a bit as my trip home from office is always a long one and was really trying to get out the door, too. So wasn't thinking as clearly as I should have been <g>.

Here is the script:

AutoItSetOption("WinTitleMatchMode", 2)     ; this allows partial window titles to be valid!

ShellExecute($PortableDrive & "\APPS\TEXT\npad, Metapad v3.51\APP- Metapad v3.51\Metapad.exe", @ScriptDir & "\..\Pronunciation file to fix.txt")

WinWait("Pronunciation file to fix")
WinSetState ("Pronunciation file to fix", "", @SW_MAXIMIZE)

Send("{CTRLDOWN}{END}")
Sleep(500)

$PortableDrive = StringLeft(@ScriptDir, 2)
ShellExecute($PortableDrive & "\eBooks-BOOKS\Manual, Reference.pdf")

WinWait("Manual, Reference.pdf")
WinSetState ("Manual, Reference.pdf", "", @SW_MAXIMIZE)
One click opens the 2 files and the text file is ready for input after AI scrolls down to the end in it.

The trouble I ran into today when I started using this script, was that if the documents are already open, they'll be opened again, which is not good. I sometimes have to access the 2 files quite frequently and usu. close after each annotation, but sometimes the edits come close together and I leave it open but then click on the script next time I need it and then find duplicate instances of the files up and running, clogging up my taskbar unncecessarily, etc.

Thinking I was so smart, I tried using the "if not process exists" forgetting that that's only for running processes, not for actual documents. i.e., that syntax would work only on the apps opening the files themselves and is not geared towards the files being opened. In other words, if I was just interested in opening up the apps metapad and Acrobat, vs. the actual docts. themselves, this would work. But that's not the case here.)

So was hoping there was something like this, though the "processExists" is designed to work with processes or apps as mentioned above; this is just to give an idea of the goal of the script:

If Not ProcessExists("Pronunciation file to fix.txt") Then Run("Pronunciation file to fix.txt")

If Not ProcessExists("Manual, Reference.pdf") Then Run("Manual, Reference.pdf")

Thanks. :)

Share this post


Link to post
Share on other sites

Maybe something as simple as this would be sufficient?:

AutoItSetOption("WinTitleMatchMode", 2)     ; this allows partial window titles to be valid!

If Not WinExists("Pronunciation file to fix") Then
    ShellExecute($PortableDrive & "\APPS\TEXT\npad, Metapad v3.51\APP- Metapad v3.51\Metapad.exe", @ScriptDir & "\..\Pronunciation file to fix.txt")

    WinWait("Pronunciation file to fix")
    WinSetState ("Pronunciation file to fix", "", @SW_MAXIMIZE)

    Send("{CTRLDOWN}{END}")
    Sleep(500)
EndIf
If Not WinExists("Manual, Reference.pdf") Then
    $PortableDrive = StringLeft(@ScriptDir, 2)
    ShellExecute($PortableDrive & "\eBooks-BOOKS\Manual, Reference.pdf")

    WinWait("Manual, Reference.pdf")
    WinSetState ("Manual, Reference.pdf", "", @SW_MAXIMIZE)
EndIf

- The Kandie Man ;-)


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

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