-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFile.ps1
More file actions
199 lines (170 loc) · 6.15 KB
/
File.ps1
File metadata and controls
199 lines (170 loc) · 6.15 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env pwsh
# Helper utilities for dealing with file paths and other file system related tasks
# https://github.com/ajgorhoe/IGLib.modules.IGLibScriptsPS.git
# Execute definitions from other files:
. "$(Join-Path "$PSScriptRoot" "Common.ps1")"
# Check whether the current script has already been executed before:
CheckScriptExecuted $ExecutedScriptPath_File $MyInvocation.MyCommand.Path;
# Store scritp path in a variable in order to enable later verifications:
$ExecutedScriptPath_File = $MyInvocation.MyCommand.Path
# Auxiliary definitions:
Set-Alias print Write-Host
Set-Alias alias Set-Alias
Set-Alias aliases Get-Alias
Set-Alias ScripttDir GetScriptDirectory
Set-Alias CurrentDir GetCurrentDirectory
Set-Alias FullPath GetAbsolutePath
Set-Alias IsFile FileExists
Set-Alias DirExists DirectoryExists
Set-Alias IsDir DirectoryExists
Set-Alias AbsolutePath GetAbsolutePath
Set-Alias FileName GetFileName
Set-Alias GetFileOrDirectoryName GetFileName
Set-Alias ParentDir GetParentDirectory
Set-Alias RootDir GetRootDirectory
Set-Alias CopyDir CopyDirectoryRecursive
Set-Alias RemoveDir RemoveDirectoryRecursive
Set-Alias Files GetFilesRecursively
Set-Alias FilesRun RunOnFilesRecursively
Set-Alias OnFiles RunOnFilesRecursively
function GetScriptDirectoryIGLib() { $PSScriptRoot }
function GetScriptDirectory()
{ $MyInvocation.MyCommand.Path }
function GetCurrentDirectory() { return $(Get-Location).Path }
function DirectoryExists(<#[system.string]#> $DirectoryPath = $null)
{
if ("$DirectoryPath" -eq "") { return $false; }
return Test-Path "$DirectoryPath" -PathType Container
}
function FileExists(<#[system.string]#> $FilePath = $null)
{
if ("$FilePath" -eq "") { return $false; }
return Test-Path "$FilePath" -PathType Leaf
}
function PathExists($Path = $null)
{
if ("$Path" -eq "") { return $false; }
return Test-Path "$Path"
}
function GetAbsolutePath($Path = $null)
{
if ("$Path" -eq "") { $Path = "."; }
return $(Resolve-Path "$Path").Path;
}
<#
.Synopsis
Returns the filaname part of the specific file path.
.Description
Returns the filaname part of the specific file path. This function does not
check whether the $Path parameter actually contains a path of an existing
file, and operates merely on path-as-string basis. For example, if $Path
is a path of an existing directory, then this function will just return the
name of the last level of the directory.
.Parameter Path
Specifies the file name.
#>
function GetFileName($Path = ".")
{
if ("$Path" -eq "") { $Path = "." }
if (PathExists($Path))
{
$Path = GetAbsolutePath($Path);
}
return Split-Path "$Path" -Leaf
}
function GetParentDirectory($Path = ".")
{
if ("$Path" -eq "") { $Path = "." }
if (PathExists($Path))
{
return Split-Path "$Path" -Resolve
}
return Split-Path "$Path"
}
function GetRootDirectory($Path = $null, $ChildPath = $null)
{
# print GetRootDirectory called with:
# print " Path: $Path"
# print " ChildPath: $ChildPath"
if ("$Path" -eq "" -and "$ChildPath" -eq "") { $Path = "."; }
if ($(PathExists("$Path")) -or $true)
{
$ParentPath = GetParentDirectory($Path);
# print "Path: $Path"
# print "ParentPath: $ParentPath"
if ("$ParentPath" -eq "") {
return $Path;
}
# print "Calling: GetRootDirectory($ParentPath, $Path)";
return GetRootDirectory $ParentPath $Path
} else
{
return $null;
}
}
# Recursively copies the directory at $Path to the $Destination.
function CopyDirectoryRecursive($DirectoryPath, $DestinationPath)
{
Copy-Item "$DirectoryPath" -Destination "$DestinationPath" -Recurse
}
# Recursively removes the specified directory.
function RemoveDirectoryRecursive($DirectoryPath)
{
Remove-Item -Recurse -Force "$DirectoryPath"
}
# Returns all files contained in the spexified directories
function GetFilesRecursively($DirectoryPath, $Filter = $null,
$IncludeFiles = $true, $IncludeDirectories = $true)
{
if (-not $(DirectoryExists $DirectoryPath))
{
return $null;
}
return Get-ChildItem "$DirectoryPath" -Filter "$Filter" -Recurse | ForEach-Object { $_.FullName }
# return Get-ChildItem "$DirectoryPath" -Filter "$Filter" -Recurse | % { $_.FullName }
}
function RunOnFilesRecursively($DirectoryPath, $Filter = $null,
$IncludeFiles = $true, $IncludeDirectories = $true, $Function)
{
foreach ($file in $(GetFilesRecursively($DirectoryPath, $Filter, $IncludeFiles, $IncludeDirectories)))
{
Write-Host "Executing on file: $(FileName $File)..."
$result_75937548 = . $function $file
Write-Host "Result of function evaluation: $result_75937548"
}
}
function ExamplesRunOnFilesRecursive()
{
# Calculation of total size of filtered files contained in a directory (recursive) - manual using Files:
# Remark - in PowerShell, $($null + 1) evaluates to 1.
# Remark: running with trailing '.' will pass variable values to the calling context.
. Files "." "*.ps1" | ForEach-Object { # Could also use %
$f=$_;
if ($(IsFile $f)) {
$TotalLength = $TotalLength + $(Get-Item $f).Length;
}
}
Write-Host "`nTotal size of all .ps1 files included in the current directory: $TotalLength"
# The same calculation using OnFiles (=RunOnFilesRecursively), with script block (anonumous function):
# Reset the total size:
$TotalLength = 0
. OnFiles "." "*.ps1" -Function {
param ($filePath) # Define function parameter list within the block, like in scripts
if ($(IsFile $filePath)) {
$TotalLength = $TotalLength + $(Get-Item $filePath).Length;
Write-Host "Accummulated value: TotalLength = $TotalLength ."
}
}
Write-Host "`nTotal size via OnFiles, using anonymous function: $TotalLength"
# Via defined function:
function AddFileLength ($filePath) {
if ($(IsFile $filePath)) {
$TotalLength = $TotalLength + $(Get-Item $filePath).Length;
Write-Host "Accummulated value: TotalLength = $TotalLength ."
}
}
. OnFiles "." "*.ps1" -Function AddFileLength
Write-Host "`nTotal size via OnFiles, using named function AddFileLength(): $TotalLength"
}
# At some point, figure out what to do with the cal lbelow (maybe move to another script?)
# ExamplesRunOnFilesRecursive