-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGet-Files.psm1
67 lines (58 loc) · 2.02 KB
/
Get-Files.psm1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#requires -version 3
function Get-Files
{
[CmdletBinding()]
param
(
[Parameter(Position=0, Mandatory=$true)] [string[]]$inputs,
[Parameter(Mandatory=$false)] [string]$match = ".*",
[Parameter(Mandatory=$false)] [string]$matchDesc = "",
[Parameter(Mandatory=$false)] [switch]$acceptFiles = $true,
[Parameter(Mandatory=$false)] [switch]$acceptFolders,
[Parameter(Mandatory=$false)] [switch]$recurse
)
$inFilesAll = @()
foreach ($input in $inputs)
{
$isDirectory = Test-Path -LiteralPath $input -PathType Container
$isFile = Test-Path -LiteralPath $input -PathType Leaf
if ($isDirectory -and !$acceptFolders)
{
throw "Error: input must not be a directory."
}
elseif ($isFile -and !$acceptFiles)
{
throw "Error: input must be a directory."
}
# Workaround for Get-Childitem bug
if(!($input -match '`'))
{ $inputEsc = [System.Management.Automation.WildcardPattern]::Escape($input) } else { $inputEsc = $input }
try
{
$inFiles = Get-ChildItem -Recurse:($recurse -and $isDirectory) -LiteralPath ([System.Management.Automation.WildcardPattern]::Unescape($inputEsc)) -ErrorAction Stop `
| ?{ $_ -match $match }
}
catch
{
$msg = "Error: Failed processing $input"
if($_.Exception.GetType().Name -eq "ItemNotFoundException")
{
$msg += ": File or Directory not found"
}
else { $msg += ".`nError Message: $($_.Exception.Message)" }
throw $msg
}
if (!$inFiles)
{
if(Test-Path $input -pathType container)
{ Write-Host "Notice: No $matchDesc files found in $input." -ForegroundColor Gray}
else
{
throw "Error: $input is not a $matchDesc file."
}
}
$inFilesAll += $inFiles
}
return $inFilesAll
}
Export-ModuleMember Get-Files