Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if file is locked

Status
Not open for further replies.

Gill1978

Programmer
Jun 12, 2001
277
GB
Hi,

I'm very new to batch file programming ... could someone tell me how I could check to see if a file is locked ... what I need is to check if a file is locked then to show an error.

Can't seem to find anything on this??

Thanks in advance

Julie
 
What I do in batch scripts is first attempt to rename a file a work file name, then do an IF NOT EXIST on the new filename.

If it is locked, you will not be able to rename the file and the IF NOT EXIST statement will GOTO an error handler.

If working interactively, this utility works well:
 
I'm not trying to unlock the file, just go to an error message or some other part of the code.

Is there no syntax for checking this?

Thanks

Julie
 
Guess you didn't understand my post.

Attempt to rename the file, it it fails then provide an error message.

Code:
SET FILEPATH=C:\....
SET FILENAME=file.txt
SET WORKFILENAME=file.wrk

RENAME %FILEPATH%%FILENAME% %WORKFILENAME%

REM Check if rename. If no file, then there was a sharing violation
IF NOT EXIST %FILEPATH%%WORKFILENAME% GOTO FILELOCKED

:FILELOCKED
ECHO FILE IS LOCKED

 
I see what you mean.

I tried the above and it didn't work. I opened the .txt file for editing ran the above code ... and it re-named the file and carried on going even though I had that file open.

That's a bit weird ...

Any other suggestions I could try?

Thanks

Julie
 
Notepad doesn't necessary keep an open file handle.

What type of locks are you trying to check for?

 
For example if the file is open for editing ... then the code should stop with an error ... is this possible???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top