PoshCode Archive  Artifact Content

Artifact b26e25df5fa898538ad479bbec9e42837c30dc7eddbfc6eb8c690ab9b14231d5:

  • File Test-FileLock.ps1 — part of check-in [2b8162295b] at 2018-06-10 12:56:56 on branch trunk — simle function to determine is file locked by external program or not. (user: Vadims Podans size: 1032)

# encoding: ascii
# api: powershell
# title: Test-FileLock
# description: simle function to determine is file locked by external program or not.
# version: 1.0
# author: Vadims Podans
# license: CC0
# x-poshcode-id: 1364
# x-archived: 2016-06-02T09:05:15
# x-published: 2010-10-02T04:53:00
#
#
#####################################################################
# Test-FileLock.ps1
# Version 1.0
#
# test if file is locked by external program or not
#
# Vadims Podans (c) 2009
# http://www.sysadmins.lv/
#####################################################################

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