Check if File is in use

Discussion in 'ASP General' started by joethis, Oct 19, 2004.

  1. joethis

    joethis Guest

    Is there a way to make sure that a file is already in use using asp? For
    instance, if one person has opened a file and is about to write to it; then
    is there a way to keep another user from reading, or writing to that text
    file until the first user is finished?
    joethis, Oct 19, 2004
    #1
    1. Advertising

  2. joethis wrote:
    > Is there a way to make sure that a file is already in use using asp?
    > For instance, if one person has opened a file and is about to write
    > to it; then is there a way to keep another user from reading, or
    > writing to that text file until the first user is finished?


    Use a database. Doing this will severely impair the scalabiltiy of your
    application because you will need to serialize this operation. One way to do
    it is as follows:

    User A renames the file. Opens it and starts updating. User B attempts to
    open file, doesn't find it and waits. User A finishes edits saves, closes
    and gives file its original name. User B renames it and opens it.

    Or do you need to allow multiple users to have it open at the same time? You
    might try a "semaphore" system:

    User A creates an editlock file and opens the file for editing. Other users
    can read the file, but no one else can create another editlock file until
    User A is finished. When user A finishes editing the file, he deletes the
    editlock file. If other users open a file which is locked for editing, set a
    flag in a global variable indicating that the file needs to re-read before
    they are allowed to lock it for editing. There are all sorts of gotchas that
    can shoot this plan down, so you have to realize that you are attempting to
    do the same thing a database management system does, i.e., you are
    reinventing the wheel. Use a database.

    Bob Barrows

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Oct 19, 2004
    #2
    1. Advertising

  3. > Is there a way to make sure that a file is already in use using asp? For
    > instance, if one person has opened a file and is about to write to it;

    then
    > is there a way to keep another user from reading, or writing to that text
    > file until the first user is finished?


    Ugh, what if the first user never finished? Leaves his browser open, locks
    his workstation, walks outside to get a coffee, and gets run over by a
    segway. Or just goes out for lunch, leaving the file locked for an hour or
    more. Do you really want web users to wait for this?

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)
    Aaron [SQL Server MVP], Oct 19, 2004
    #3
  4. joethis

    joethis Guest

    This is a log that holds errors; so if there is a database problem; then I
    would not be able to store that error. That is why I need the file locking
    capability. Thanks for the ideas though.

    "Bob Barrows [MVP]" wrote:

    > joethis wrote:
    > > Is there a way to make sure that a file is already in use using asp?
    > > For instance, if one person has opened a file and is about to write
    > > to it; then is there a way to keep another user from reading, or
    > > writing to that text file until the first user is finished?

    >
    > Use a database. Doing this will severely impair the scalabiltiy of your
    > application because you will need to serialize this operation. One way to do
    > it is as follows:
    >
    > User A renames the file. Opens it and starts updating. User B attempts to
    > open file, doesn't find it and waits. User A finishes edits saves, closes
    > and gives file its original name. User B renames it and opens it.
    >
    > Or do you need to allow multiple users to have it open at the same time? You
    > might try a "semaphore" system:
    >
    > User A creates an editlock file and opens the file for editing. Other users
    > can read the file, but no one else can create another editlock file until
    > User A is finished. When user A finishes editing the file, he deletes the
    > editlock file. If other users open a file which is locked for editing, set a
    > flag in a global variable indicating that the file needs to re-read before
    > they are allowed to lock it for editing. There are all sorts of gotchas that
    > can shoot this plan down, so you have to realize that you are attempting to
    > do the same thing a database management system does, i.e., you are
    > reinventing the wheel. Use a database.
    >
    > Bob Barrows
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    >
    >
    >
    joethis, Oct 19, 2004
    #4
  5. > This is a log that holds errors; so if there is a database problem; then I
    > would not be able to store that error. That is why I need the file locking
    > capability. Thanks for the ideas though.


    If you are having enough database errors that you need to log them *and*
    prevent multiple people from logging errors at the exact same second, I
    might suggest a better development cycle that eliminates all of these errors
    BEFORE the code is live.

    I might also suggest reserving database connection errors for e-mail alerts
    or a text log, and everything else for the database.

    A
    Aaron [SQL Server MVP], Oct 19, 2004
    #5
  6. joethis wrote:

    > This is a log that holds errors; so if there is a database problem; then I
    > would not be able to store that error. That is why I need the file locking
    > capability. Thanks for the ideas though.


    See the following thread from the archives of this newsgroup:
    http://www.google.com/groups?hl=en&...oft.public.inetserver.asp.general&btnG=Search

    In that thread, Bill James' post
    http://www.google.com/groups?hl=en&lr=lang_en&selm=edNtTewPAHA.129@cppssbbsa04
    is most informative.

    Notes
    =====
    1. You can write errors to the IIS log with the Response.AppendToLog()
    method.

    2. You could lock the Application object for the duration of the update:
    Application.Lock
    .... do file operations here...
    Application.Unlock
    ....
    but this locks out all other pages for the duration.

    You could create a separate log filename for each error and write that
    file in a subdirectory. The FileSystemObject's GetTempName() method can
    create unique filenames for you:
    http://aspfaqs.com/ASPScripts/PrintFAQ.asp?FAQID=143

    This would eliminate any conflict, since each filename would be unique.
    The contents of the error file(s) could be merged in a separate step.

    Good Luck,
    Michael D. Kersey
    Michael D. Kersey, Oct 20, 2004
    #6
  7. joethis

    Jeff Dillon Guest

    Don't write to the same file. Upon error, generate a unique filename in a
    common directory, and write to that. Yes, you'll have multiple error log
    files...common practice. Use the date/time and a unique identifier in the
    filename.

    Jeff
    "joethis" <> wrote in message
    news:...
    > This is a log that holds errors; so if there is a database problem; then I
    > would not be able to store that error. That is why I need the file locking
    > capability. Thanks for the ideas though.
    >
    > "Bob Barrows [MVP]" wrote:
    >
    > > joethis wrote:
    > > > Is there a way to make sure that a file is already in use using asp?
    > > > For instance, if one person has opened a file and is about to write
    > > > to it; then is there a way to keep another user from reading, or
    > > > writing to that text file until the first user is finished?

    > >
    > > Use a database. Doing this will severely impair the scalabiltiy of your
    > > application because you will need to serialize this operation. One way

    to do
    > > it is as follows:
    > >
    > > User A renames the file. Opens it and starts updating. User B attempts

    to
    > > open file, doesn't find it and waits. User A finishes edits saves,

    closes
    > > and gives file its original name. User B renames it and opens it.
    > >
    > > Or do you need to allow multiple users to have it open at the same time?

    You
    > > might try a "semaphore" system:
    > >
    > > User A creates an editlock file and opens the file for editing. Other

    users
    > > can read the file, but no one else can create another editlock file

    until
    > > User A is finished. When user A finishes editing the file, he deletes

    the
    > > editlock file. If other users open a file which is locked for editing,

    set a
    > > flag in a global variable indicating that the file needs to re-read

    before
    > > they are allowed to lock it for editing. There are all sorts of gotchas

    that
    > > can shoot this plan down, so you have to realize that you are attempting

    to
    > > do the same thing a database management system does, i.e., you are
    > > reinventing the wheel. Use a database.
    > >
    > > Bob Barrows
    > >
    > > --
    > > Microsoft MVP -- ASP/ASP.NET
    > > Please reply to the newsgroup. The email account listed in my From
    > > header is my spam trap, so I don't check it very often. You will get a
    > > quicker response by posting to the newsgroup.
    > >
    > >
    > >
    Jeff Dillon, Oct 20, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davisro
    Replies:
    1
    Views:
    692
    Michael D. Ober
    Jun 14, 2004
  2. mit
    Replies:
    1
    Views:
    805
    Ramu Pulipati
    Jan 25, 2006
  3. kris
    Replies:
    0
    Views:
    510
  4. kris
    Replies:
    0
    Views:
    477
  5. kris
    Replies:
    1
    Views:
    5,197
Loading...

Share This Page