Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Select-String FileShare.ReadWrite

Reply
 
Old 06-09-2008   #1 (permalink)
Bob Bins


 
 

Select-String FileShare.ReadWrite

How come powershell cannot open files that are in use even if they have the
share flag specified? Below are exact details of the error I am getting.

Please let me know if there is a way to get around this error.

Thanks.
-----


The log file I am try to parse is already opened in another application
using the below line of code in c#.

FileStream fsLog = new FileStream(@"C:\logfiles\20080609system.log",
FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);

The below commands show the error I get when trying to open it in
powershell.

PS C:\logfiles> Select-String -pattern "1" 20080609system.log
Select-String : The file can not be read: C:\logfiles\20080609system.log
At line:1 char:14
+ Select-String <<<< -pattern "1" 20080609system.log
PS C:\logfiles> $error[0] | format-list * -force


Exception : System.ArgumentException: The file can not be read:
C:\logfiles\
20080609system.log ---> System.IO.IOException: The
process cannot access the file 'C:\
logfiles\20080609system.log' because it is being
used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode,
String maybeFullPath)



My System SpecsSystem Spec
Old 06-09-2008   #2 (permalink)
RichS [MVP]


 
 

RE: Select-String FileShare.ReadWrite

It may well be that Select-String doesn't honour the FileShare.ReadWrite and
wants exclusive access to the file

--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Bob Bins" wrote:
Quote:

> How come powershell cannot open files that are in use even if they have the
> share flag specified? Below are exact details of the error I am getting.
>
> Please let me know if there is a way to get around this error.
>
> Thanks.
> -----
>
>
> The log file I am try to parse is already opened in another application
> using the below line of code in c#.
>
> FileStream fsLog = new FileStream(@"C:\logfiles\20080609system.log",
> FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
>
> The below commands show the error I get when trying to open it in
> powershell.
>
> PS C:\logfiles> Select-String -pattern "1" 20080609system.log
> Select-String : The file can not be read: C:\logfiles\20080609system.log
> At line:1 char:14
> + Select-String <<<< -pattern "1" 20080609system.log
> PS C:\logfiles> $error[0] | format-list * -force
>
>
> Exception : System.ArgumentException: The file can not be read:
> C:\logfiles\
> 20080609system.log ---> System.IO.IOException: The
> process cannot access the file 'C:\
> logfiles\20080609system.log' because it is being
> used by another process.
> at System.IO.__Error.WinIOError(Int32 errorCode,
> String maybeFullPath)
>
>
>
My System SpecsSystem Spec
Old 06-09-2008   #3 (permalink)
Bob Bins


 
 

Re: Select-String FileShare.ReadWrite

That's exactly what I needed.

Thank you.


"Shay Levi" <no@xxxxxx> wrote in message
news:95d808932fe2f8ca987891454730@xxxxxx
Quote:

>
> You can workaround it:
>
>
> get-content C:\logfiles\20080609system.log | select-string -pattern
> "yourPattern"
>
>
>
> ---
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

>> How come powershell cannot open files that are in use even if they
>> have the share flag specified? Below are exact details of the error I
>> am getting.
>>
>> Please let me know if there is a way to get around this error.
>>
>> Thanks.
>> -----
>> The log file I am try to parse is already opened in another
>> application using the below line of code in c#.
>>
>> FileStream fsLog = new FileStream(@"C:\logfiles\20080609system.log",
>> FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
>>
>> The below commands show the error I get when trying to open it in
>> powershell.
>>
>> PS C:\logfiles> Select-String -pattern "1" 20080609system.log
>> Select-String : The file can not be read:
>> C:\logfiles\20080609system.log
>> At line:1 char:14
>> + Select-String <<<< -pattern "1" 20080609system.log
>> PS C:\logfiles> $error[0] | format-list * -force
>> Exception : System.ArgumentException: The file can not be
>> read:
>> C:\logfiles\
>> 20080609system.log ---> System.IO.IOException:
>> The
>> process cannot access the file 'C:\
>> logfiles\20080609system.log' because it is
>> being
>> used by another process.
>> at System.IO.__Error.WinIOError(Int32
>> errorCode,
>> String maybeFullPath)
>
>

My System SpecsSystem Spec
Old 06-09-2008   #4 (permalink)
Shay Levi


 
 

Re: Select-String FileShare.ReadWrite


You can workaround it:


get-content C:\logfiles\20080609system.log | select-string -pattern "yourPattern"



---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> How come powershell cannot open files that are in use even if they
> have the share flag specified? Below are exact details of the error I
> am getting.
>
> Please let me know if there is a way to get around this error.
>
> Thanks.
> -----
> The log file I am try to parse is already opened in another
> application using the below line of code in c#.
>
> FileStream fsLog = new FileStream(@"C:\logfiles\20080609system.log",
> FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
>
> The below commands show the error I get when trying to open it in
> powershell.
>
> PS C:\logfiles> Select-String -pattern "1" 20080609system.log
> Select-String : The file can not be read:
> C:\logfiles\20080609system.log
> At line:1 char:14
> + Select-String <<<< -pattern "1" 20080609system.log
> PS C:\logfiles> $error[0] | format-list * -force
> Exception : System.ArgumentException: The file can not be
> read:
> C:\logfiles\
> 20080609system.log ---> System.IO.IOException:
> The
> process cannot access the file 'C:\
> logfiles\20080609system.log' because it is
> being
> used by another process.
> at System.IO.__Error.WinIOError(Int32
> errorCode,
> String maybeFullPath)

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
can I pipe to select-string? PowerShell
problems with $var | select-string -pattern $string -q PowerShell
Re: select-string exceptions PowerShell
Re: select-string exceptions PowerShell
select-string exceptions PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46