Jump to content

Test for file lock?


mdwerne
 Share

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

well is it read only?

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

#571583

...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
Link to comment
Share on other sites

#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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

  • Moderators

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:

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

 

Link to comment
Share on other sites

  • 13 years later...

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

 

Link to comment
Share on other sites

  • Developers

mmm ... do you seriously thinks the OP is still looking for an answer to a question asked 13 years ago? ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

  • Recently Browsing   0 members

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