473,387 Members | 1,606 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,387 software developers and data experts.

How do i find out if a file is locked?

How can I determine if the file is locked. I have looked everywhere and I get mixed answers and I can't get this going.

I know this is easy for some, but please post a quick sol.

Thanks.
Jan 13 '09 #1
34 22273
donbock
2,426 Expert 2GB
Who [what application] is locking the file in question and how is it being accomplished?

A file might be locked via a system call -- you need to tell us what operating system you're using.

Some applications perform their own ad-hoc file locking and unlocking -- you need to tell us what application.

In either case, it should be clear that file locking is idiosyncratic. There is no single portable way to determine if a file is locked.
Jan 13 '09 #2
gpraghuram
1,275 Expert 1GB
This is one of a common question and the answer is no.
There is no system call to do this

Raghu
Jan 14 '09 #3
I am running Windows XP.

The file is being locked by a 3rd party software. As far as I am concerned the application is doing the locking and unlocking.

This is what happens:
I create a project using this application. The project has x number of files. When the project is open, one file (JUST ONE) gets locked.

gpraghuram - you mentioned that there is no way to determine if a file is locked. I use winzip to zip/backup the project files at least once a day. Winzip notices that this file cannot be zipped due to the fact that it is being used.

This is the error I get from winzip after trying to zip the project files while the project is up and running.

Warning: The following file is open by another program. If that program
Warning: writes to the file while WinZip is zipping the file, the zipped
Warning: file may be corrupt: C:\Rhapsody\Model\JDW341\myscripts.vba

My question is, how does Winzip know? That is the same function/api call that I am looking for in my program that I am writing.

Do you guys have any ideas?
Jan 14 '09 #4
Banfa
9,065 Expert Mod 8TB
WinZip noticed because it tried to open the file and was unable to, you could try the same thing but the important point is that it is non-portable and just being able to open a file doesn't mean another program isn't using it.
Jan 14 '09 #5
I see what you mean.
In my case it should be ok since that file will only open by that specific program.

Thanks.
Jan 14 '09 #6
I dont think opening the file is cutting it. I open the file with no errors. I believe it might be when you try to save it.

I can't imaging Winzip trying to save every file to see if it can copy it or not. :\
Jan 14 '09 #7
Banfa
9,065 Expert Mod 8TB
try opening it for write access
Jan 14 '09 #8
this is what i have:

#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
ofstream myfile ("C:\\Rhapsody\\Model\\JDW341\\LR240.vba");

if (myfile.is_open())
{
cout << "access granted.\n";
system("PAUSE");
myfile.close();
}

else { cout << "access denied.\n"; system("PAUSE"); }
return 0;
}
Jan 14 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
You might try using actual Win32 calls to get your file access instead of just using ANS C++. As far as ANS C++ goes, if the file exists, it will open.

System Software Win32 File API
Jan 14 '09 #10
So, one way to know if a file is being used by another program is to try to rename a file. It is not the ideal way because if that file you are checking is not used by another program, than you will end up rename it.

All, I would like to have is the a function to tell me that the file is or is not being used without any changes to the file.

I tried opening the file with exclusive rights and with write only rights, and it did not help. I tried this on different types of files, including a word document.

Any help on this?
Jan 15 '09 #11
weaknessforcats
9,208 Expert Mod 8TB
Did you read the link I gave you?

CreateFile has an argument dwShareMode which if it is zero the file cannot be shared by another process. That is, if the other application has opened the file with dwShareMode of zero, then your CreateFile will fail because the file is in use. Otherwise it will not.

What this means is that there is no sure-fire way to tell if a process has a file open or not.
Jan 15 '09 #12
yes I did.

This is what I found and have so far. I tried opening the file with dwShareMode being "0", but it doesn't write anything to the file. The only time it doesnt something to the file is, when I have "GENERIC_READ | GENERIC_WRITE". Thats the only time I know my program is doing something. So, I tried this, even with my test file open, and it still goes through without giving me an INVALID_HANDLE.
#include <windows.h>
#include <stdio.h>

int main()
{
HANDLE hFile;
DWORD wmWritten;
char strData[] = "testing";

hFile = CreateFile(L"test.txt",
0,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);

if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)\n", GetLastError());
system("PAUSE");
return 0;
}

WriteFile(hFile,strData,(DWORD)(sizeof(strData)),& wmWritten,NULL);
CloseHandle(hFile);
return 0;
}
Jan 15 '09 #13
ok, im sorry, i had the parameter in the wrong place.

So, i changed the sharemode to 0 and still works. While the file is opened, it does write to the file. The only thing is, you wont see the change until you reopen the file.

I don't know why they would allow such a thing.

here is the corrected version of my code:


#include <windows.h>
#include <stdio.h>

int main()
{
HANDLE hFile;
DWORD wmWritten;
char strData[] = "testing11115";

hFile = CreateFile(L"test.txt",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);

if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)\n", GetLastError());
system("PAUSE");
return 0;
}

WriteFile(hFile,strData,(DWORD)(sizeof(strData)),& wmWritten,NULL);
CloseHandle(hFile);
return 0;
}
Jan 15 '09 #14
@gpraghuram
"No" is always a wrong answer.
Of course, you can do it with Win32 api (several methods)
See Win32 api ng and MSDN to learn win32 api.
Jan 16 '09 #15
George666,

If I were to learn the MSDN library (like you described), why would I be in this forum? It is not as easy as you think. I am here to ask advice from the experts so I can learn and save some time banging my heard against the table.

If you know an API call that can use to tell if a file is being used by another process, please by all means let me know. I have not been successful with that. I have not only looked at MSDN, but also Google has been my friend as well.

The closest Ive gotten to accomplish this task is using the CreateFile function, which so far has not been workin.
Jan 16 '09 #16
donbock
2,426 Expert 2GB
Let's start over. What are you trying to do with these files and why do you think it is helpful to explicitly check if they are locked?
Jan 16 '09 #17
Ok Don,

I am using Windows XP.

I have this modeling program. I create a project in this program. The project files are located on a network drive. Multiple users can open the project and view what is inside the project AT THE SAME TIME.

There are all these files which are associated with this project. There is one particular file that gets locked if the project is open by one or more users. I know this for sure because when I try to Zip the project files (while the project is open), WinZip warns me about this file being used by another process. If the project is closed and no user is accessing it, WinZip does not give me this warning message. Also, the program itself tells me that this file is LOCKED and it cannot be saved when the project is open by multiple users.

I am trying to write a program that will constantly monitor this file and tell me if it’s locked or not. This way I know when the project files are free or if they are in use.

That is all.

If you have any ideas, please let me know.

Thanks.
Jan 16 '09 #18
Banfa
9,065 Expert Mod 8TB
How about rather than having everyone open the files from the network you set-up a source control repository (like subversion) on the network to contain these (and other) project files and then let everyone check out a local copy of the files to work on / view.

Then the problem of someone having a file lock just doesn't occur.
Jan 16 '09 #19
Banfa,

thats in the process. this is a temp fix.
Jan 16 '09 #20
donbock
2,426 Expert 2GB
@sevak316
What is your motivation for monitoring this information? What harm does it do to let your users run wild, getting error messages when they interfere with each other?

Does this modeling program itself provide a feature to report which users have a given project open?
Jan 16 '09 #21
maybe i should let them go wild. I'll warm up some popcorn, lay back and watch the show.

the program doesnt provide info. I already looked at their log files.

I am just shocked that windows api doesnt have a simple function which return yes if the file is in use and returns falls otherwise. I have tried different api calls and non of them give me a consistant answer.
Jan 16 '09 #22
donbock
2,426 Expert 2GB
When you ask the modeling program to open a file, does it give you the choice of opening it as either read-only, shareable-edit, or exclusive-edit?
Jan 16 '09 #23
no, it doesnt.

can you please tell me why windows doesnt have a simple function which will tell you if the file is being used or not. Do you think that is an easy task to accomplish?
Jan 16 '09 #24
donbock
2,426 Expert 2GB
@sevak316
There's no answer to this existential question -- we can't read Bill Gates' mind. Don't get me started on the virtues, or lack thereof, of the Windows operating system.

I may be a little dense, but I still don't understand why you don't let your users run wild. The only inconvenience is that a user seeking to change the project will get an error message, will then have to send an email to the team asking them to exit the program, wait til he's alone and then finish changing the project. Is this really so terrible ... especially if you're working on a long-term solution via a source code repository?
Jan 16 '09 #25
The main reason is that they can replace each other. If one user is working on a different version of the project than another user, they can end up replacing each others work when they go ahead and save the project. Thats why I am trying to make this an easy task for everyone rather than sending each other e-mails to know if they can use the project, they will just have to run this program that constantly checks the project files for them.

I thought it would be a nice idea, but too bad Microsoft is not smart to come up with this function.
Jan 16 '09 #26
donbock
2,426 Expert 2GB
@sevak316
I thought you said that any number of people can open the project, but changes cannot be saved if more than one person has the project open.

If that's the case, then suppose ten people open the project, with two of them being editors intending to change it. Everybody is able to open the project without any errors or warnings. The first editor to finish his changes tries to save and gets an error. He then sends out an email to the team asking everybody to close the project. The second editor is forced to abandon his changes when he closes the project. The first editor successfully saves his changes and notifies the team that the project is open for business again.

It is no fun to have to abandon your changes, but at least one set of changes won't silently replace another.
Jan 16 '09 #27
weaknessforcats
9,208 Expert Mod 8TB
I think this is all handled by Source Depot when ties to Visual Studio. It's the source control system that allows multiple checkouts amd merge of changes on the check-in.

I think Visual SourceSafe does this also.
Jan 18 '09 #28
Donbock: there is no way for a user to abandon their changes after 4 hours of hard labor.
weaknessforcats: we are definitley going to use a source control system very soon. I just wanted a quick fix. That is all.

Thank you guys anyways, but the problem seems to be with Microsoft and their API.

-Sevak
Jan 19 '09 #29
donbock
2,426 Expert 2GB
You can allow any number of readers but you need to limit the number of editors to one. If you can do this then the worst impact is that the editor will ask all the readers to exit the application for a few minutes when he saves his changes.

You can implement quick-and-dirty low-tech edit locking by creating a token. Only the person holding the token is allowed to edit the file. The token should be something big and noticeable -- perhaps a stuffed animal. Apply appropriate punishments to anybody caught editing without the token or not returning the token when finished. You can retire the token when the source code control system is working.

Although this token approach relies on your developers following the rules; that's not much different from your notion of supplying a utility that you expect your developers to run before they open the project.
Jan 19 '09 #30
Im trying to think of the punishment that can be used if you overwrite someones work?

1. Carwash (the entire Team gets a carwash)
2. Drinks/Pizza night for the Team
3. PS3 (if you replace 5 hours (or more) of work that someone did)


PS - The files are going under source control today.
Jan 19 '09 #31
same as raghu...he is right.
Jan 20 '09 #32
It is possible!!!

Found a program which does it. Now all I have to do is to find the method this guy used to get the name!

The file upload size for the forum is to weak, so I can post a picture. However, I can give you the link to the download page of the program. It's free!

I would still like to know how this guy did it. If anyone has a clue let me know.

WhoLockMe Explorer Extension v1.04 beta (NT-Win2K-XP) download page

I would like to thank the academy, Don, Weakness, Banfa.

;)
Jan 22 '09 #33
donbock
2,426 Expert 2GB
I thought the files went under source code control a couple of days ago. Are you planning to use this WhoLockMe extension instead of source code control?
Jan 23 '09 #34
The files are under source control.

I just wanted to let you guys know that it is possible to know what user has locked a file. I just don't know which API call can make this possible.
Jan 26 '09 #35

Sign in to post your reply or Sign up for a free account.

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
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
5
by: Fabio R. | last post by:
To support a webfarm scenario, I'd like to store a global array (serialized) in a file on a network share. In this array there is a list of pages "locked" by other users so I need to read this...
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" )
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
5
by: cmay | last post by:
The only examples I have seen on how to check if a file is locked is to try to open it a catch an exception. MS has stated that you should never use error trapping in this manner. Is there no...
11
bhcob1
by: bhcob1 | last post by:
Hi, Whenever I delete a record my command button, the record deletes, a list displaying all records is updated and then a message box appears: Microsoft Access can't find the field 'I' referred to...
0
by: Dmitry Teslenko | last post by:
Hello! I use some script in python 2.5 from vim editor (it has python bindings) that updates some file and then launches another program (ms visual studio, for example) to do something with...
0
by: Gabriel Genellina | last post by:
En Tue, 13 May 2008 11:57:03 -0300, Dmitry Teslenko <dteslenko@gmail.com> escribió: Is the code above contained in a function? So all references are released upon function exit? If not, you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.