469,352 Members | 2,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

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 21144
donbock
2,422 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,064 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,064 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,422 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,064 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,422 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,422 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,422 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,422 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,422 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,422 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

Post your reply

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

Similar topics

6 posts views Thread by Pekka Niiranen | last post: by
5 posts views Thread by Fabio R. | last post: by
4 posts views Thread by hkappleorange | last post: by
5 posts views Thread by cmay | last post: by
reply views Thread by Dmitry Teslenko | last post: by
reply views Thread by Gabriel Genellina | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.