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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Locking source and destination files

Status
Not open for further replies.

evergreean43

Technical User
Joined
May 25, 2006
Messages
165
Location
US
I have a small script that works where it copies a file from one Windows server to another. The destination file is always overwritten.

Now I think I should check if the file I am copying from (sourceFile) is opened by someone and also check if the file I am copying to (destinationFile) is being used.

Here is my current copy command:
Code:
Use File::Copy
...
copy($sourceFile,$destinationFile) ||  die "$!";


Please advise how I can do this because I am not sure about opening it up and how flock works?
Code:
open(INPUT, "<$sourceFile");  
@data = <INPUT>;        
close(INPUT);            
open (OUTPUT, ">$sourceFile"); 
flock(OUTPUT, 2);        
copy($sourceFile,$destinationFile) ||  die "$!";
 
Hi,

On a windows machine you'll find that the open() function when called with a '> like that will fail if the file exists and is already in use.

Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Should I even bother with the lock attempt?

I am copying an Access 2000 database and it seems to copy even if someone is already using it on the destination and source ends.
 
I wouldn't no - just check the return value of the open() function

Mike

The options are: fast, cheap and right - pick any two. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top