From 4fea9dbcccd506b5dcbf1ce8c300193ffd4b05bd Mon Sep 17 00:00:00 2001 From: Princess Pi Date: Fri, 25 Sep 2026 16:01:49 -0600 Subject: [PATCH] waypoint debug --- Windows-Scripts/checksum.ps1 | 117 ++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 43 deletions(-) diff --git a/Windows-Scripts/checksum.ps1 b/Windows-Scripts/checksum.ps1 index 05d1de7..39ff4b1 100644 --- a/Windows-Scripts/checksum.ps1 +++ b/Windows-Scripts/checksum.ps1 @@ -5,10 +5,15 @@ param( [Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)] [string[]]$FilePath, - # Output file (-o / -OutFile) + # Custom output file (-f / -OutFile) + [Parameter(Mandatory = $false, ParameterSetName = 'Generate')] + [Alias('f')] + [string]$OutFile, + + # Auto output file switch (-o) [Parameter(Mandatory = $false, ParameterSetName = 'Generate')] [Alias('o')] - [string]$OutFile, + [switch]$AutoOut, # File containing checksums to verify (-c / -Check) [Parameter(Mandatory = $false, ParameterSetName = 'Verify')] @@ -44,7 +49,7 @@ NAME checksum - Compute and verify message digests / file checksums SYNOPSIS - .\checksum.ps1 [-FilePath] [-o []] [-Algorithm ] [-r] + .\checksum.ps1 [-FilePath] [-o] [-f ] [-Algorithm ] [-r] .\checksum.ps1 [-FilePath] -Match [-Algorithm ] [-r] .\checksum.ps1 -Check [-Algorithm ] [-r] .\checksum.ps1 -Help @@ -58,40 +63,43 @@ PARAMETERS -FilePath Specifies the target file(s) or wildcard pattern (*). - -o, -OutFile - Enables auto-saving checksum results to file(s). - - If passed with an argument (e.g. -o custom.txt), writes to 'custom.txt'. - - If passed with NO argument (or empty string ""), automatically outputs to 'checksums.'. - When combined with -r and *, recursively auto-generates checksum files per directory. + -o, -O + Auto-saves checksum results. (Takes no arguments). + - If input filename contains a wildcard (*), saves to 'checksums.'. + - If input filename does NOT contain a wildcard, saves to '.'. + When combined with -r and *, recursively auto-generates checksums per directory. - -c, -Check + -f, -F, -OutFile + Saves checksum results to a specific custom filename. + Overrides the auto-naming behavior of -o if both are passed. + + -c, -C, -Check Reads hashes from a specified checksum file and verifies target files on disk. If -r is specified, recursively looks for and validates checksum files by name across subdirectories. - -m, -Match + -m, -M, -Match Compares the computed hash of target file(s) directly against a given hash string. - -a, -algo, -Algorithm + -a, -A, -Algo, -Algorithm Specifies the hash algorithm to use. - Supported options: SHA1, SHA256 (default), SHA384, SHA512, MD5. + Supported options: MD5, SHA1, SHA256 (default), SHA384, SHA512. In -c or -m mode, auto-detects algorithm from hash length if omitted. - -r, -Recursive + -r, -R, -Recursive Processes target files or verification files recursively across directory structures. - -h, -Help + -h, -H, -Help Displays this help documentation. EXAMPLES - 1. Compute SHA256 for a single file: - .\checksum.ps1 myfile.iso + 1. Compute SHA256 for a single file and auto-save as 'myfile.iso.sha256': + .\checksum.ps1 myfile.iso -o - 2. Compute SHA512 checksums recursively and auto-save (prompts for filename, hit Enter for auto): + 2. Compute SHA512 recursively and auto-save as 'checksums.sha512' in each folder: .\checksum.ps1 * -r -o -a SHA512 - .\checksum.ps1 * -Recursive -OutFile "" -Algorithm SHA512 - 3. Compute SHA256 checksums and save to custom file: - .\checksum.ps1 * -o my_hashes.txt + 3. Compute SHA256 checksums and save to a specific custom file: + .\checksum.ps1 * -f my_hashes.txt 4. Verify checksums against a verification file: .\checksum.ps1 -c checksums.sha256 @@ -296,10 +304,11 @@ process { if ([string]::IsNullOrWhiteSpace($Algorithm)) {$Algorithm = 'SHA256'; }; - $wantOutFile =$PSBoundParameters.ContainsKey('OutFile'); - $isAutoFile = [string]::IsNullOrWhiteSpace($OutFile); + $wantCustomOut =$PSBoundParameters.ContainsKey('OutFile'); + $wantAutoOut =$AutoOut.IsPresent; - if ($wantOutFile) { + if ($wantCustomOut -or$wantAutoOut) { + # Recursive wildcard auto-saves per directory if ($Recursive -and$isWildcardGlobal) { $groupedItems =$allItems | Group-Object DirectoryName; @@ -307,10 +316,9 @@ process { $dirPath =$group.Name; $filesInDir =$group.Group; - if ($isAutoFile) { - $currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())"; + if ($wantCustomOut) {$currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile); } else { - $currentOutFile = Join-Path -Path ($dirPath) -ChildPath ($OutFile); + $currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())"; }; $linesToWrite = @(); @@ -326,28 +334,51 @@ process { }; } else { - if ($isAutoFile) {$singleOutFile = "checksums.$($Algorithm.ToLower())"; - } else { - $singleOutFile =$OutFile; - }; - - $linesToWrite = @(); - foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower(); - - $relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'; - if (-not $relativePath) { $relativePath =$item.Name; }; - if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) { - $relativePath =$relativePath.Substring(2); + # Custom file OR Auto file with a wildcard + if ($wantCustomOut -or ($wantAutoOut -and$isWildcardGlobal)) { + if ($wantCustomOut) { + $singleOutFile =$OutFile; + } else { + $singleOutFile = "checksums.$($Algorithm.ToLower())"; }; - $outputLine = "${hash}${relativePath}"; - $linesToWrite +=$outputLine; - Write-Output $outputLine; - }; + $linesToWrite = @(); + foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower(); + + $relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'; + if (-not $relativePath) { $relativePath =$item.Name; }; + if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) { + $relativePath =$relativePath.Substring(2); + }; - Set-Content -Path ($singleOutFile) -Value$linesToWrite -Encoding utf8; + $outputLine = "${hash}${relativePath}"; + $linesToWrite +=$outputLine; + Write-Output $outputLine; + }; + + Set-Content -Path ($singleOutFile) -Value$linesToWrite -Encoding utf8; + } + # Auto file for explicit individual files (No wildcard) + else { + foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower(); + + $relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue'; + if (-not $relativePath) { $relativePath =$item.Name; }; + if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) { + $relativePath =$relativePath.Substring(2); + }; + + $outputLine = "${hash}${relativePath}"; + Write-Output $outputLine; + + # Save directly next to the original file: . + $itemOutFile = "$($item.FullName).$($Algorithm.ToLower())"; + Set-Content -Path ($itemOutFile) -Value$outputLine -Encoding utf8; + }; + }; }; } + # Standard console output (No save flags passed) else { foreach ($item in ($allItems)) {$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($Algorithm)).Hash.ToLower();