Skip to content

Commit 95dbf48

Browse files
committed
added the write-bounds function for verbose outputs
1 parent 268459d commit 95dbf48

14 files changed

+181
-28
lines changed

Project_Move-ChildItemUp/Helpers/MidLogicHelper/Get-FolderInfo.ps1

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,27 @@ function Get-FolderInfo {
4040
[System.Collections.Generic.List[string]]$folderPathsArray
4141
)
4242

43+
Write-Bounds `
44+
-FunctionName $MyInvocation.MyCommand.Name `
45+
-Mode "Enter"
46+
Write-Verbose "Start collecting folder relevant info..."
47+
4348
# Initialize array of objects to return
49+
Write-Verbose "Initializing folder object array list..."
4450
$folderObjArray = [System.Collections.Generic.List[pscustomobject]]::new()
4551

4652
# Initialize the seenPaths for skipping duplicates
53+
Write-Verbose "Initializing seen path hash set..."
4754
$seenPaths = [System.Collections.Generic.HashSet[string]]::new()
4855

4956
# Start main loop
57+
Write-Verbose "Start main loop..."
5058
foreach ($folderPath in $folderPathsArray) {
5159

5260
# Step 0: Check for empty input
61+
Write-Verbose "Checking for empty or null input..."
5362
if ([string]::IsNullOrWhiteSpace($folderPath)) {
54-
Write-Verbose "Empty or whitespace folder path. Skipping..."
63+
Write-Warning "Empty or whitespace folder path. Skipping..."
5564
continue
5665
}
5766

@@ -60,6 +69,7 @@ function Get-FolderInfo {
6069
$resolvedPath = Resolve-PathwErr $folderPath
6170

6271
# Step 2: Check if the resolved path is `$null`
72+
Write-Verbose "Checking for null resolved path..."
6373
if ([string]::IsNullOrEmpty($resolvedPath)) {
6474
# If the resolved path is `$null` then the path is invalid
6575
Write-Warning "Path resolution failed for '$folderPath'. Skipping..."
@@ -70,13 +80,15 @@ function Get-FolderInfo {
7080
# Step 3: Validate folder path
7181
# validates the path exists and is a folder
7282
# potential duplicate validation logic
83+
Write-Verbose "Checking for path type (folder or file)..."
7384
if (-not (Confirm-FolderPath $resolvedPath)) {
7485
# If the folder path is not valid (likely a file)
7586
Write-Warning "The path '$folderPath' is not valid (likely a file). Skipping..."
7687
continue
7788
}
7889

7990
# Step 4: Check for duplicates
91+
Write-Verbose "Checking for duplicate entries..."
8092
if (-not $seenPaths.Add($resolvedPath)) {
8193
Write-Warning "Duplicate folder detected: '$resolvedPath'. Skipping..."
8294
continue
@@ -86,15 +98,22 @@ function Get-FolderInfo {
8698
$folderObj = Get-FolderParentInfo $resolvedPath
8799

88100
# Step 6: Add to return list
101+
Write-Verbose "Adding folder object to the process list..."
89102
$folderObjArray.Add($folderObj)
90103
}
91104

92105
# Checking output folder array for null to make powershell happy
106+
Write-Verbose "Checking for empty process list..."
93107
if (-not $folderObjArray -or $folderObjArray.Count -eq 0) {
94108
Show-ErrorMsg `
95109
-FunctionName $MyInvocation.MyCommand.Name `
96110
-CustomMessage "No valid folder can be processed."
97111
}
98112

113+
Write-Verbose "Folder info collection complete."
114+
Write-Bounds `
115+
-FunctionName $MyInvocation.MyCommand.Name `
116+
-Mode "Exit"
117+
99118
return $folderObjArray
100119
}

Project_Move-ChildItemUp/Helpers/MidLogicHelper/Move-FolderContents.ps1

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,27 @@ function Move-FolderContents {
3737
)
3838

3939
# Start moving files in a folder
40+
Write-Bounds `
41+
-FunctionName $MyInvocation.MyCommand.Name `
42+
-Mode "Enter"
43+
4044
Write-Host "File moving starts..." -ForegroundColor Cyan
4145
foreach ($folderObj in $folderObjArray) {
4246

4347
# Construct full path
48+
Write-Verbose "Constructing full path..."
4449
$folderFullPath = Join-Path -Path $folderObj.Parent -ChildPath $folderObj.Name
4550

4651
# Create an array of fileObj
52+
Write-Verbose "Obtaining all available files in the folder..."
4753
$fileObjArray = Get-ChildItem -Path $folderFullPath -Force
4854

4955
# Start actually moving the files
56+
Write-Verbose "Start moving loop..."
5057
foreach ($fileObj in $fileObjArray) {
5158

59+
Write-Verbose "Setup move target..."
60+
5261
# Set targetFileObj
5362
$targetFileObj = [PSCustomObject]@{
5463
Filename = $fileObj.Name
@@ -60,25 +69,30 @@ function Move-FolderContents {
6069
-Path $targetFileObj.Destination `
6170
-ChildPath $targetFileObj.Filename
6271

63-
# Handle file name conflicts inside the parent folder
72+
Write-Verbose "Check for filename conflicts..."
73+
74+
# Handle filename conflicts inside the parent folder
6475
if (Test-Path $targetFilePath) {
6576
Write-Warning "Conflict detected: '$($targetFileObj.Filename)' already exists in `n$($targetFileObj.Destination)."
6677

6778
# Prompt user for response: skip, rename, (overwrite)...
68-
$userAction = Confirm-FileConflict $targetFileObj $folderObj
79+
$userAction =
80+
Confirm-FileConflict $targetFileObj $folderObj
6981

7082
# Handles skipping
7183
if ($userAction -eq "skip") {
7284
# For simplicity, the continue is called here
7385
# If future more options that requires continue comes,
7486
# consider change this to a bool condition.
87+
Write-Verbose "File skipped."
7588
continue
7689
}
7790

7891
# This line carries out the user action,
7992
# right now there is only one, but if there are more in the future
8093
# consider adding here.
81-
$targetFileObj = Deploy-UserAction $userAction $targetFileObj $folderObj
94+
$targetFileObj =
95+
Deploy-UserAction $userAction $targetFileObj $folderObj
8296
}
8397

8498
# Define moving source and target paths
@@ -96,11 +110,16 @@ function Move-FolderContents {
96110
# Move the file, ensuring paths with spaces are handled correctly
97111
try {
98112
Move-Item -LiteralPath "$sourceFilePath" -Destination "$destinationFilePath"
99-
Write-Verbose "Moved file '$sourceFilePath' to new path '$destinationFilePath'."
113+
Write-Verbose "Moved file '$sourceFilePath' to `n'$destinationFilePath'."
100114
}
101115
catch {
102116
Write-Warning "Failed to move file '$sourceFilePath' to '$destinationFilePath'. Error: $($_.Exception.Message)"
117+
continue
103118
}
104119
}
105120
}
121+
122+
Write-Bounds `
123+
-FunctionName $MyInvocation.MyCommand.Name `
124+
-Mode "Exit"
106125
}

Project_Move-ChildItemUp/Helpers/MidLogicHelper/Remove-EmptyFolder.ps1

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,23 @@ function Remove-EmptyFolder {
3939
[System.Collections.Generic.List[pscustomobject]]$folderObjArray
4040
)
4141

42+
Write-Bounds `
43+
-FunctionName $MyInvocation.MyCommand.Name `
44+
-Mode "Enter"
45+
Write-Verbose "Start cleaning up after moving file..."
46+
4247
foreach ($folderObj in $folderObjArray) {
4348

4449
# Construct full path
50+
Write-Verbose "Construct full path..."
4551
$folderPath = Join-Path -Path $folderObj.Parent -ChildPath $folderObj.Name
4652

4753
# Check if the folder is empty
54+
Write-Verbose "Checking if folder is empty..."
4855
$childItems = Get-ChildItem -Path $folderPath -Force
49-
5056
if ($childItems.Count -eq 0) {
5157
# Use ShouldProcess for confirmation
58+
Write-Verbose "Removing Empty folder..."
5259
if ($PSCmdlet.ShouldProcess("$folderPath", "Remove empty folder")) {
5360
Remove-Item -Path $folderPath -Force
5461
Write-Host "Removed empty folder:`n$folderPath" `
@@ -57,8 +64,13 @@ function Remove-EmptyFolder {
5764
}
5865
else {
5966
Write-Host "Skipped non-empty folder:`n$folderPath" `
60-
-ForegroundColor DarkYellow
67+
-ForegroundColor DarkYellow
6168
continue
6269
}
6370
}
71+
72+
Write-Verbose "Clean up complete."
73+
Write-Bounds `
74+
-FunctionName $MyInvocation.MyCommand.Name `
75+
-Mode "Exit"
6476
}

Project_Move-ChildItemUp/Helpers/PScmdWrapper/Deploy-UserAction.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,20 @@ function Deploy-UserAction {
5454
[Parameter(Mandatory = $true, Position = 2)]
5555
[PSCustomObject]$folderObj
5656
)
57+
58+
Write-Bounds `
59+
-FunctionName $MyInvocation.MyCommand.Name `
60+
-Mode "Enter"
61+
Write-Verbose "Carrying out user action..."
5762

5863
# Execute the rename operation
5964
if ($userAction -eq "rename") {
6065
$targetFileObj = Start-Rename $targetFileObj
6166
return $targetFileObj
6267
}
68+
69+
Write-Verbose "User action completed."
70+
Write-Bounds `
71+
-FunctionName $MyInvocation.MyCommand.Name `
72+
-Mode "Exit"
6373
}

Project_Move-ChildItemUp/Helpers/PScmdWrapper/Get-FolderParentInfo.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,30 @@ function Get-FolderParentInfo {
3838
[string]$inputFolderPath
3939
)
4040

41+
Write-Bounds `
42+
-FunctionName $MyInvocation.MyCommand.Name `
43+
-Mode "Enter"
44+
Write-Verbose "Start collection parent folder information..."
45+
46+
Write-Verbose "Initializing null variables for assignment..."
4147
# Initialize two attributes with null values for ecc purposes
4248
$parentFolder = $null
4349
$folderName = $null
4450

4551
try {
52+
Write-Verbose "Splitting the input path '$inputFolderPath'..."
4653
# Split the path into parent folder and folder name
4754
$parentFolder = Split-Path -Path $inputFolderPath -Parent
4855
$folderName = Split-Path -Path $inputFolderPath -Leaf
4956
}
5057
catch {
5158
Show-ErrorMsg `
5259
-FunctionName $MyInvocation.MyCommand.Name
53-
-CustomMessage "Error in splitting folder path '$inputFolderPath'."
54-
-Exception $_.Exception
60+
-CustomMessage "Error in splitting folder path '$inputFolderPath'."
61+
-Exception $_.Exception
5562
}
56-
63+
64+
Write-Verbose "Constructing the folder object..."
5765
# Create a PS object with Parent and Name properties
5866
$folderObj = [PSCustomObject]@{
5967
Parent = $parentFolder
@@ -64,5 +72,10 @@ function Get-FolderParentInfo {
6472
# because you can always assemble your own full path at any time, this prevents
6573
# unnecessary use of obj attributes.
6674

75+
Write-Verbose "Parent folder information collected."
76+
Write-Bounds `
77+
-FunctionName $MyInvocation.MyCommand.Name `
78+
-Mode "Exit"
79+
6780
return $folderObj
6881
}

Project_Move-ChildItemUp/Helpers/PScmdWrapper/Resolve-PathwErr.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ function Resolve-PathwErr {
3535
[string]$inputFolderPath
3636
)
3737

38-
Write-Verbose "Resolving path for '$inputFolderPath'."
38+
Write-Bounds `
39+
-FunctionName $MyInvocation.MyCommand.Name `
40+
-Mode "Enter"
41+
Write-Verbose "Resolving for '$inputFolderPath'..."
3942

4043
# Catch any error during Resolve-Path operation
4144
try {
@@ -45,7 +48,14 @@ function Resolve-PathwErr {
4548
}
4649
catch {
4750
$outputPath = $null
51+
Write-Verbose "Resolve operation failed, fall back to null."
52+
Write-Verbose "Output path: null."
4853
}
54+
55+
Write-Verbose "Path resolution complete..."
56+
Write-Bounds `
57+
-FunctionName $MyInvocation.MyCommand.Name `
58+
-Mode "Exit"
4959

5060
return $outputPath
5161
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Write-Bounds {
2+
param(
3+
[string]$FunctionName,
4+
5+
[ValidateSet('Enter', 'Exit')]
6+
[string]$Mode
7+
)
8+
9+
$prefix = if ($Mode -eq 'Enter') { 'ENTER' } else { 'EXIT ' }
10+
Write-Verbose "[==== ${prefix}: $FunctionName ====]"
11+
}

Project_Move-ChildItemUp/Helpers/QueryHelper/Confirm-FileConflict.ps1

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,60 @@ function Confirm-FileConflict {
4343
[PSCustomObject]$folderObj
4444
)
4545

46+
Write-Bounds `
47+
-FunctionName $MyInvocation.MyCommand.Name `
48+
-Mode "Enter"
49+
Write-Verbose "Checking user response..."
50+
4651
$skipKeyWords = @('S', 's', '')
4752
$renameKeyWords = @('R', 'r')
4853
# $overwriteKeyWords = @('O', 'o')
4954

50-
Write-Host "To skip this file, type 'S' or 's' or 'Enter'." `
51-
-ForegroundColor DarkYellow
52-
Write-Host "To rename this file, type 'R' or 'r'." `
53-
-ForegroundColor DarkYellow
55+
Write-Host "To skip this file, type " -NoNewline -ForegroundColor Cyan
56+
Write-Host "'S'" -NoNewline -ForegroundColor Cyan
57+
Write-Host ", 's', or press Enter." -ForegroundColor Cyan
58+
59+
Write-Host "To rename this file, type " -NoNewline -ForegroundColor Cyan
60+
Write-Host "'R'" -NoNewline -ForegroundColor Cyan
61+
Write-Host " or 'r'." -ForegroundColor Cyan
62+
5463

5564
# Loop for continuous prompting
5665
while ($true) {
5766
# Read user input
5867
$response = Read-Host "Enter your response"
5968

6069
if ($skipKeyWords -contains $response) {
61-
Write-Host "File '$($targetFileObj.Filename)' will be skipped."
70+
Write-Host "File '$($targetFileObj.Filename)' will be skipped." `
71+
-ForegroundColor Green
72+
73+
Write-Verbose "User action confirmed."
74+
Write-Bounds `
75+
-FunctionName $MyInvocation.MyCommand.Name `
76+
-Mode "Exit"
77+
6278
return "skip"
6379
}
6480
elseif ($renameKeyWords -contains $response) {
65-
Write-Host "File '$($targetFileObj.Filename)' in '$($folderObj.Name)' will be renamed."
81+
Write-Host "File '$($targetFileObj.Filename)' in '$($folderObj.Name)' will be renamed." `
82+
-ForegroundColor Green
83+
84+
Write-Verbose "User action confirmed."
85+
Write-Bounds `
86+
-FunctionName $MyInvocation.MyCommand.Name `
87+
-Mode "Exit"
88+
6689
return "rename"
6790
}
6891
else {
69-
Write-Warning "Invalid response. Please follow the instructions:"
70-
Write-Host "To skip this file, type 'S' or 's' or 'Enter'." `
71-
-ForegroundColor DarkYellow
72-
Write-Host "To rename this file, type 'R' or 'r'." `
73-
-ForegroundColor DarkYellow
92+
Write-Warning "Invalid response. Please follow the instructions below:"
93+
Write-Host "To skip this file, type " -NoNewline -ForegroundColor DarkYellow
94+
Write-Host "'S'" -NoNewline -ForegroundColor DarkYellow
95+
Write-Host ", 's', or press Enter." -ForegroundColor DarkYellow
96+
97+
Write-Host "To rename this file, type " -NoNewline -ForegroundColor DarkYellow
98+
Write-Host "'R'" -NoNewline -ForegroundColor DarkYellow
99+
Write-Host " or 'r'." -ForegroundColor DarkYellow
74100
}
75101
}
76102
}

0 commit comments

Comments
 (0)