Jump to content

Need to check if file is ready to open.


Recommended Posts

A process of mine opens a HML file, modifies it and then saves, closes it.

I then need to run an.exe that opens this file right after this.. However, nothing that I find on this forum works that supposedly tests to see if the fie is still open, ready to be used by another process.

Right now, I just Sleep(2000) .

I would like something cleaner than this.

This function, for example, returns False when nothing has it opened or if I open the file with notepad or any other editor.

I am running on a win7 laptop, if that has any bearing.

$File="C:\Utilities\test\report.hr5"
MsgBox(0,"", _FileInUse($File))


Exit


Func _FileInUse($sFilePath) ; By Nessie. Modified by guinness.
    Local Const $hFileOpen = _WinAPI_CreateFile($sFilePath, $CREATE_ALWAYS, $FILE_SHARE_WRITE)
    If $hFileOpen Then
        _WinAPI_CloseHandle($hFileOpen)
         Return False
    EndIf

    Local $fReturn = False
    If _WinAPI_GetLastError() = 32 Then $fReturn = True
    Return $fReturn
EndFunc

 

Link to comment
Share on other sites

Link to comment
Share on other sites

 

6 hours ago, AutoitMike said:

I then need to run an.exe that opens this file right after this.. However, nothing that I find on this forum works that supposedly tests to see if the fie is still open, ready to be used by another process.

 

 

Note:i use FileDelete to Detect if the file open.

i have this script but it work for exe/dll:

#include <WinAPIFiles.au3>
#include <WinAPIHObj.au3>

$File = "D:\Main.exe"
_CHKFileInUse($File)
Func _CHKFileInUse($File)
    Local $ST
    For $i = 1 To StringLen($File)
        $ST = StringRight($File, $i)
        if StringInStr($ST, "\") Then
            ExitLoop
        EndIf
    Next
    FileCopy($File, @TempDir & "\" & StringReplace($ST, "\", ""), $FC_OVERWRITE + $FC_CREATEPATH)
    if FileDelete($File) = 0 Then
        Return MsgBox(64, "", "File In Use")
    Else
        Return MsgBox(64, "", "File Not In Use")
        FileCopy(@TempDir & "\" & StringReplace($ST, "\", ""), $File)
    EndIf
EndFunc   ;==>_CHKFileInUse

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

Link to comment
Share on other sites

My installation does not have WinAPIFiles.au3, but I do have WinAPI.au3

I dont have WinAIObj.au3

My version has the extensive MS Word interface and functionality for which I am fully taking advantage of.

 

Edited by AutoitMike
Link to comment
Share on other sites

Be easier to loop while looking for your process name. If that program above crashes in the middle of it you could lose your file. It’s a horrible idea to be deleting a file to tell if it’s not in use

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

I am not sure what the process name is. I am using the following code to open the file in question:

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($ReportFile)

;just some of the code here:
;add node year built and update it
    $oOther = $oXML.SelectSingleNode('//template/genInfo/GA/SM[1]')
    $Node=$oXML.CreateElement('SMT')
    $oOther.AppendChild($Node)
    $oOther = $oXML.SelectSingleNode('//template/genInfo/GA/SM[1]/SMT')
    $oOther.text=$YearBuilt
    
 $oxml.save($ReportFile)

 

 ;Now, I got to run an exe that starts up and opens this file. But what is the name of the process that would have $ReportFile open?

Edited by AutoitMike
dont want to reveal actual file data
Link to comment
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
 Share

×
×
  • Create New...