Open a file in use by another program

0

I am trying to access, by means of a powershell code inserted in a bat, to a log file created by another program but it gives me the following error:

Exception when calling "OpenRead" with arguments "1": "The process can not get access to the file 'C: \ Users \ g \ Documents \ BAT \ GrabI.log' because it is being used in another process.

The powershell code used is the following:

@PowerShell  ^
    $N = 200; ^
    $fpath = 'Grab%1.log';  ^
    $fs = [System.IO.File]::OpenRead($fpath);  ^
    $fs.Seek(-$N, 'End') ^| Out-Null;  ^
    $mystr = '';  ^
    for ($i = 0; $i -lt $N; $i++)  ^
    {  ^
        $mystr = ($mystr) + ([char[]]($fs.ReadByte()));  ^
    }  ^
    Write-Host $mystr > logtmp%1.log
%End PowerShell%

Is there any way to do the Open that allows me to access the file in read mode even if it is in use?

    
asked by GGG 29.06.2017 в 21:22
source

1 answer

0

Although on this page I have not had much "audience", in the matrix there have been people who have tried to lend me a hand. But in the end I have found the solution to my needs by resorting to the Get-Content function instead of trying to open and process the log file. This is the code that has worked for me:

@PowerShell  ^
    $N = 200; ^
    $mystr = Get-Content 'Grab%1.log' ^| Select-Object -last 1;  ^
    Write-Host $mystr.substring($mystr.length-$N,$N) > logtmp%1.log
%End PowerShell%
    
answered by 02.07.2017 в 08:29