By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

Detect file opened by Notepad

P: n/a
Don

I wrote an app that alerts a user who attempts to open a file that the
file is currently in use. It works fine except when the file is
opened by Notepad. If a text file is opened, most computers are
configured to use Notepad to open the file by default and if they are
configured to use Notepad by default I want it to remain that way
rather than retrieve the text into my app or force the user to use
another app to read the file. I'm using the following code I found
online and lErrNum returns a value of zero if Notepad has the file
open but returns 70 when most other files are open using another
application.

Don

Open sPathFile For Input Lock Read As #iFileNum
Close iFileNum
lErrNum = Err.Number
On Error GoTo 0

Select Case lErrNum
Case 0 'No error occurred. File is NOT already open by another
user.
FileIsOpen = False
Case 70 'Error number for "Permission Denied." File is already
opened by another user.
FileIsOpen = True
Case 53 'File not found
FileIsOpen = False
Case Else 'Another error occurred.
FileIsOpen = True
MsgBox Error(lErrNum)
End Select
Sep 13 '08 #1
Share this Question
Share on Google+
36 Replies


P: n/a
Notepad opens the file, load it in memory, then close it immediately. You
can open the file exclusively, or use FindFirstChangeNotification() to see
if the user edited and saved the file outside your process, then prompt the
user if he wants to reload the file. See this VB6 sample:

http://vbnet.mvps.org/code/fileapi/watchedfolder.htm


Sep 13 '08 #2

P: n/a
Don
Thanks for the response, but I need something that detects that the
file is currently open. Since the file is immediately closed after
reading the contents as you described, it sounds like what I'm asking
isn't possible. I'm trying to prevent users from having the file open
at the same time and making changes not aware that two or more people
are making changes that might conflict. Every method I can think of
either won't detect the file is open preventing the problem or detects
that a change was made after it was too late to prevent it.

Don

On Sat, 13 Sep 2008 14:10:09 -0400, "expvb" <no****@cox.netwrote:
>Notepad opens the file, load it in memory, then close it immediately. You
can open the file exclusively, or use FindFirstChangeNotification() to see
if the user edited and saved the file outside your process, then prompt the
user if he wants to reload the file. See this VB6 sample:

http://vbnet.mvps.org/code/fileapi/watchedfolder.htm

Sep 13 '08 #3

P: n/a
ds*****@yahoo.com (Don) wrote in news:48*************@news.west.cox.net:
Thanks for the response, but I need something that detects that the
file is currently open.
1

But the file is NOT currently open. It is opened only for milliseconds
while Notepad reads it then is closed again immediately.
Since the file is immediately closed after
reading the contents as you described, it sounds like what I'm asking
isn't possible.
That is correct.
I'm trying to prevent users from having the file open
at the same time and making changes not aware that two or more people
are making changes that might conflict. Every method I can think of
either won't detect the file is open
Goto 1

Sep 13 '08 #4

P: n/a
dpb
Don wrote:
Thanks for the response, but I need something that detects that the
file is currently open. ...
You can only detect it open while it is open...which means you have only
the fleetest of intervals during which time you can find that
out--otherwise, as you've learned, the file _isn't_ open.

You can't prevent it unless you restrict users to an app that is
single-user-at-a-time aware or put the file into a version control
system w/ single-user checkout/login features or some similar solution.

--
Sep 13 '08 #5

P: n/a
Don
On Sat, 13 Sep 2008 21:01:58 +0000 (UTC), DanS
<t.***************@a.d.e.l.p.h.i.a.n.e.twrote:
>ds*****@yahoo.com (Don) wrote in news:48*************@news.west.cox.net:
>Thanks for the response, but I need something that detects that the
file is currently open.

1

But the file is NOT currently open. It is opened only for milliseconds
while Notepad reads it then is closed again immediately.
Yes, that's what was explained in the first response. But I was
already aware that the contents of Notepad are read into memory and
the file closed. I should have worded it differently.

Seems odd that there isn't a way to prevent the problem of multiple
users working on the same file and creating the conflicts that are
occuring other than to use another app to do so.

Most all apps we use to retrieve various files will either lock the
file or using FileIsOpen will detect that the file is in use. I'm
surprised that Notepad works the way it does and opens up this
possibilty that several people can open and alter the same file
simultaneously not aware of what all others are doing at that given
moment. I'm certain they are not aware this is happening so I'll have
to advise them this is occurring.

I plan to put in my app a detour that if the user selects a file that
launches Notepad by default, another app will open instead or I will
use the Rich Textbox or other means to display the text file and lock
the file to prevent others from accessing it. Didn't want to do that,
but I guess I have no choice but to see that they avoid using Notepad.

Don
>
> Since the file is immediately closed after
reading the contents as you described, it sounds like what I'm asking
isn't possible.

That is correct.
>I'm trying to prevent users from having the file open
at the same time and making changes not aware that two or more people
are making changes that might conflict. Every method I can think of
either won't detect the file is open

Goto 1
Sep 13 '08 #6

P: n/a
On Sat, 13 Sep 2008 20:23:38 GMT, ds*****@yahoo.com (Don) wrote:
>Thanks for the response, but I need something that detects that the
file is currently open. Since the file is immediately closed after
reading the contents as you described, it sounds like what I'm asking
isn't possible.
Yep, not possible.
I'm trying to prevent users from having the file open
at the same time and making changes not aware that two or more people
are making changes that might conflict. Every method I can think of
either won't detect the file is open preventing the problem or detects
that a change was made after it was too late to prevent it.
That old 'between a rock and a hard place' comes to mind. Consider
this, can you perhaps run through the windows of open applications
looking for Notepad, and seeing what file that window has open? That
*might* work, but would be Notepad specific. I'm sure there are other
programs that may present the same problems, too, however.
>
Don

On Sat, 13 Sep 2008 14:10:09 -0400, "expvb" <no****@cox.netwrote:
>>Notepad opens the file, load it in memory, then close it immediately. You
can open the file exclusively, or use FindFirstChangeNotification() to see
if the user edited and saved the file outside your process, then prompt the
user if he wants to reload the file. See this VB6 sample:

http://vbnet.mvps.org/code/fileapi/watchedfolder.htm

Sep 13 '08 #7

P: n/a

"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
>
I wrote an app that alerts a user who attempts to open a file that the
file is currently in use. It works fine except when the file is
opened by Notepad. If a text file is opened, most computers are
configured to use Notepad to open the file by default and if they are
configured to use Notepad by default I want it to remain that way
rather than retrieve the text into my app or force the user to use
another app to read the file. I'm using the following code I found
online and lErrNum returns a value of zero if Notepad has the file
open but returns 70 when most other files are open using another
application.

Don
You might try and detect if the file has been changed since you last chewed
on it (or while you are chewing on it)

Take a look at FindFirstChangeNotification, FindNextChangeNotification.

-ralph
Sep 13 '08 #8

P: n/a
Don
On Sat, 13 Sep 2008 16:55:08 -0500, "Ralph"
<nt*************@yahoo.comwrote:
>
"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
>>
I wrote an app that alerts a user who attempts to open a file that the
file is currently in use. It works fine except when the file is
opened by Notepad. If a text file is opened, most computers are
configured to use Notepad to open the file by default and if they are
configured to use Notepad by default I want it to remain that way
rather than retrieve the text into my app or force the user to use
another app to read the file. I'm using the following code I found
online and lErrNum returns a value of zero if Notepad has the file
open but returns 70 when most other files are open using another
application.

Don

You might try and detect if the file has been changed since you last chewed
on it (or while you are chewing on it)

Take a look at FindFirstChangeNotification, FindNextChangeNotification.

-ralph

I saw that mentioned in the first response and although that would not
work, I took a look at it. It can certainly be a handy tool, but I
need to detect that the file is currently open to prevent another user
from using it which just can't be done. One possibility I might look
into is whether I can search all workstations to determine if that
particular file is open at any particular workstation on the network.
That would work, but I think I"m just dreaming here.
Don

Sep 13 '08 #9

P: n/a
Don
On Sat, 13 Sep 2008 17:53:38 -0400, PeterD <pe****@hipson.netwrote:
>On Sat, 13 Sep 2008 20:23:38 GMT, ds*****@yahoo.com (Don) wrote:
>>Thanks for the response, but I need something that detects that the
file is currently open. Since the file is immediately closed after
reading the contents as you described, it sounds like what I'm asking
isn't possible.

Yep, not possible.
>I'm trying to prevent users from having the file open
at the same time and making changes not aware that two or more people
are making changes that might conflict. Every method I can think of
either won't detect the file is open preventing the problem or detects
that a change was made after it was too late to prevent it.

That old 'between a rock and a hard place' comes to mind. Consider
this, can you perhaps run through the windows of open applications
looking for Notepad, and seeing what file that window has open? That
*might* work, but would be Notepad specific. I'm sure there are other
programs that may present the same problems, too, however.
That actually has possibilties. I do know how to enumerate through
all open windows and even target a Notepad window and it's caption,
but I would have to do so by accessing all workstations on the network
until I find one that is running that file via Notepad. Seems like
that would be the challenge of my lifetime but I might just try
researching that one.
Don
>
>>
Don

On Sat, 13 Sep 2008 14:10:09 -0400, "expvb" <no****@cox.netwrote:
>>>Notepad opens the file, load it in memory, then close it immediately. You
can open the file exclusively, or use FindFirstChangeNotification() to see
if the user edited and saved the file outside your process, then prompt the
user if he wants to reload the file. See this VB6 sample:

http://vbnet.mvps.org/code/fileapi/watchedfolder.htm

Sep 13 '08 #10

P: n/a

"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
On Sat, 13 Sep 2008 16:55:08 -0500, "Ralph"
<nt*************@yahoo.comwrote:
I saw that mentioned in the first response and although that would not
work, I took a look at it. It can certainly be a handy tool, but I
need to detect that the file is currently open to prevent another user
from using it which just can't be done. One possibility I might look
into is whether I can search all workstations to determine if that
particular file is open at any particular workstation on the network.
That would work, but I think I"m just dreaming here.
Don
Pretty much. <g>

I didn't follow exvb's link so wasn't aware he already mentioned it. It
wouldn't help in the case where a User opened this file after you opened it,
and then only closed it (with changes) after your app had closed.

Here's what I would do in a situation like this, back up a step. Forget for
the moment HOW to do something and re-think why I feel I need to do it.

Maybe you need to provide your own 'editor' and otherwise protect the data?

-ralph
Sep 13 '08 #11

P: n/a
If you explain the underlying problem in more details, maybe some posters
here can suggest alternatives. One way is adding Edit button in a toolbar so
it can be easily opened with the program of your choice. Another, is to make
a copy of the file into another folder, then start Windows Explorer with
that folder shown by default. Search MSKB for "explorer command line
options".

But the best option seems to be making a new extension, and perhaps add a
block of binary data at the beginning of the file to discourage editing.
Sep 13 '08 #12

P: n/a
On Sat, 13 Sep 2008 22:17:45 GMT, ds*****@yahoo.com (Don) wrote:
>On Sat, 13 Sep 2008 17:53:38 -0400, PeterD <pe****@hipson.netwrote:
>>That old 'between a rock and a hard place' comes to mind. Consider
this, can you perhaps run through the windows of open applications
looking for Notepad, and seeing what file that window has open? That
*might* work, but would be Notepad specific. I'm sure there are other
programs that may present the same problems, too, however.

That actually has possibilties. I do know how to enumerate through
all open windows and even target a Notepad window and it's caption,
but I would have to do so by accessing all workstations on the network
until I find one that is running that file via Notepad.
Ouch... Yea, you're screwed. Maybe permissions is an answer. Only your
application has write permissions for the file?
>Seems like
that would be the challenge of my lifetime but I might just try
researching that one.
Don
Sep 14 '08 #13

P: n/a
Don
On Sun, 14 Sep 2008 08:50:24 -0400, PeterD <pe****@hipson.netwrote:
>On Sat, 13 Sep 2008 22:17:45 GMT, ds*****@yahoo.com (Don) wrote:
>>On Sat, 13 Sep 2008 17:53:38 -0400, PeterD <pe****@hipson.netwrote:
>>>That old 'between a rock and a hard place' comes to mind. Consider
this, can you perhaps run through the windows of open applications
looking for Notepad, and seeing what file that window has open? That
*might* work, but would be Notepad specific. I'm sure there are other
programs that may present the same problems, too, however.

That actually has possibilties. I do know how to enumerate through
all open windows and even target a Notepad window and it's caption,
but I would have to do so by accessing all workstations on the network
until I find one that is running that file via Notepad.

Ouch... Yea, you're screwed. Maybe permissions is an answer. Only your
application has write permissions for the file?
Considering that, also. Might keep all files in folders with specific
rights made available only if they access the files through my app or
make the folders invisible except via the app.

I think the answer is for my app to detect that they are attempting to
open a file with Notepad and create a detour, causing the file to open
in my app or an app I know will lock the file. My app already does
that, but I didn't want to have to do that.
>
>>Seems like
that would be the challenge of my lifetime but I might just try
researching that one.
Don
Sep 14 '08 #14

P: n/a
Don
On Sat, 13 Sep 2008 19:13:55 -0400, "expvb" <no****@cox.netwrote:
>If you explain the underlying problem in more details, maybe some posters
here can suggest alternatives. One way is adding Edit button in a toolbar so
it can be easily opened with the program of your choice. Another, is to make
a copy of the file into another folder, then start Windows Explorer with
that folder shown by default. Search MSKB for "explorer command line
options".

But the best option seems to be making a new extension, and perhaps add a
block of binary data at the beginning of the file to discourage editing.

I've tried all those methods except for the last, but the bottom line
is that I'll just create a detour when my app detects that Notepad is
the app that is about to open the file and instead the file will open
in my app or an app of my choice.

The problem with the new extension or binary data is that I'm
concerned what might be left behind if the program is unexpectedly
closed and someone attempts to access files outside my app. I plan to
get around that by making the folders invisible or changing the access
rights, but the detour idea does work and I guess that's how I'll
proceed - as long as I also make those folders inaccessible to users
other than through my app.
Sep 14 '08 #15

P: n/a
Don
On Sat, 13 Sep 2008 17:36:17 -0500, "Ralph"
<nt*************@yahoo.comwrote:
>
"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
>On Sat, 13 Sep 2008 16:55:08 -0500, "Ralph"
<nt*************@yahoo.comwrote:
>
I saw that mentioned in the first response and although that would not
work, I took a look at it. It can certainly be a handy tool, but I
need to detect that the file is currently open to prevent another user
from using it which just can't be done. One possibility I might look
into is whether I can search all workstations to determine if that
particular file is open at any particular workstation on the network.
That would work, but I think I"m just dreaming here.
Don

Pretty much. <g>

I didn't follow exvb's link so wasn't aware he already mentioned it. It
wouldn't help in the case where a User opened this file after you opened it,
and then only closed it (with changes) after your app had closed.

Here's what I would do in a situation like this, back up a step. Forget for
the moment HOW to do something and re-think why I feel I need to do it.

Maybe you need to provide your own 'editor' and otherwise protect the data?
And that's EXACTLY what I believe to be the answer. I think that's
where I stand and either place a Rich Text box in my app for that
purpose or create a detour so that if Notepad is detected as the
default app about to lanuch the file, I can instead launch an app I
know would work.
Don
>
-ralph

Sep 14 '08 #16

P: n/a
Don wrote:
"Ralph" wrote:
>>
Maybe you need to provide your own 'editor' and otherwise protect
the data?

And that's EXACTLY what I believe to be the answer. I think that's
where I stand and either place a Rich Text box in my app for that
purpose or create a detour so that if Notepad is detected as the
default app about to lanuch the file, I can instead launch an app I
know would work.
Don
I haven't read this whole thread, but in your shoes I would tug at
this from the other end. Open the file yourself with exclusive access:
lock out all writes except your own.

If someone has the file already open, your open will fail and you can
deal with it as you like.

In the case of Notepad having the file open, since the file isn't
"really" open, you'll still be able to gain exclusive access. But if
the Notepad user tries to save her copy, she won't be able to. She'll
have to save under a different name or folder. So you're covered
either way.

--
Jim Mack
MicroDexterity Inc
www.microdexterity.com

Sep 14 '08 #17

P: n/a
Don
On Sun, 14 Sep 2008 12:24:27 -0400, "Jim Mack" <jm***@mdxi.nospam.com>
wrote:
>Don wrote:
>"Ralph" wrote:
>>>
Maybe you need to provide your own 'editor' and otherwise protect
the data?

And that's EXACTLY what I believe to be the answer. I think that's
where I stand and either place a Rich Text box in my app for that
purpose or create a detour so that if Notepad is detected as the
default app about to lanuch the file, I can instead launch an app I
know would work.
Don

I haven't read this whole thread, but in your shoes I would tug at
this from the other end. Open the file yourself with exclusive access:
lock out all writes except your own.
This was my original plan. I just wanted to avoid that and find a way
to detect that Notepad had the file open which I now know can't be
done. But . . . (seem my comment following your last comment)
>
If someone has the file already open, your open will fail and you can
deal with it as you like.

In the case of Notepad having the file open, since the file isn't
"really" open, you'll still be able to gain exclusive access. But if
the Notepad user tries to save her copy, she won't be able to. She'll
have to save under a different name or folder. So you're covered
either way.
This is exactly the problem we're having and why I needed to find a
way to solve the problem. Users are opening files, making changes,
then they can't save them because someone else has the file open. But
I found a solution to that as long as it isn't Notepad that has the
file open (or previously open) for reasons already discussed. I like
the idea of exclusive access, but my fear is what might happen if my
app is unexpectedly closed leaving a file open. I'm not familiar with
exclusive access so I'll look into it. If that exclusiveness is
released in such a case, then that can be a solution to my problem.
I'm assuming you mean that Notepad can be used to open a file with
exclusive access. If so, I didn't know that and I'll research that.
Others in this thread have mentioned that and I just sort of tossed
the idea aside because of my concerns of the file not reverting back
after a user is finished with it or if the program terminates
unexpectedly. I'll check this out.
Don
>
--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
Sep 14 '08 #18

P: n/a
Don wrote:
On Sun, 14 Sep 2008 12:24:27 -0400, "Jim Mack" <jm***@mdxi.nospam.com>
wrote:
>I haven't read this whole thread, but in your shoes I would tug at
this from the other end. Open the file yourself with exclusive
access: lock out all writes except your own.

This was my original plan. I just wanted to avoid that and find a way
to detect that Notepad had the file open which I now know can't be
done. But . . . (seem my comment following your last comment)
>>
Like Jim, I haven't read the whole thread. But it occurs to me that the
situation you describe is somewhat unusual. You apparently have text files in
shared locations that more than one user may try to edit at the same time. I
wonder what sort of file that is, and why it is in a shared location.

Given that there is a reason for it, it is the case that two users could then
"open" (meaning read) the same file using Notepad. User A could then make a
change and save, then user B could make a change and save, over-writing anything
user A did. Since both users have only used Notepad, this has nothing to do with
your app, and everything to do with the decision to put text files in a shared
location that may be simultaneously edited by multiple users using Notepad.

If that functionality is really needed, I would suggest developing a little app
for editing text files that does lock the file during the edit, and distributing
it to the users. Rather than change the registration of .txt files, you can
simply instruct the users that if they want a "safe" text editor that will avoid
collisions, they should explicitly start your text editor program instead of
double-clicking the file.

Either that, or make your app treat text files the same way Notepad does - read
them and close, then reopen and overwrite to save changes. That would make it do
the same thing as when two users edit the same file using Notepad, which is
apparently the current situation.
Sep 14 '08 #19

P: n/a
"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
I'm assuming you mean that Notepad can be used to open a file with
exclusive access.
Not sure what you mean, but if you open the file exclusively, like the
following, Notepad can't even read it:

Option Explicit

Dim f As Long

Private Sub Command1_Click()
f = FreeFile
Open App.Path & "\Test.txt" For Binary Access Read Write Lock Read Write As
f

End Sub

Private Sub Command2_Click()
Close f
End Sub

Private Sub Form_Unload(Cancel As Integer)
If f <0 Then
Close f
End If
End Sub
Sep 14 '08 #20

P: n/a
"expvb" <no****@cox.netwrote in message
news:uJ*************@TK2MSFTNGP06.phx.gbl...
"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
>I'm assuming you mean that Notepad can be used to open a file with
exclusive access.

Not sure what you mean, but if you open the file exclusively, like the
following, Notepad can't even read it:

Option Explicit

Dim f As Long

Private Sub Command1_Click()
f = FreeFile
Open App.Path & "\Test.txt" For Binary Access Read Write Lock Read Write
As f

End Sub

Private Sub Command2_Click()
Close f
I forgot to add the following line here, so the file number is not closed
again in Form_Unload:

f = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
If f <0 Then
Close f
End If
End Sub


Sep 14 '08 #21

P: n/a
dpb
Don wrote:
....
This is exactly the problem we're having and why I needed to find a
way to solve the problem. Users are opening files, making changes,
then they can't save them because someone else has the file open. ...
Version Control System

--
Sep 14 '08 #22

P: n/a
On Sun, 14 Sep 2008 15:19:34 GMT, ds*****@yahoo.com (Don) wrote:
>On Sun, 14 Sep 2008 08:50:24 -0400, PeterD <pe****@hipson.netwrote:
>>On Sat, 13 Sep 2008 22:17:45 GMT, ds*****@yahoo.com (Don) wrote:
>>>On Sat, 13 Sep 2008 17:53:38 -0400, PeterD <pe****@hipson.netwrote:

That old 'between a rock and a hard place' comes to mind. Consider
this, can you perhaps run through the windows of open applications
looking for Notepad, and seeing what file that window has open? That
*might* work, but would be Notepad specific. I'm sure there are other
programs that may present the same problems, too, however.

That actually has possibilties. I do know how to enumerate through
all open windows and even target a Notepad window and it's caption,
but I would have to do so by accessing all workstations on the network
until I find one that is running that file via Notepad.

Ouch... Yea, you're screwed. Maybe permissions is an answer. Only your
application has write permissions for the file?

Considering that, also. Might keep all files in folders with specific
rights made available only if they access the files through my app or
make the folders invisible except via the app.

I think the answer is for my app to detect that they are attempting to
open a file with Notepad and create a detour, causing the file to open
in my app or an app I know will lock the file. My app already does
that, but I didn't want to have to do that.
What extension are you using for your file? If you are using a
commonly defined extension (.TXT, .DAT, etc.) you may find you are
headed down a rocky road. You probably should:

1. create your own unique extension (remember, you are not limited to
three characters.) Search to make sure your extension is reasonably
unique.

2. Setup Windows so that when a file with that extension is clicked in
Explorer, it opens in your program, not notepad.
>
>>
>>>Seems like
that would be the challenge of my lifetime but I might just try
researching that one.
Don
Sep 14 '08 #23

P: n/a
Don
On Sun, 14 Sep 2008 17:53:29 -0400, PeterD <pe****@hipson.netwrote:
>On Sun, 14 Sep 2008 15:19:34 GMT, ds*****@yahoo.com (Don) wrote:
>>On Sun, 14 Sep 2008 08:50:24 -0400, PeterD <pe****@hipson.netwrote:
>>>On Sat, 13 Sep 2008 22:17:45 GMT, ds*****@yahoo.com (Don) wrote:

On Sat, 13 Sep 2008 17:53:38 -0400, PeterD <pe****@hipson.netwrote:

>That old 'between a rock and a hard place' comes to mind. Consider
>this, can you perhaps run through the windows of open applications
>looking for Notepad, and seeing what file that window has open? That
>*might* work, but would be Notepad specific. I'm sure there are other
>programs that may present the same problems, too, however.

That actually has possibilties. I do know how to enumerate through
all open windows and even target a Notepad window and it's caption,
but I would have to do so by accessing all workstations on the network
until I find one that is running that file via Notepad.

Ouch... Yea, you're screwed. Maybe permissions is an answer. Only your
application has write permissions for the file?

Considering that, also. Might keep all files in folders with specific
rights made available only if they access the files through my app or
make the folders invisible except via the app.

I think the answer is for my app to detect that they are attempting to
open a file with Notepad and create a detour, causing the file to open
in my app or an app I know will lock the file. My app already does
that, but I didn't want to have to do that.

What extension are you using for your file? If you are using a
commonly defined extension (.TXT, .DAT, etc.) you may find you are
headed down a rocky road. You probably should:

1. create your own unique extension (remember, you are not limited to
three characters.) Search to make sure your extension is reasonably
unique.

2. Setup Windows so that when a file with that extension is clicked in
Explorer, it opens in your program, not notepad.
I considered that and I think that can work. When the user selects
that particular file, my app can rename the file as you suggest. We
receive so many files from so many sources it would have to be done
this way rather than renaming every file as we receive them manually.

But my concern is that the files would always have to remain with
their new extension or renamed back after the user is done and if the
program ends unexpectedly, there could be a problem with the file left
named with the new extension rather than the .txt extension. I'm
thinking down the road how we might end up if we have text files left
with two different extensions.

I'm still inclined to set up my app to detour a user to another app
that does keep the file open or present a pop-up warning if they
choose to allow Notepad to open the file, they could be making changes
while others are doing the same at that very moment.
>>
>>>
Seems like
that would be the challenge of my lifetime but I might just try
researching that one.
Don
Sep 15 '08 #24

P: n/a
Don
On Sun, 14 Sep 2008 11:09:53 -0700, "Steve Gerrard"
<my********@comcast.netwrote:
>Don wrote:
>On Sun, 14 Sep 2008 12:24:27 -0400, "Jim Mack" <jm***@mdxi.nospam.com>
wrote:
>>I haven't read this whole thread, but in your shoes I would tug at
this from the other end. Open the file yourself with exclusive
access: lock out all writes except your own.

This was my original plan. I just wanted to avoid that and find a way
to detect that Notepad had the file open which I now know can't be
done. But . . . (seem my comment following your last comment)
>>>

Like Jim, I haven't read the whole thread. But it occurs to me that the
situation you describe is somewhat unusual. You apparently have text files in
shared locations that more than one user may try to edit at the same time. I
wonder what sort of file that is, and why it is in a shared location.
As discussed in this thread, putting the files in a non-shared
location won't solve the problem. The problem is that Notepad
immediately closes the file after reading in the text so it is no
longer open and therefore available for use again even though the file
is open on a workstation and can be saved back causing the overwriting
condition as described previously.
Don
>Given that there is a reason for it, it is the case that two users could then
"open" (meaning read) the same file using Notepad. User A could then make a
change and save, then user B could make a change and save, over-writing anything
user A did. Since both users have only used Notepad, this has nothing to do with
your app, and everything to do with the decision to put text files in a shared
location that may be simultaneously edited by multiple users using Notepad.
Same as above as I see it.
Don
>If that functionality is really needed, I would suggest developing a little app
for editing text files that does lock the file during the edit, and distributing
it to the users. Rather than change the registration of .txt files, you can
simply instruct the users that if they want a "safe" text editor that will avoid
collisions, they should explicitly start your text editor program instead of
double-clicking the file.
That is how my app originally functioned and the purpose of this
thread. Since Notepad is a default on most systems I wanted to take
advantage of allowing the user to remain using Notepad, but prevent
other users from accessing the file.
Don
>Either that, or make your app treat text files the same way Notepad does - read
them and close, then reopen and overwrite to save changes. That would make it do
the same thing as when two users edit the same file using Notepad, which is
apparently the current situation.
Again, that's what my app can currently do and that might be how I end
up proceeding. But I'm more inclined to offer the user an option when
my app detects that it's a .txt file and give them a choice of Notepad
with a warning as previously described, offer a selection of other
apps that do keep the file open, or retrieve the file into my app in a
Rich Textbox with even more functions than Notepad provides.
Don
>
Sep 15 '08 #25

P: n/a
Don
On Sun, 14 Sep 2008 14:14:07 -0400, "expvb" <no****@cox.netwrote:
>"expvb" <no****@cox.netwrote in message
news:uJ*************@TK2MSFTNGP06.phx.gbl...
>"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
>>I'm assuming you mean that Notepad can be used to open a file with
exclusive access.

Not sure what you mean, but if you open the file exclusively, like the
following, Notepad can't even read it:

Option Explicit

Dim f As Long

Private Sub Command1_Click()
f = FreeFile
Open App.Path & "\Test.txt" For Binary Access Read Write Lock Read Write
As f

End Sub

Private Sub Command2_Click()
Close f

I forgot to add the following line here, so the file number is not closed
again in Form_Unload:

f = 0
>End Sub

Private Sub Form_Unload(Cancel As Integer)
If f <0 Then
Close f
End If
End Sub

This is how I've set up my apps in the past to read and work with
files and this would work, but it's what I wanted to avoid. I wanted
users to be able to work with whatever is the default app for .txt
files. Notepad is pretty much the overwhelming default app and one
that closes the file too quickly.

So I will probably go back to exactly what you are suggesting above or
create a detour to another app I konw they have on their system that
will open a text file and keep the file open.

My hope to find a way to do this allowing Notepad to retrieve the file
just ain't gonna happen I guess.

By the way, I generally close text files immediately just like Notepad
when a user uses my apps to read a file. I see the pros, but now I
also see the cons.

Don

Sep 15 '08 #26

P: n/a
dpb
Don wrote:
....
But my concern is that the files would always have to remain with
their new extension or renamed back after the user is done and if the
program ends unexpectedly, there could be a problem with the file left
named with the new extension rather than the .txt extension. I'm
thinking down the road how we might end up if we have text files left
with two different extensions.

I'm still inclined to set up my app to detour a user to another app
that does keep the file open or present a pop-up warning if they
choose to allow Notepad to open the file, they could be making changes
while others are doing the same at that very moment.
....

Have I yet mentioned what you're looking for is a version control system
for these files???

--
Sep 15 '08 #27

P: n/a

"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
On Sun, 14 Sep 2008 14:14:07 -0400, "expvb" <no****@cox.netwrote:
<cut>
So I will probably go back to exactly what you are suggesting above or
create a detour to another app I konw they have on their system that
will open a text file and keep the file open.
How about opening the file exclusively from your app, copying the contents
to a temp file, shelling the default editor on that copy and waiting for it
to terminate, then updating the original and deleting the temp file?

Sep 15 '08 #28

P: n/a
On Mon, 15 Sep 2008 12:31:00 GMT, ds*****@yahoo.com (Don) wrote:
>On Sun, 14 Sep 2008 17:53:29 -0400, PeterD <pe****@hipson.netwrote:
>>On Sun, 14 Sep 2008 15:19:34 GMT, ds*****@yahoo.com (Don) wrote:
>>>On Sun, 14 Sep 2008 08:50:24 -0400, PeterD <pe****@hipson.netwrote:

On Sat, 13 Sep 2008 22:17:45 GMT, ds*****@yahoo.com (Don) wrote:

>On Sat, 13 Sep 2008 17:53:38 -0400, PeterD <pe****@hipson.netwrote:
>
>>That old 'between a rock and a hard place' comes to mind. Consider
>>this, can you perhaps run through the windows of open applications
>>looking for Notepad, and seeing what file that window has open? That
>>*might* work, but would be Notepad specific. I'm sure there are other
>>programs that may present the same problems, too, however.
>
>That actually has possibilties. I do know how to enumerate through
>all open windows and even target a Notepad window and it's caption,
>but I would have to do so by accessing all workstations on the network
>until I find one that is running that file via Notepad.

Ouch... Yea, you're screwed. Maybe permissions is an answer. Only your
application has write permissions for the file?

Considering that, also. Might keep all files in folders with specific
rights made available only if they access the files through my app or
make the folders invisible except via the app.

I think the answer is for my app to detect that they are attempting to
open a file with Notepad and create a detour, causing the file to open
in my app or an app I know will lock the file. My app already does
that, but I didn't want to have to do that.

What extension are you using for your file? If you are using a
commonly defined extension (.TXT, .DAT, etc.) you may find you are
headed down a rocky road. You probably should:

1. create your own unique extension (remember, you are not limited to
three characters.) Search to make sure your extension is reasonably
unique.

2. Setup Windows so that when a file with that extension is clicked in
Explorer, it opens in your program, not notepad.

I considered that and I think that can work. When the user selects
that particular file, my app can rename the file as you suggest.
Rename *what* file? No where did I suggest renaming files. What file
are you opening? A .TXT file?
We
receive so many files from so many sources it would have to be done
this way rather than renaming every file as we receive them manually.
OK, so what is the purpose of this exercise?
>
But my concern is that the files would always have to remain with
their new extension or renamed back after the user is done and if the
program ends unexpectedly, there could be a problem with the file left
named with the new extension rather than the .txt extension. I'm
thinking down the road how we might end up if we have text files left
with two different extensions.
Renaming is a very bad idea... Copying is also bad... Both can and
will create problems.
>
I'm still inclined to set up my app to detour a user to another app
that does keep the file open or present a pop-up warning if they
choose to allow Notepad to open the file, they could be making changes
while others are doing the same at that very moment.
Since I'm totally confused as to what you are trying to achieve, I'm
out of ideas. I only know that it appears that you want something that
(IMHO) is impossible.
>
>>>

>Seems like
>that would be the challenge of my lifetime but I might just try
>researching that one.
>Don
Sep 15 '08 #29

P: n/a

"Bob Butler" <no***@nospam.everwrote in message
news:On**************@TK2MSFTNGP06.phx.gbl...
>
"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
On Sun, 14 Sep 2008 14:14:07 -0400, "expvb" <no****@cox.netwrote:
<cut>
So I will probably go back to exactly what you are suggesting above or
create a detour to another app I konw they have on their system that
will open a text file and keep the file open.

How about opening the file exclusively from your app, copying the contents
to a temp file, shelling the default editor on that copy and waiting for
it
to terminate, then updating the original and deleting the temp file?
Hi Bob!

Long time no read. We've missed your sager.

-ralph
Sep 15 '08 #30

P: n/a
Don wrote:
I wrote an app that alerts a user who attempts to open a file that the
file is currently in use. It works fine except when the file is
opened by Notepad. If a text file is opened, most computers are
configured to use Notepad to open the file by default and if they are
configured to use Notepad by default I want it to remain that way
rather than retrieve the text into my app or force the user to use
another app to read the file. I'm using the following code I found
online and lErrNum returns a value of zero if Notepad has the file
open but returns 70 when most other files are open using another
application.

Don

Open sPathFile For Input Lock Read As #iFileNum
Close iFileNum
lErrNum = Err.Number
On Error GoTo 0

Select Case lErrNum
Case 0 'No error occurred. File is NOT already open by another
user.
FileIsOpen = False
Case 70 'Error number for "Permission Denied." File is already
opened by another user.
FileIsOpen = True
Case 53 'File not found
FileIsOpen = False
Case Else 'Another error occurred.
FileIsOpen = True
MsgBox Error(lErrNum)
End Select
As mentioned numerous times through this thread, notepad doesn't keep
the file open.
You may be able to do it with a tiny stub exe that opens the file, then
shells out to notepad and waits for the process to exit.
That will break if they then use that notepad process to open other files.

--
Dean Earley (de*********@icode.co.uk)
i-Catcher Development Team

iCode Systems
Sep 15 '08 #31

P: n/a
Don
On Mon, 15 Sep 2008 06:06:51 -0700, "Bob Butler" <no***@nospam.ever>
wrote:
>
"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
>On Sun, 14 Sep 2008 14:14:07 -0400, "expvb" <no****@cox.netwrote:
<cut>
>So I will probably go back to exactly what you are suggesting above or
create a detour to another app I konw they have on their system that
will open a text file and keep the file open.

How about opening the file exclusively from your app, copying the contents
to a temp file, shelling the default editor on that copy and waiting for it
to terminate, then updating the original and deleting the temp file?
Hey, I like that.

I was already using a simlar function to work on a temp file, but I
was using my own app and I wasn't locking the original with my own app
while doing so. But to allow the user to use Notepad, if that's their
default app for a text file, and monitor its use so the original file
remains locked during the interim sounds interesting.

Although this has great possibilites, I'm not sure how confident I
will be using it due to concerns I mentioned previously regarding the
consequences that might result if the program shuts down unexpectedly
and we're left with the original file locked . . .OR would the file
detect that my app has shut down and revert back to being available?

Although I have decided to either detour the user to another app or
use my own app to read the file and either of those solves the
problem, I'm going to have to give your suggestion some thought. I
have a long ride to work so I have a long way to think about this.
Don

Sep 15 '08 #32

P: n/a
Don
Although it has absolutely NO relevence to this thread, in researching
ways to get around my problem with Notepad I discovered something I
didn't know.

If you type "Bush hid the facts" (no quotes and no period) in Notepad
and save the file, when you open the file you won't see the phrase. I
won't spoil it by giving the explanation here, but you can Google your
way to the explanation. Try Googling "Things you can't type in
Notepad" and I think you'll find it.

Don
On Sat, 13 Sep 2008 17:28:13 GMT, ds*****@yahoo.com (Don) wrote:
>
I wrote an app that alerts a user who attempts to open a file that the
file is currently in use. It works fine except when the file is
opened by Notepad. If a text file is opened, most computers are
configured to use Notepad to open the file by default and if they are
configured to use Notepad by default I want it to remain that way
rather than retrieve the text into my app or force the user to use
another app to read the file. I'm using the following code I found
online and lErrNum returns a value of zero if Notepad has the file
open but returns 70 when most other files are open using another
application.

Don

Open sPathFile For Input Lock Read As #iFileNum
Close iFileNum
lErrNum = Err.Number
On Error GoTo 0

Select Case lErrNum
Case 0 'No error occurred. File is NOT already open by another
user.
FileIsOpen = False
Case 70 'Error number for "Permission Denied." File is already
opened by another user.
FileIsOpen = True
Case 53 'File not found
FileIsOpen = False
Case Else 'Another error occurred.
FileIsOpen = True
MsgBox Error(lErrNum)
End Select

Sep 15 '08 #33

P: n/a
dpb
PeterD wrote:
....
Rename *what* file? No where did I suggest renaming files. What file
are you opening? A .TXT file?
....

I think multiple suggestions are getting mixed up...iirc somebody did
suggest renaming in some fashion would allow an individual user to make
changes. Of course, that raises the issue of then having to try to
somehow merge the changes or simply ignoring some over the acceptance of
others.
>receive so many files from so many sources it would have to be done
this way rather than renaming every file as we receive them manually.

OK, so what is the purpose of this exercise?
Absolutely no clues to that have been offered, but it's clear they need
to check each received file into a version control system and only
retrieve/use files from that repository via a single-user-at-a-time
option for check out for modifications although allowing multiple
viewing might be desirable.

With that, I'm now going to mark the thread "ignore"...

....

--
Sep 15 '08 #34

P: n/a
"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
Although this has great possibilites, I'm not sure how confident I
will be using it due to concerns I mentioned previously regarding the
consequences that might result if the program shuts down unexpectedly
and we're left with the original file locked . . .OR would the file
detect that my app has shut down and revert back to being available?
If there is a crash or your app aborts for any reason then recovery would
depend on whether you've saved the state of "user x is editing text y in
location z" somewhere. If you didn't then any changes are lost and if you
did you can decide to restart the editing session (maybe asking the user in
case it's an endless loop).

If the user's text editor crashes then you should see it terminate and can
take precautions -- maybe check the return value of the editor app and if
not 0 then ask the user to confirm that it is OK to update.... maybe compare
the before & after versions and if the sizes aren't close ask the user to
confirm... nothing is ever going to be foolproof so it's up to you to
decide how many hoops you want to jump through and how many you want to
force your users through.

As soon as you do this you're going to find users editing the text on the
originals while your app is not running. Unless you're encrypting the text
and putting it in a difficult-to-find location the users are going to find a
way around any restrictions you set. Even if you do those things somebody
probably still will. *&%^&^%^& users!

Sep 15 '08 #35

P: n/a
Don wrote:
>
That is how my app originally functioned and the purpose of this
thread. Since Notepad is a default on most systems I wanted to take
advantage of allowing the user to remain using Notepad, but prevent
other users from accessing the file.
Don
But two users can both use Notepad on the same file. Your app has nothing to do
with it. That is the problem.
Sep 15 '08 #36

P: n/a
"Don" <ds*****@yahoo.comwrote in message
news:48**************@news.west.cox.net...
If you type "Bush hid the facts" (no quotes and no period) in
Notepad and save the file, when you open the file you won't
see the phrase. I won't spoil it by giving the explanation here,
but you can Google your way to the explanation. Try Googling
"Things you can't type in Notepad" and I think you'll find it.
Actually I can't reproduce that problem in the version of NotedPad that
comes with Vista, either on that phrase or on any of the other phrases I've
just discovered on the Internet. I must make a mental note to try it on
other systems though when I get the time. Never come across it before.
Sounds vaguely interesting :-)

Mike


Sep 16 '08 #37

This discussion thread is closed

Replies have been disabled for this discussion.