Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 025eee3

Browse files
error state, unable to fix the ghost value problem
1 parent dd2c595 commit 025eee3

File tree

12 files changed

+143
-135
lines changed

12 files changed

+143
-135
lines changed

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

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,87 +15,100 @@ function Get-FolderInfo {
1515
and creates folder objects.
1616
1717
.INPUTS
18-
[string[]]
18+
[System.Collections.Generic.List[string]]
1919
Accepts an array of folder paths as input.
2020
2121
.OUTPUTS
22-
[PSCustomObject[]]
22+
[System.Collections.Generic.List[pscustomobject]]
2323
Returns an array of custom folder objects containing resolved paths and validation status.
2424
The object `folderObj` contains the following properties
2525
- `Parent`: The parent folder of the input folder path.
26-
- `Name`: The name of the folder.
27-
- `Valid`: A boolean indicating whether the folder is valid.
26+
- `Name`: The name of the folder.
27+
- `Valid`: A boolean indicating whether the folder is valid.
2828
2929
.NOTES
30-
This is a helper function that should only be called in another function.
31-
This function should not be called by the user directly.
30+
Private helper function for internal validation in the Move-ChildItemUp module.
31+
Not intended for direct use by end users.
3232
33+
Scope: Private
34+
Author: Jialiang Chang
35+
Version: 1.0.0
36+
Last Updated: 2025年06月25日
3337
#>
38+
[OutputType([System.Collections.Generic.List[pscustomobject]])]
3439
[CmdletBinding()]
3540
param (
3641
[Parameter(Mandatory = $true, Position = 0)]
37-
[string[]]$folderPathsArray
42+
[System.Collections.Generic.List[string]]$folderPathsArray
3843
)
3944

40-
# Initialize array of objects
41-
# Use the more efficient list instead of the PowerShell array
42-
$folderObjArray = New-Object System.Collections.Generic.List[string]
45+
# Initialize array of objects to return
46+
$folderObjArray = [System.Collections.Generic.List[pscustomobject]]::new()
4347

44-
# Initialize the previous folder path for skipping duplicates
45-
$previousFolderPath = $null
48+
# Initialize the seenPaths for skipping duplicates
49+
$seenPaths = [System.Collections.Generic.HashSet[string]]::new()
4650

47-
# Resolve path with ecc and validation
48-
# Split path into parent and name
49-
# Store info in a folder object
51+
$isFolderValid=$null
52+
53+
# Start main loop
5054
foreach ($folderPath in $folderPathsArray) {
51-
# Resolve with ecc
52-
$folderPath = Resolve-PathwErr $folderPath
5355

54-
# Check duplicates and skip the second one
55-
# For the first folder in the array, this code should never execute
56-
if ($null -eq $folderPath) {
57-
Write-Verbose "Invalid folder due to failed path resolution."
58-
}
59-
elseif ($previousFolderPath -eq $folderPath) {
60-
# If the previous folder path is the same as the current folder path
61-
Write-Warning "The folder '$folderPath' is the same as the previous folder."
62-
# add an additional false to the valid folder bool variable.
63-
$isFolderValid = $false
56+
# Step 0: Check for empty input
57+
if ([string]::IsNullOrWhiteSpace($folderPath)) {
58+
Write-Verbose "Empty or whitespace folder path. Skipping..."
59+
continue
6460
}
6561

66-
# Update the previous folder path for next step duplication
67-
# To be clear, when executing this program inside an actual folder or a valid
68-
# working directory, a duplicate should not happen thanks to Windows,
69-
# but there might be a chance of user error in input, so it makes sense
70-
# to implement this additional error checking mechanism.
71-
$previousFolderPath = $folderPath
72-
73-
# Moved null detection logic here to make powershell happy
74-
# If null is detected in input, this only means resolution failed
75-
if ([string]::IsNullOrEmpty($folderPath)) {
76-
Write-Verbose "Invalid folder due to failed path resolution."
77-
$isFolderValid = $false
62+
# Step 1: Resolve the folder path for consistency
63+
# when resolution failed, fall back to `$null`
64+
$resolvedPath = Resolve-PathwErr $folderPath
65+
66+
# Step 2: Check if the resolved path is `$null`
67+
if ([string]::IsNullOrEmpty($resolvedPath)) {
68+
# If the resolved path is `$null` then the path is invalid
69+
Write-Warning "Path resolution failed for '$folderPath'. Skipping..."
70+
# Skip the path
71+
continue
7872
}
79-
else {
80-
# Validate folder path
81-
$isFolderValid = Confirm-FolderPath $folderPath
8273

83-
# It only make sense to execute the following when input is not null
74+
# Step 3: Validate folder path
75+
# validates the path exists and is a folder
76+
# potential duplicate validation logic
77+
$isFolderValid = Confirm-FolderPath $resolvedPath
78+
79+
# Check if the folder path is valid
80+
if ($isFolderValid) {
81+
# If the folder path is valid
82+
# Check for duplicate
83+
if ($seenPaths.Contains($resolvedPath)) {
84+
Write-Warning "Duplicate folder detected: '$resolvedPath'. Skipping..."
85+
continue
86+
}
87+
88+
# Add the resolved path to the seen list
89+
$seenPaths.Add($resolvedPath)
8490

8591
# Setup folder object
86-
$folderObj = Get-FolderParentInfo $folderPath$isFolderValid
92+
$folderObj = Get-FolderParentInfo $resolvedPath
8793

8894
# Add folder object to the array for return
8995
$folderObjArray.Add($folderObj)
90-
}
96+
}
97+
else {
98+
# If the folder path is not valid (likely a file)
99+
Write-Warning "The path '$folderPath' is not valid (likely a file). Skipping..."
100+
continue
101+
}
91102
}
92103

93104
# Checking output folder array for null to make powershell happy
94105
if (-not $folderObjArray -or $folderObjArray.Count -eq 0) {
95106
Show-ErrorMsg `
96-
-FunctionName $MyInvocation.MyCommand.Name
107+
-FunctionName $MyInvocation.MyCommand.Name`
97108
-CustomMessage "No valid folder can be processed."
98109
}
99110

111+
Write-Host "DEBUG: First item type in folderObjArray: $($folderObjArray[0].GetType().FullName)"
112+
100113
return $folderObjArray
101114
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,32 @@ function Move-FolderContents {
1515
and validity status.
1616
1717
.INPUTS
18-
[PSCustomObject[]]
18+
[System.Collections.Generic.List[pscustomobject]]
1919
Accepts an array of folder objects as input.
2020
2121
.OUTPUTS
2222
None. Outputs status messages to the console.
2323
2424
.NOTES
25-
This is a helper function that should only be called in another function.
26-
This function should not be called by the user directly.
27-
25+
Private helper function for internal validation in the Move-ChildItemUp module.
26+
Not intended for direct use by end users.
27+
28+
Scope: Private
29+
Author: Jialiang Chang
30+
Version: 1.0.0
31+
Last Updated: 2025年06月25日
2832
#>
2933
[CmdletBinding()]
3034
param (
3135
[Parameter(Mandatory = $true, Position = 0)]
32-
[PScustomObject[]]$folderObjArray
36+
[System.Collections.Generic.List[pscustomobject]]$folderObjArray
3337
)
3438

3539
# Start moving files in a folder
3640
Write-Host "File moving starts..." -ForegroundColor Cyan
3741
foreach ($folderObj in $folderObjArray) {
38-
39-
# If the folder was marked invalid, skip this folder
40-
if (-not $folderObj.Valid) {
41-
Write-Verbose "Skipped moving file in an invalid entry."
42-
continue
43-
}
4442

43+
# Construct full path
4544
$folderFullPath = Join-Path -Path $folderObj.Parent -ChildPath $folderObj.Name
4645

4746
# Create an array of fileObj

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@ function Remove-EmptyFolder {
3232
)]
3333
param(
3434
[Parameter(Mandatory = $true, Position = 0)]
35-
[PSCustomObject[]]$folderObjArray
35+
[System.Collections.Generic.List[pscustomobject]]$folderObjArray
3636
)
3737

3838
foreach ($folderObj in $folderObjArray) {
39-
40-
If (-not $folderObj.Valid) {
41-
Write-Verbose "Skipping removal of an invalid entry..."
42-
continue
43-
}
4439

40+
# Construct full path
4541
$folderPath = Join-Path -Path $folderObj.Parent -ChildPath $folderObj.Name
4642

4743
# Check if the folder is empty

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,15 @@ function Get-FolderParentInfo {
1414
.PARAMETER inputFolderPath
1515
The folder path to process. This can be an absolute or relative path.
1616
17-
.PARAMETER isFolderValid
18-
A boolean flag indicating whether the folder path is valid. If `$false`, the function
19-
skips processing and logs a warning.
20-
2117
.INPUTS
2218
[string[]]
2319
Accepts a folder path as input.
2420
25-
[bool]
26-
Accepts a validity flag as input.
27-
2821
.OUTPUTS
2922
[PSCustomObject]
3023
Returns a custom object `folderObj` with the following properties:
3124
- `Parent`: The parent folder of the input folder path.
3225
- `Name`: The name of the folder.
33-
- `Valid`: A boolean indicating whether the folder is valid.
3426
3527
.NOTES
3628
This is a helper function that should only be called in another function.
@@ -40,27 +32,17 @@ function Get-FolderParentInfo {
4032
[CmdletBinding()]
4133
param (
4234
[Parameter(Mandatory = $true, Position = 0)]
43-
[string[]]$inputFolderPath,
44-
45-
[Parameter(Mandatory = $true, Position = 1)]
46-
[bool]$isFolderValid
35+
[string[]]$inputFolderPath
4736
)
4837

4938
# Initialize two attributes with null values for ecc purposes
5039
$parentFolder = $null
5140
$folderName = $null
5241

5342
try {
54-
if ($isFolderValid) {
55-
# Split the path into parent folder and folder name
56-
$parentFolder = Split-Path -Path $inputFolderPath -Parent
57-
$folderName = Split-Path -Path $inputFolderPath -Leaf
58-
}
59-
else {
60-
# We have moved warning to here.
61-
# ? Where to put the warning is the best
62-
Write-Warning "Path '$inputFolderPath' will be skipped."
63-
}
43+
# Split the path into parent folder and folder name
44+
$parentFolder = Split-Path -Path $inputFolderPath -Parent
45+
$folderName = Split-Path -Path $inputFolderPath -Leaf
6446
}
6547
catch {
6648
Show-ErrorMsg `
@@ -70,11 +52,9 @@ function Get-FolderParentInfo {
7052
}
7153

7254
# Create a PS object with Parent and Name properties
73-
# Store folder validity, will skip invalid folder later
7455
$folderObj = [PSCustomObject]@{
7556
Parent = $parentFolder
7657
Name = $folderName
77-
Valid = $isFolderValid
7858
}
7959
# Also notice that in the above code block, there is an option to also include the
8060
# full path to this folder. However, I finally decided to not to add this attribute

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,37 @@ function Resolve-PathwErr {
1515
The folder path to resolve. This can be an absolute or relative path.
1616
1717
.INPUTS
18-
[string]
19-
Accepts a single folder path as input.
18+
[string] Accepts a single folder path as input.
2019
2120
.OUTPUTS
22-
[string]
23-
Returns the resolved folder path if successful; otherwise, returns `$null`.
21+
[string] Returns the resolved folder path if successful; otherwise, returns `$null`.
2422
2523
.NOTES
26-
This is a helper function that should only be called in another function.
27-
This function should not be called by the user directly.
28-
24+
Private helper function for internal validation in the Move-ChildItemUp module.
25+
Not intended for direct use by end users.
26+
27+
Scope: Private
28+
Author: Jialiang Chang
29+
Version: 1.0.0
30+
Last Updated: 2025年06月25日
2931
#>
3032
[CmdletBinding()]
3133
param (
3234
[Parameter(Mandatory = $true, Position = 0)]
3335
[string]$inputFolderPath
3436
)
35-
3637

37-
$inputFolderPathResolved = Resolve-Path -Path $inputFolderPath -ErrorAction SilentlyContinue
38-
if ($null -eq $inputFolderPathResolved) {
39-
Write-Warning "Resolve path '$inputFolderPath' failed."
40-
Write-Warning "The folder will be skipped."
38+
Write-Verbose "Resolving path for '$inputFolderPath'."
39+
40+
# Catch any error during Resolve-Path operation
41+
try {
42+
$outputPath = (Resolve-Path -Path $inputFolderPath -ErrorAction Stop).Path
43+
Write-Verbose "Resolve operation successful for '$inputFolderPath',"
44+
Write-Verbose "output path: '$outputPath'.`n"
45+
}
46+
catch {
47+
$outputPath = $null
4148
}
42-
return $inputFolderPathResolved
4349

50+
return $outputPath
4451
}

‎Project_Move-ChildItemUp/Helpers/ValidationHelper/Confirm-FolderArray.ps1

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,42 @@
22
function Confirm-FolderArray {
33
<#
44
.SYNOPSIS
5-
Validates an array of folder paths to ensure they are suitable for processing.
5+
Validates an array of folder paths for null values and duplicate entries.
66
77
.DESCRIPTION
8-
The `Confirm-FolderArray` function checks an array of folder paths for null values and
9-
duplicates. This function performs a very simple check on the input file array.
10-
Duplicate check work by compare length of array and the number of unique strings.
11-
More detailed checks for duplicate is in another function.
8+
The `Confirm-FolderArray` function performs basic validation on an array of folder paths
9+
to ensure they are suitable for further processing. Specifically, it checks that the input
10+
is not null or empty, and that it does not contain duplicate folder paths.
11+
12+
The duplicate check is performed by comparing the total number of items in the array
13+
to the number of unique strings. More advanced folder validation, such as resolving
14+
equivalent paths or case-insensitive duplicates, is handled by other functions in the module.
1215
1316
.PARAMETER folderPathsArray
14-
An array of folder paths to validate. The function checks for null values and duplicates.
17+
An array of folder paths to validate. The function ensures the array is not empty and
18+
contains only unique entries.
1519
1620
.INPUTS
17-
[string[]]
18-
Accepts an array of folder paths as input.
21+
[string[]] Accepts an array of folder paths as input.
1922
2023
.OUTPUTS
21-
None. Outputs validation messages to the console.
24+
None. This function does not return output objects, but may write warning or verbose messages
25+
to the console.
2226
2327
.NOTES
24-
This is a helper function that should only be called in another function.
25-
This function should not be called by the user directly.
28+
Private helper function for internal validation in the Move-ChildItemUp module.
29+
Not intended for direct use by end users.
2630
31+
Scope: Private
32+
Author: Jialiang Chang
33+
Version: 1.0.0
34+
Last Updated: 2025年06月25日
2735
#>
36+
2837
[CmdletBinding()]
2938
param(
3039
[Parameter(Mandatory = $true, Position = 0)]
31-
[string[]]$folderPathsArray
40+
[System.Collections.Generic.List[string]]$folderPathsArray
3241
)
3342

3443
# Checking input folder array for null
@@ -46,7 +55,7 @@ function Confirm-FolderArray {
4655
if ($folderPathsArray.Count -ne ($folderPathsArray | Select-Object -Unique).Count) {
4756
# Current behavior, function continues and warns user about the skipping
4857
Write-Warning "Duplicate folders detected in the input."
49-
Write-Verbose"Will skip the duplicate folders."
58+
Write-Warning"The duplicate folders will be skipped."
5059
}
5160

5261
Write-Verbose "Folder path array input check passed."

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /