function Test-FileLocked([string]$FilePath) {
try { [IO.File]::OpenWrite($FilePath).close(); return $false }
catch { return $true }
}
function Test-AnyFileLocked([string]$DirPath) {
$files = Get-ChildItem $DirPath -Recurse
foreach ($file in $files) {
if (Test-FileLocked (Convert-Path $file.PSPath)) {
return $true
}
}
return $false
}
if (Test-FileLocked $filePath){
Write-Warning "File in use."
}
if (Test-AnyFileLocked $dirPath){
Write-Warning "Some files in use."
}
本文链接: https://www.pstips.net/check-if-a-file-is-locked.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!