forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileNameTools.ps1
128 lines (114 loc) · 4.08 KB
/
FileNameTools.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Function New-RandomFileName {
[cmdletbinding(DefaultParameterSetName="none")]
Param(
[parameter(Position = 0)]
[Parameter(ParameterSetName = 'none')]
[Parameter(ParameterSetName = 'home')]
[Parameter(ParameterSetName = 'temp')]
#enter an extension without the leading period e.g 'bak'
[string]$Extension,
[Parameter(ParameterSetName = 'temp')]
[alias("temp")]
[Switch]$UseTempFolder,
[Parameter(ParameterSetName = 'home')]
[alias("home")]
[Switch]$UseHomeFolder
)
if ($UseTempFolder) {
$filename = [system.io.path]::GetTempFileName()
}
elseif ($UseHomeFolder) {
if ($PSVersionTable.PSEdition -eq 'Core' -AND $PSVersionTable.OS -notmatch "Windows") {
$filename = Join-Path -Path $env:HOME -ChildPath ([system.io.path]::GetRandomFileName())
}
else {
$filename = Join-Path -Path $env:USERPROFILE\Documents -ChildPath ([system.io.path]::GetRandomFileName())
}
}
else {
$filename = [system.io.path]::GetRandomFileName()
}
if ($Extension) {
#get the extension and strip off the leading period
$original = [system.io.path]::GetExtension($filename).Substring(1)
$filename -replace "$original$", $Extension
}
else {
$filename
}
} #end New-RandomFilename
Function New-CustomFileName {
[cmdletbinding()]
Param (
[Parameter(Position = 0,Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Template,
[ValidateSet("Lower", "Upper", "Default")]
[string]$Case = "default"
)
#convert placeholders to lower case but leave everything else as is
[regex]$rx="%\w+(?=%|-|\.|\s|\(|\)|\[|\])"
Write-Detail "Starting $($myinvocation.MyCommand)" | Write-Verbose
Write-Detail "Processing template: $template" | Write-Verbose
$rx.matches($Template) | foreach-object {
Write-Detail "Converting $($_.value) to lower case" | Write-Verbose
$Template = $Template.replace($_.value,$_.value.tolower())
}
[string]$filename = $Template
Write-Detail "Using filename: $filename" | Write-Verbose
$now = Get-Date
if ($env:USERNAME) {
$user = $env:USERNAME
}
elseif ($env:USER) {
$user = $env:USER
}
else {
$user = "Unknown"
}
#this needs to be an ordered hashtable so that the regex replacements
#will be processed in the right order
$hash = [ordered]@{
'%username' = $user
'%computername' = (hostname)
'%year' = $now.Year
'%yr' = "{0:yy}" -f $now
'%monthname' = ("{0:MMM}" -f $now)
'%month' = $now.month
'%dayofweek' = $now.DayOfWeek
'%day' = $now.Day
'%hour' = $now.hour
'%minute' = $now.minute
'%time' = "{0}{1}{2}" -f $now.hour, $now.minute, $now.Second
'%string' = ([system.io.path]::GetRandomFileName()).split(".")[0]
'%guid' = [System.Guid]::NewGuid().guid
}
$hash.GetEnumerator() | foreach-object {
Write-Detail "Testing $filename for $($_.key)" | Write-Verbose
if ($filename -match "($($_.key))") {
Write-Detail "replacing $($_.key) with $($_.value)" | Write-Verbose
$filename = $filename -replace "($($_.key))",$_.value
}
}
#handle ### number replacement
[regex]$rx = '%#+'
if ($rx.IsMatch($filename)) {
$count = $rx.Match($filename).Value.length -1
$num = (0..9 | Get-Random -Count 10 | Get-Random -count $count) -join ""
Write-Detail "replacing # with $num" | Write-Verbose
$filename = $rx.Replace($filename,$num)
}
Write-Detail "Converting case to $Case" | Write-Verbose
Switch ($Case) {
"Upper" {
$filename.toUpper()
}
"Lower" {
$filename.ToLower()
}
default {
$filename
}
} #close switch
Write-Detail "Ending $($myinvocation.MyCommand)" | Write-Verbose
} #end New-CustomFileName