8
0
2024-01-24 16:42:38 +01:00

61 lines
1.6 KiB
PowerShell

Set-Variable -name file -Value E:\test.pdf
function checkFileStatus1($filePath)
{
write-host (get-Date) "[ACTION][FILECHECK] Checking if" $filePath "is locked"
$fileInfo = New-Object System.IO.FileInfo $filePath
try
{
$fileStream = $fileInfo.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
write-host (get-Date) "[ACTION][FILEAVAILABLE]" $filePath
#$fileInfo.close()
#$fileInfo.dispose()
return $true
}
catch
{
write-host (get-Date) "[ACTION][FILELOCKED] $filePath is locked"
return $false
}
}
function checkFileStatus2($filePath)
{
write-host (get-Date) "[ACTION][FILECHECK] Checking if" $filePath "is locked"
$fileInfo = New-Object System.IO.FileInfo $filePath
try
{
$fileStream = $fileInfo.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
#$fileStream = $fileInfo.Open()
write-host (get-Date) "[ACTION][FILEAVAILABLE]" $filePath
$fileInfo.close()
$fileInfo.dispose()
#$fileStream = $fileInfo.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
return $true
}
catch
{
write-host (get-Date) "[ACTION][FILELOCKED] $filePath is locked"
return $false
}
}
checkFileStatus2 -filePath $file
# checkFileStatus1 -filePath $file