473,732 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6029
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\userna me\Local Settings\Applic ation
Data\Microsoft\ Outlook'

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

os.chdir(path)

try:
shutil.copy2(sr c, 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\userna me\Local Settings\Applic ation
Data\Microsoft\ Outlook'

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

os.chdir(path)

try:
shutil.copy2(sr c, 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\userna me\Local Settings\Applic ation
Data\Microsoft\ Outlook'

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

os.chdir(path)

try:
shutil.copy2(sr c, 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(ds t, dst_bak)
try:
shutil.copy2(sr c, dst)
except IOError:
if os.path.isfile( dst_bak):
shutil.copy2(ds t_bak, dst)
os.remove(dst_b ak)
else:
try:
shutil.copy2(sr c, dst)
except IOError:
if os.path.isfile( dst_bak):
shutil.copy2(ds t_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
7326
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
3647
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 think my problem mainly lies in knowing the quirks of the Access object model. Basically, I'm looking for a method of determining if an Access database is open yet. AND I'm looking for a method that doesn't not require checking for an...
4
2765
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
11146
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 writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
6
7644
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 restart the application before the locks are released. I tried it first as: Image i = ... ; // correctly instantiated to some image i.Save("image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); // later errors doing something with image.jpg ...
3
1770
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. is there anyother way? i am using windows os.
0
1485
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 C#". Both have been developed in .NET Framework 2.0. Recently I have started getting error in Windows application that "xml file is being locked by some other process". I have debugged and find out that its my Windows Service which is locking...
7
8397
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 cannot access the file because it is being used by another process) Thanks in advance.
7
1813
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 Windows VISTA environment but I do not get this error when testing in a Windows XP environment on another computer. I close the file and even use the Dispose command to make sure all resources are released. Is this a problem with VISTA or my computer...
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.