How to know when a source file is LOCKED.

General Macro Scheduler discussion

Moderators: JRL, Dorian (MJT support)

Post Reply
ThomasAJ

How to know when a source file is LOCKED.

Post by ThomasAJ » Tue Dec 28, 2004 2:14 am

My source file in CopyFile is sometimes locked and cannot be copied.
How can I find out if it is locked and so prevent the CopyFile from going ahead?

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Dec 28, 2004 3:41 am

It may depend on how the file is locked.

If it is made into ReadOnly, then this could work.
Read the Attributes of the file, then read the file and parse for results:

UNTESTED Structure to build on:
//Write value of attributes to a text file
Run Program>command.com /c ATTRIB c:\folder\filename.ext > c:\temp\attrib.txt

//Make the text file a variable
ReadLn>c:\Temp\attrib.txt,Attrib

//Check for position of "R", I think it is in position 1
MidStr>%Attrib%,1,1,ReadOnly

//Split macro path depending on value of attribute
IF>%ReadOnly%=R,Locked

//File is not locked, continue to copy file
Goto>End

Label>Locked
//File is locked, cannot copy file

Label>End
If it is like an Access database, then look for the existence of an "ldb" file with the same name as the database, like "DatabaseName.ldb". Different applications will follow a similar approach, making a locking file, then deleting it when no longer locked.

In that case use IFFileExists:
IfFileExists>DataBaseName.ldb,Locked

//File is not locked, continue to copy file
Goto>End

Label>Locked
//File is locked, cannot copy file

Label>End
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Tue Dec 28, 2004 8:53 am

Or if it is locked for writing a method that has been used many times before is to try to write a dummy record to it. WriteLn will generate an error of the file is locked. So you just keep trying until you don't get an error:

Label>wloop
WriteLn>filename,r,
If>r=##ERR##,wloop
MJT Net Support
[email protected]

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts