-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCheckFilesLocked.ps1
42 lines (37 loc) · 927 Bytes
/
CheckFilesLocked.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
param (
[string[]]$filePaths
)
$lockedFiles = @()
foreach ($filePath in $filePaths)
{
# Trim and skip empty or whitespace file paths
$filePath = $filePath.Trim()
if ( [string]::IsNullOrWhiteSpace($filePath))
{
continue
}
if (-Not (Test-Path $filePath))
{
Write-Host "File does not exist and will be ignored: $filePath"
continue # Skip to the next file
}
try
{
# Try to open the file in read-only mode to check if it's locked
$fileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::None)
$fileStream.Close()
}
catch
{
$lockedFiles += $filePath
}
}
if ($lockedFiles.Count -gt 0)
{
Write-Host "Locked files: $( $lockedFiles -join ', ' )"
exit 1 # One or more files are locked
}
else
{
exit 0 # No files are locked
}