Jump to content

Recommended Posts

Posted

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

 

Posted
Posted (edited)

 

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.

Posted (edited)

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
Posted

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

 

Posted (edited)

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
Posted

The process name is the name of the exe you run that modifies the file. You can have a while loop that scans for it’s existence 

My resources are limited. You must ask the right questions

 

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
×
×
  • Create New...