473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Determining if a file is locked in Windows

I found this thread about a pst file in Windows being locked and I am
having the same issue.

http://groups.google.com/group/comp....iles%27&rnum=1

The problem is that I have a script that can find the pst files on
every machine in my network and back them up to a server for safe
keeping. The problem is that when Outlook is running it locks the file
and will not allow me to copy it to the destination. I am using the
shutil module for the copy.

Is there a way to first determine if the file is locked and then do the
copy if it isn't? I thought about looking to see if Outlook.exe is
running but the machines are shared and the process could be running
but with a different pst file in use.

Thanks in advance

Oct 18 '06 #1
6 5968
elake wrote:
I found this thread about a pst file in Windows being locked and I am
having the same issue.

http://groups.google.com/group/comp....iles%27&rnum=1

The problem is that I have a script that can find the pst files on
every machine in my network and back them up to a server for safe
keeping. The problem is that when Outlook is running it locks the file
and will not allow me to copy it to the destination. I am using the
shutil module for the copy.

Is there a way to first determine if the file is locked and then do the
copy if it isn't? I thought about looking to see if Outlook.exe is
running but the machines are shared and the process could be running
but with a different pst file in use.

Thanks in advance
Try the copy and catch the exception instead.

-Larry Bates
Oct 18 '06 #2
Larry Bates wrote:
elake wrote:
I found this thread about a pst file in Windows being locked and I am
having the same issue.
http://groups.google.com/group/comp....iles%27&rnum=1

The problem is that I have a script that can find the pst files on
every machine in my network and back them up to a server for safe
keeping. The problem is that when Outlook is running it locks the file
and will not allow me to copy it to the destination. I am using the
shutil module for the copy.

Is there a way to first determine if the file is locked and then do the
copy if it isn't? I thought about looking to see if Outlook.exe is
running but the machines are shared and the process could be running
but with a different pst file in use.

Thanks in advance
Try the copy and catch the exception instead.

-Larry Bates
Larry thanks for your suggestion. this is what I tried:

#!/usr/bin/env python

import os, shutil

path = 'c:\documents and settings\username\Local Settings\Application
Data\Microsoft\Outlook'

src = 'Outlook.pst'
dst = 'test.pst'

os.chdir(path)

try:
shutil.copy2(src, dst)
except IOError:
print 'Must be locked by Outlook'

print 'Finished'

The problem is that even though I catch the IOError it overwrites the
dst file and makes it 0kb. This is going to be for backing these files
up and it wont be good to overwrite the backup with a bad copy.

Is there another way to do this that I am missing. I am still kind of
new to Python. If i could tell that outlook had the file locked before
I tried the copy then I think that it would be prevented.

Oct 19 '06 #3
elake wrote:
Larry Bates wrote:
elake wrote:
I found this thread about a pst file in Windows being locked and I am
having the same issue.
>
http://groups.google.com/group/comp....iles%27&rnum=1
>
The problem is that I have a script that can find the pst files on
every machine in my network and back them up to a server for safe
keeping. The problem is that when Outlook is running it locks the file
and will not allow me to copy it to the destination. I am using the
shutil module for the copy.
>
Is there a way to first determine if the file is locked and then do the
copy if it isn't? I thought about looking to see if Outlook.exe is
running but the machines are shared and the process could be running
but with a different pst file in use.
>
Thanks in advance
>
Try the copy and catch the exception instead.

-Larry Bates

Larry thanks for your suggestion. this is what I tried:

#!/usr/bin/env python

import os, shutil

path = 'c:\documents and settings\username\Local Settings\Application
Data\Microsoft\Outlook'

src = 'Outlook.pst'
dst = 'test.pst'

os.chdir(path)

try:
shutil.copy2(src, dst)
except IOError:
print 'Must be locked by Outlook'

print 'Finished'

The problem is that even though I catch the IOError it overwrites the
dst file and makes it 0kb. This is going to be for backing these files
up and it wont be good to overwrite the backup with a bad copy.

Is there another way to do this that I am missing. I am still kind of
new to Python. If i could tell that outlook had the file locked before
I tried the copy then I think that it would be prevented.
maybe try and open the file for reading first, then if it opens ok,
just close it and do the copy?

Oct 19 '06 #4

MatthewWarren wrote:
elake wrote:
Larry Bates wrote:
elake wrote:
I found this thread about a pst file in Windows being locked and I am
having the same issue.
http://groups.google.com/group/comp....iles%27&rnum=1

The problem is that I have a script that can find the pst files on
every machine in my network and back them up to a server for safe
keeping. The problem is that when Outlook is running it locks the file
and will not allow me to copy it to the destination. I am using the
shutil module for the copy.

Is there a way to first determine if the file is locked and then do the
copy if it isn't? I thought about looking to see if Outlook.exe is
running but the machines are shared and the process could be running
but with a different pst file in use.

Thanks in advance

Try the copy and catch the exception instead.
>
-Larry Bates
Larry thanks for your suggestion. this is what I tried:

#!/usr/bin/env python

import os, shutil

path = 'c:\documents and settings\username\Local Settings\Application
Data\Microsoft\Outlook'

src = 'Outlook.pst'
dst = 'test.pst'

os.chdir(path)

try:
shutil.copy2(src, dst)
except IOError:
print 'Must be locked by Outlook'

print 'Finished'

The problem is that even though I catch the IOError it overwrites the
dst file and makes it 0kb. This is going to be for backing these files
up and it wont be good to overwrite the backup with a bad copy.

Is there another way to do this that I am missing. I am still kind of
new to Python. If i could tell that outlook had the file locked before
I tried the copy then I think that it would be prevented.

maybe try and open the file for reading first, then if it opens ok,
just close it and do the copy?
I tried to do that and it did let me open it without an error. Here is
what I have done now and it seems work.

def copyFile(src, dst):
if os.path.isfile(dst):
shutil.copy2(dst, dst_bak)
try:
shutil.copy2(src, dst)
except IOError:
if os.path.isfile(dst_bak):
shutil.copy2(dst_bak, dst)
os.remove(dst_bak)
else:
try:
shutil.copy2(src, dst)
except IOError:
if os.path.isfile(dst_bak):
shutil.copy2(dst_bak, dst)

It check to see if the dst file is there first and them makes a backup
of it first. That way if the copy goes bad then there is still a backup
of it. Do you see anywhere that I could have done this
better/differently?

Oct 19 '06 #5
elake wrote:
Larry Bates wrote:
elake wrote:
I found this thread about a pst file in Windows being locked and I am
having the same issue.
>
>
The problem is that even though I catch the IOError it overwrites the
dst file and makes it 0kb. This is going to be for backing these files
up and it wont be good to overwrite the backup with a bad copy.
You can try to copy to a dst file that is not the backup file, but
instead it's in a temp location or with a temp name.

If the copy fails with IOError, just delete the new 0 bytes temp file.
If the copy doesn't fail, delete the old backup, rename the new temp
file to the destination name, and you are done. The rename part
shouldn't fail since you are operating in a new file that's not locked.
And for extra safety, don't erase the old backup before renaming the
new one, but rename to .bak.old or something like that.

Good luck.

Ricardo

Oct 20 '06 #6
elake wrote:
Larry Bates wrote:
elake wrote:
I found this thread about a pst file in Windows being locked and I am
having the same issue.
>
>
The problem is that even though I catch the IOError it overwrites the
dst file and makes it 0kb. This is going to be for backing these files
up and it wont be good to overwrite the backup with a bad copy.
You can try to copy to a dst file that is not the backup file, but
instead it's in a temp location or with a temp name.

If the copy fails with IOError, just delete the new 0 bytes temp file.
If the copy doesn't fail, delete the old backup, rename the new temp
file to the destination name, and you are done. The rename part
shouldn't fail since you are operating in a new file that's not locked.
And for extra safety, don't erase the old backup before renaming the
new one, but rename to .bak.old or something like that.

Good luck.

Ricardo

Oct 20 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Pekka Niiranen | last post by:
Hi, I have used the following example from win32 extensions: -----SCRIPT STARTS---- import win32file import win32con import win32security import pywintypes
6
by: Kenneth Courville | last post by:
Hello, I'm looking for assistance with the Access object model. I know this is VB, but I'm building an Office Add-using C# directed at Access 2002. I'm literate in VB, so you can reply in VB... I...
4
by: hkappleorange | last post by:
I ued this code to connect to a mdb file A = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=C:\Inetpub\wwwroot\ASPX\Authors.mdb" )
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
6
by: JezB | last post by:
I have an Image object (i) which I want to write to a file. This does work but when I later try to do something with this file I get errors, because I think the file is still 'locked'. I have to...
3
by: Kirt | last post by:
i have a code that backsup file from src to dest. Now if some of the files are locked , i need to skip those files.. I was trying to use fctl module but it can be used only in unix i suppose. ...
0
by: JM | last post by:
I am working on an application which uses xml file as a database. The application has 2 interfaces which accesses this xml file. "Windows application written in C#" and "Windows Service written in...
7
by: Ganesh | last post by:
Hi, I would like to know if there is any way I can forcefully delete a file locked by another process on Windows as well as on Linux. On windows DeleteFile returns me error code 32 (Process...
7
by: =?Utf-8?B?UGFycm90?= | last post by:
I am receiving a "file is being used by another process" error message whenever I try to access a text file that I had previously created and then closed. This only happens when I test in a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.