forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplit-FileName.ps1
More file actions
50 lines (40 loc) · 1.46 KB
/
Copy pathSplit-FileName.ps1
File metadata and controls
50 lines (40 loc) · 1.46 KB
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
<#
.SYNOPSIS
Returns the specified part of the filename.
.DESCRIPTION
Split-FileName returns only the specified part of a filename:
either the filename without extension (default) or extension.
It can also tell whether the filename has an extension.
.INPUTS
System.String file path to parse.
.OUTPUTS
System.String containing the base file name (or extension),
or System.Boolean if the -HasAttribute switch is present.
.EXAMPLE
Split-FileName.ps1 readme.txt
readme
.EXAMPLE
Split-FileName.ps1 readme.txt -Extension
․txt
(the leading . is included, but can't be entered as such in this example)
.EXAMPLE
Split-FileName.ps1 readme.txt -HasExtension
True
#>
[CmdletBinding(DefaultParameterSetName='__AllParameterSets')][OutputType([string])][OutputType([bool],ParameterSetName='HasExtension')] Param(
# A file path to extract a part of; the base name without extension by default.
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string] $Path,
<#
Indicates that the path should be checked for the presence of an extension,
returning a boolean value.
#>
[Parameter(ParameterSetName='HasExtension')][switch] $HasExtension,
# Indicates the path's extension should be returned.
[Parameter(ParameterSetName='Extension')][switch] $Extension
)
Process
{
if($HasExtension) { [IO.Path]::HasExtension($Path) }
elseif($Extension) { [IO.Path]::GetExtension($Path) }
else { [IO.Path]::GetFileNameWithoutExtension($Path) }
}