-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDemo3.ps1
91 lines (69 loc) · 1.73 KB
/
Demo3.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
#requires -version 5.0
#define some properties
#add a constructor or two
#add some methods
Class MyFileObject {
#region Properties
[ValidateNotNullorEmpty()]
[string]$Path
[string]$Name
[string]$Extension
[string]$Directory
[int]$Size
[datetime]$Created
[datetime]$Modified
#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."
}
}
#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
}
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
$f | get-member
$f.GetCreatedAge()
$f.GetModifiedAge()
$f.GetModifiedAge().ToString()
# modify file.txt
add-content -Value "this is something else" -Path .\file.txt
$f.Refresh()
$f
$f.GetModifiedAge().ToString()