Jump to content

Recommended Posts

Posted

Hello,

Is there anyway with Autoit to test if a file is locked and by whom (what process)?

I'm trying to replace some log files (all xml) in a folder and sometimes the replacement works fine, and sometimes it fails and one of the files has not been replaced. That's why I need a suggestion regarding my question above.

Thanks,

-Mike

Posted (edited)

  NerdFencer said:

Try using FileGetAttrib

Hi NerdFencer,

Thanks a lot for the suggestion. Sadly I tested all the attribs and Archive is the only one that came back as true. When I try and manually delete the file, I get an error that the action can't be completed because the file is open in another program.

Any other thoughts/suggestions?

-Mike

Edited by mdwerne
Posted

well is it read only?

filegetattrib() if 'R' then....
or what do you mean by LOCKED?

Posted

Restart your computer and delete it. Occasionally things you close , the computer believes is still running in the background.

If its something to the extent of Process Manager ( CTRL + ALT + DEL ) than just close it there.

But I have this happen sometimes =/. Its usually just things running in the background.

Posted (edited)

  bob00037 said:

well is it read only?

filegetattrib() if 'R' then....
or what do you mean by LOCKED?
Hi Bob,

Sorry for my lack of clarification. The file is not necessarily locked from writing (attrib = R) it is locked (open) by a running process/service so I can't delete or replace it . My question was really twofold.

1) How can query/test a single file to see if it is locked (or not) by "something" so that I can determine if I can make changes to it. Specifically delete it.

2) If I can tell if the file is locked, can I determine what process or service has it locked. That way maybe I can kill the process or stop the service temporarily and make my changes.

Here is a rough example using some imaginary functions...

$fileinquestion = ("C:\tempfile.xml")

if _IsFileLocked($fileinquestion) = 1 then

$processinquestion = _ProcessOfLockedFile($fileinquestion)

ProcessClose($processinquestion)

FileDelete($fileinquestion)

else

FileDelete($fileinquestion)

endif

I hope all this helps to clarify my question. So far all my searches of the forum and Google have come up null with regard to testing a file for a locked(open/in use) state.

Thanks for the reply.

-Mike

Edited by mdwerne
Posted

  Drew said:

Restart your computer and delete it. Occasionally things you close , the computer believes is still running in the background.

If its something to the extent of Process Manager ( CTRL + ALT + DEL ) than just close it there.

But I have this happen sometimes =/. Its usually just things running in the background.

Hi Drew,

Yeah...unfortunately it's more than just a reboot. Whatever process has the strangle hold may or may not be running at any given point. Upon reboot, the same situation will exist. Your right about killing the process, but my goal is to test the file programatically and not have to do anything by hand.

Thanks for the input!

-Mike

Posted (edited)

  mdwerne said:

Is there anyway with Autoit to test if a file is locked...

#571583

  mdwerne said:

...and by whom (what process)?

Not that I know of any AU method, try http://ccollomb.free.fr/unlocker/ , maybe you can use the dll files (for private versions only of course :lmao: )....

or

When you know the file is locked, try to delete it and Unlocker will tell you the process... then in your script check for _FileInUse() and kill the process determined with Unlocker :) ...

Edit: Changed post of _FileInUse()

Edited by KaFu
Posted

  KaFu said:

#571583

Not that I know of any AU method, try http://ccollomb.free.fr/unlocker/ , maybe you can use the dll files (for private versions only of course :lmao: )....

or

When you know the file is locked, try to delete it and Unlocker will tell you the process... then in your script check for _FileInUse() and kill the process determined with Unlocker :) ...

Edit: Changed post of _FileInUse()

KaFu,

The example by Siao in your first link worked PERFECTLY! Gave me a return value that I can work with. I'm hesitant to use a third party product. I'd really like to do this all in Autoit. But for testing sake...I will give it a shot. Thanks for the tip!!

-Mike

Posted

  mdwerne said:

I'm hesitant to use a third party product. I'd really like to do this all in Autoit.

Just use it to determine the process... to kill with AU later on :)... this works at least if it's always the same and only one process...

  • Moderators
Posted

KaFu,

My thanks too. I searched for something like this in the forums but never found it. If you do not use the correct terms to search for it can be really frustrating at times!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

  • 13 years later...
Posted

You could also try:

    Local $iFileExists = FileExists($sFilePath)

    Local $hFileLockTest = FileOpen($filename, $FO_READ)

    if $iFileExists = 1 And $hFileLockTest = -1 Then

          ; this file is locked!

    Else

         FileClose($hFileLockTest)

         FileDelete($filename)

         ; from FileDelete: 'Some file attributes can make the deletion impossible, if this is the case look at FileSetAttrib() to change the attributes of a file.'

    EndIf

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...