View Single Post
Old 05-15-2007   #5 (permalink)
char1iecha1k


 
 

Re: Open file for exclusive access

On 15 May, 13:54, Oisin Grehan <ois...@gmail.com> wrote:
> On May 15, 5:11 am, char1iecha1k <charlesgarg...@gmail.com> wrote:
>
>
>
> > On 15 May, 09:09, RichS <R...@discussions.microsoft.com> wrote:

>
> > > Is this for reading, writing or both
> > > --
> > > Richard Siddaway
> > > Please note that all scripts are supplied "as is" and with no warranty
> > > Blog:http://richardsiddaway.spaces.live.com/
> > > PowerShell User Group:http://www.get-psuguk.org.uk

>
> > > "char1iecha1k" wrote:
> > > > Hi,

>
> > > > I would like to open a file for exclusive access for the duration of a
> > > > script. If the script fails or finishes the lock must be released.
> > > > This is to prevent another user or process from accessing the same
> > > > data file. I have googled to no avail.

>
> > > > Thanks in advance

>
> > I want to open file for reading, and when open disallow any other
> > process from opening that file. So far I have this

>
> > $test1=new-object System.IO.FileStream("test1.txt",
> > [System.IO.FileMode]::Open)

>
> > Now I need to know how to return the state of the file into a variable
> > so that my script can move on to the next unopen file ie

>
> > for each file in folder open file (if $file already open then return,
> > else process job)

>
> > hope that makes sense- Hide quoted text -

>
> > - Show quoted text -

>
> Do you have control of these other scripts? If so, why not use a
> traditional ".lock" file - e.g. when you start up, create a file
> called "mydatafile.lock," and delete it when you're finished. All
> scripts should look for this while on start up, and quit if it already
> exists.


Thats the way I do it at the moment. If the script errors for some
unkown reason then the lock file doesnt get removed.

If I open a file in a script as soon as the script ends (due to an
error or natural cause) the file will close.

I have got a bit further, but there are some peculiarities. Here is
the relevant bit of the script :

&{
trap { write-host "error"; exit }
$script:test = new-object System.IO.FileStream("file.lck",
[System.IO.FileMode]::Open)
}
write-host "success"

if the file isnt opened anywhere else then there will be no error in
the script block and the file is opened.
if the file is opened in another process then creating the new object
fails and and the trap handler exits the script.
as soon as the script finishes or exits the filestream is closed

this all works but if you run that piece of code in a test batch file
and keep running it then some times it displays an error and sometimes
not. here is a sample output below

1# gc test.ps1
&{
trap { write-host "error"; exit }
$script:test = new-object System.IO.FileStream("cmdc.lck",
[System.IO.FileMode]::Open)
}
write-host "success"
2# .\test.ps1
success
3# .\test.ps1
success
4# .\test.ps1
success
5# .\test.ps1
error
6# .\test.ps1
success
7# .\test.ps1
error
8# .\test.ps1
success
9# .\test.ps1
error
10# .\test.ps1
success
11# .\test.ps1
error
12# .\test.ps1
success
13# .\test.ps1
success
14# .\test.ps1
error
15# .\test.ps1
error



My System SpecsSystem Spec