![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Open file for exclusive access 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 |
My System Specs![]() |
| | #2 (permalink) |
| Guest | RE: Open file for exclusive access 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 > > |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Open file for exclusive access 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 |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Open file for exclusive access 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. |
My System Specs![]() |
| | #5 (permalink) |
| Guest | 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 Specs![]() |
| | #6 (permalink) |
| Guest | Re: Open file for exclusive access char1iecha1k wrote: > 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 > > > Sounds like you need to set your PSH script to continue even if there's a failure of any sorts so the lock gets created, then removed even if any commands within it fail? http://blogs.msdn.com/powershell/arc...25/583241.aspx |
My System Specs![]() |
| | #7 (permalink) |
| Guest | Re: Open file for exclusive access On 15 May, 15:19, Marco Shaw <marco.shaw@_NO_SPAM_gmail.com> wrote: > char1iecha1k wrote: > > 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 > > Sounds like you need to set your PSH script to continue even if there's > a failure of any sorts so the lock gets created, then removed even if > any commands within it fail? > > http://blogs.msdn.com/powershell/arc...25/583241.aspx I don't wont to continue if the file is already open. If the file is not open then I want to open it. If after that the script fails or finishes naturally then the I want the filestream to close, which it does most of the time, but as shown above sometimes it doesn't work properly. I have executed the script manually (once every second) to produce the output above. Even when leaving a good 5 seconds between executions it still sometimes errors. This shouldn't happen, if it should then I need to know why and how to avoid it |
My System Specs![]() |
| | #8 (permalink) |
| Guest | Re: Open file for exclusive access "char1iecha1k" <charlesgargent@gmail.com> wrote in message news:1179176871.496647.42740@l77g2000hsb.googlegroups.com... > 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 > Perhaps you should try this FileStream constructor: public FileStream ( string path, FileMode mode, FileAccess access, FileShare share ) And set the share parameter to FileShare.None. -- Keith |
My System Specs![]() |
| | #9 (permalink) |
| Guest | Re: Open file for exclusive access Keith Hill wrote: > "char1iecha1k" <charlesgargent@gmail.com> wrote in message > news:1179176871.496647.42740@l77g2000hsb.googlegroups.com... >> 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 >> > > Perhaps you should try this FileStream constructor: > > public FileStream ( > string path, > FileMode mode, > FileAccess access, > FileShare share > ) > > And set the share parameter to FileShare.None. Good suggestion, Is the FileShare.None option enough to lock the file or should one call $fileStream.Lock(0,$filestream.Length-1)? And how about unlocking the file, will dereferencing the FileStream lift the lock, or is an explicit call to unlock required/recommended? -- Joris van Lier Please note that all scripts and opinions are supplied "as is" and with no warranty Blog: http://whizzrd.spaces.live.com |
My System Specs![]() |
| | #10 (permalink) |
| Guest | Re: Open file for exclusive access If you use the FileShare parameter then the file will be locked to anyone else until the file is closed (by disposing of the FileStream). The Lock/Unlock methods allow you lock/unlock certain regions of a file where that file allows write access to others. -- Keith "Joris van Lier" <whizzrd@hotmail.com> wrote in message news:etPbqbwlHHA.4188@TK2MSFTNGP02.phx.gbl... Keith Hill wrote: > "char1iecha1k" <charlesgargent@gmail.com> wrote in message > news:1179176871.496647.42740@l77g2000hsb.googlegroups.com... >> 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 >> > > Perhaps you should try this FileStream constructor: > > public FileStream ( > string path, > FileMode mode, > FileAccess access, > FileShare share > ) > > And set the share parameter to FileShare.None. Good suggestion, Is the FileShare.None option enough to lock the file or should one call $fileStream.Lock(0,$filestream.Length-1)? And how about unlocking the file, will dereferencing the FileStream lift the lock, or is an explicit call to unlock required/recommended? -- Joris van Lier Please note that all scripts and opinions are supplied "as is" and with no warranty Blog: http://whizzrd.spaces.live.com |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to open Access in VBS and leave it open | Bob Bridges | VB Script | 16 | 08-26-2008 12:19 PM |
| Exclusive User For Media Center?! | ThÉoPatrizi | Media Center | 4 | 08-16-2008 09:46 PM |
| Error "Could Not Open The File: Access Denied" setting up print to file | ijope | General Discussion | 0 | 08-15-2008 03:20 PM |
| in vb.net how to open a file from file download prompt without askinguser to save it or run it....just open it in internet explorer | Kaustubh Budukh | .NET General | 4 | 06-23-2008 04:10 PM |
| How to open a file for read/write access in Program Files directory | Michael Harvey | Vista security | 1 | 02-28-2007 05:10 PM |