Re: Rename file in VB 6 if not in use
From: Bonj (a_at_b.com)
Date: 03/02/04
- Next message: Bob O`Bob: "Re: IE Control Error "This service is currently disabled""
- Previous message: Steven Burn: "Re: VB 6 - Bad file name or number (Error 52) with FSO.CopyFile"
- In reply to: Azmee: "Rename file in VB 6 if not in use"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 2 Mar 2004 20:18:03 -0000
If by 'in use by others' you mean it's out to an application that opens
files without maintaining a lock on them (e.g. notepad) then you have no way
of telling whether it's in use. If, say, I create c:\a_text_file.txt and
then open it with notepad, and while it's open I rename it to
c:\a_new_text_file.txt, if notepad then saves it, the original file will
simply be recreated with the contents of the save.
If, on the other hand it's, say, an .xls file that's out to Excel, then you
can rely on the fact that Excel will have placed a lock on the file so that
it can't be modified or moved until Excel has closed the file and released
that lock The type of lock in Excel's case is the type of lock where another
application can read the file, but not write to it or rename or move it. The
application of this type of lock can be achieved by a VB program by having
the file open for write access. There is another 'stronger' type of lock,
for example that which SQL server is able to put on .mdf files, that even
prevents the file being copied or read - in SQL server's case, while the
file is attached. I'm not aware of how it's possible to cause a VB program
to apply this type of lock to a file.
In short, if you are controlling the code in the application that may be
reading the files when another one wants to rename them, then you can easily
code it to have the file open for write access when in use, such that the
program trying to rename it will go into its error trap. Otherwise, say if
you wanted the files to be read by more than one instance of your
application at once, you could get the application reading them to place a
log file in the directory that contains open files, containing information
about which files were open by who, similar to the way in which Access does
when creating .ldb files. The renaming application could then simply read
this file.
If you have no control over the application that reads them, then you will
need to find out which locking mechanism it employs. If it employs any, then
the error trapping solution will work. If you have no control over the
application that reads the files and it doesn't place locks, say such as
notepad doesn't, then you have no way of knowing which files are open - but
it thus may not matter.
"Azmee" <anonymous@discussions.microsoft.com> wrote in message
news:6C950E79-8C48-4AE5-884C-5E835DCDC24A@microsoft.com...
> I want to rename a txt file if that file is not in use by others.
> How can I know if anybody is not writing on that file.
> Can any body help me? Thanks
> /Azmee
>
- Next message: Bob O`Bob: "Re: IE Control Error "This service is currently disabled""
- Previous message: Steven Burn: "Re: VB 6 - Bad file name or number (Error 52) with FSO.CopyFile"
- In reply to: Azmee: "Rename file in VB 6 if not in use"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|