forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Join-FileName.ps1
50 lines (39 loc) · 1.14 KB
/
Join-FileName.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
43
44
45
46
47
48
49
50
<#
.SYNOPSIS
Combines a filename with a string.
.DESCRIPTION
Join-FileName appends a string to a filename, including a new extension
overwrites the filename's extension.
.INPUTS
System.String file path.
.OUTPUTS
System.String file path with appended text.
.FUNCTIONALITY
Files
.EXAMPLE
Join-FileName.ps1 activity.log '-20161111'
activity-20161111.log
.EXAMPLE
Join-FileName.ps1 readme.txt .bak
readme.bak
.EXAMPLE
Join-FileName.ps1 C:\temp\activity.log .27.old
C:\temp\activity.27.old
#>
[CmdletBinding()][OutputType([string])] Param(
# The path to a file.
[Parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)][string] $Path,
# Text to append to the filename, either before the extension or including one.
[Parameter(Mandatory=$true,Position=1)][Alias('Extension')][string] $AppendText
)
Process
{
if($AppendText -like '.*') { [IO.Path]::ChangeExtension($Path,$AppendText) }
else
{
$name = (Split-Path $Path -LeafBase) + $AppendText
if($AppendText -notlike '*.*'){ $name += Split-Path $Path -Extension }
if(Split-Path $Path) { Join-Path (Split-Path $Path) $name }
else { $name }
}
}