Sign in to follow this  
Followers 0
mdwerne

Test for file lock?

12 posts in this topic

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

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

well is it read only?

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
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...

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Try using FileGetAttrib

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