Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Command to know if a file is open on the network

$
0
0

This may be more of what you're looking for:

function TestFileLock ($FilePath ){
    $FileLocked = $false
    $FileInfo = New-Object System.IO.FileInfo $FilePath
    trap {Set-Variable -name Filelocked -value $true -scope 1; continue}
    $FileStream = $FileInfo.Open( [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None )
    if ($FileStream) {$FileStream.Close()}
    $FileLocked
}
$File = "c:\pagefile.sys"
if (TestFileLock $File) {Write-Output "File '$File' is locked"} else {Write-Output "File '$File' is not locked"}

Viewing all articles
Browse latest Browse all 13067

Trending Articles