-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDemo4.ps1
153 lines (123 loc) · 3.56 KB
/
Demo4.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#requires -version 5.0
#define some properties
#add a constructor or two
#add some methods
#enhancements and an enumeration
#added an enumeration
enum FileClass {
File
Script
Text
Office
Graphic
Executable
System
Media
}
#a helper function
Function Get-FileClass {
[cmdletbinding()]
Param([string]$Extension)
Switch -Regex ($Extension) {
"bat|ps1|psm1|psd1|ps1xml|vbs|wpf" { [fileclass]::Script }
"txt|log|xml" { [fileclass]::Text }
"do[ct](x)?|xls(x)?|p[po]t(x)?|pdf" { [fileclass]::Office }
"exe|cmd|application" { [fileclass]::Executable }
"sys|dll" { [fileclass]::System }
"bmp|jpg|png|tif|gif|jpeg" { [fileclass]::Graphic }
"mp3|wav|mp4|avi|wmf" { [fileClass]::Media }
Default { [fileclass]::File }
}
}
Class MyFileObject {
#region Properties
[ValidateNotNullorEmpty()]
[string]$Path
[string]$Name
[string]$Extension
[string]$Directory
[int]$Size
[datetime]$Created
[datetime]$Modified
[fileclass]$FileClass #<----- Added
hidden[string]$Owner #<----- Added
hidden[string]$Basename #<----- Added
#endregion
#region Methods
[timespan]GetCreatedAge() {
$result = (Get-Date) - $this.Created
Return $result
}
[timespan]GetModifiedAge() {
$result = (Get-Date) - $this.Modified
Return $result
}
[void]Refresh() {
If (Test-Path -Path $this.path) {
$item = Get-Item -Path $this.path
$this.size = $item.Length
$this.Modified = $item.LastWriteTime
}
else {
Write-Warning "Failed to find $($this.path). Cannot refresh the object."
}
}
#vvvvvvvvvvvvvvvvvvvvvvvvvv---NEW---vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
[string]GetFileType() {
$result = cmd /c assoc ".$($this.Extension)"
if ($result -match "=") {
Return $result.split("=")[1]
}
else {
return $result
}
}
[void]Zip() {
$destination = Join-Path -Path $this.Directory -ChildPath "$($this.basename).zip"
Compress-Archive -Path $this.Path -DestinationPath $destination
}
[void]Zip([string]$DestinationFolder) {
$destination = Join-Path -Path $DestinationFolder -ChildPath "$($this.basename).zip"
Compress-Archive -Path $this.Path -DestinationPath $destination
}
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#endregion
#region Constructors
MyFileObject([string]$FilePath) {
If (Test-Path -Path $Filepath) {
$item = Get-Item -Path $Filepath
$this.path = $item.fullname
$this.Name = $item.Name
$this.Extension = $item.Extension.Substring(1)
$this.size = $item.Length
$this.Created = $item.CreationTime
$this.Modified = $item.LastWriteTime
$this.Directory = $item.Directory
$this.owner = ($item | Get-ACL).owner
$this.Basename = $item.BaseName
$this.FileClass = Get-FileClass -Extension $item.Extension
}
else {
Write-Warning "Failed to find $filepath"
#don't create the object
Break
}
}
#endregion
}
cls
Return
#demo this version
$f = New-Object MyFileObject -ArgumentList .\file.txt
$f | get-member
#can use it if you know it on hidden properties
$f.owner
#display it
$f | get-member -Force -MemberType Properties
#try the new methods
$f.GetFileType()
$f.zip()
dir *.zip
#zip to a folder
$f.zip("c:\")
dir C:\*.zip