PoshCode Logo PowerShell Code Repository

Test-FileLock by Vadims Podans 7 years ago
View followups from Vadims Podans | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1363"></script>download | new post

simle function to determine is file locked by external program or not.

  1. filter Test-FileLock {
  2.     if ($args[0]) {$filepath = gi $(Resolve-Path $args[0]) -Force} else {$filepath = gi $_.fullname -Force}
  3.     if ($filepath.psiscontainer) {return}
  4.     $locked = $false
  5.     trap {
  6.         Set-Variable -name locked -value $true -scope 1
  7.         continue
  8.     }
  9.     $inputStream = New-Object system.IO.StreamReader $filepath
  10.     if ($inputStream) {$inputStream.Close()}
  11.     @{$filepath = $locked}
  12. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

Remember me