Add optional -Target parameter to Create-Turnaround.ps1 that defaults to '<last directory member>.jpg' in current directory, deriving name from source directory when -Source is a directory or from current directory when -Source is a file pattern, with drive root detection that throws an error for paths like 'C:\' that have no directory member to name the file after

This commit is contained in:
guru committed 2026-10-20 08:31:26 -04:00
1 parent 186daada82
commit c057f804bf
1 file changed
+31 -2
+31 -2
View File
@@ -12,6 +12,10 @@
.PARAMETER Target
Path to the output turnaround image. Can be absolute or relative to current directory. .png extension is added if not provided.
A recognised image extension (.png, .jpg, .jpeg, .webp, .bmp, .avif, .tif, .tiff) selects the output format.
When omitted, a JPEG named after the last directory member of the source is written to the current directory:
- -Source is a directory: the last directory member of that directory's full path (e.g. C:\renders\CharacterA -> CharacterA.jpg).
- -Source is a file pattern or a single file: the last directory member of the current directory's full path.
Throws when that path is a drive root (e.g. C:\), since it has no directory member to name the file after.
.PARAMETER Rows
Number of rows to arrange the images in. Defaults to 1 (all images in a single horizontal row).
When only -Rows is given, the number of columns is computed as ceil(imageCount / Rows).
@@ -53,8 +57,8 @@ param(
[Parameter(Mandatory = $true)]
[string]$Source,
[Parameter(Mandatory = $true)]
[string]$Target,
[Parameter(Mandatory = $false)]
[string]$Target = $null,
[Parameter(Mandatory = $false)]
[int]$Rows = 0,
@@ -105,6 +109,31 @@ function Get-ImageDimensions {
return [pscustomobject]@{ Width = [int]$Matches[1]; Height = [int]$Matches[2] }
}
# --- Default -Target: '<last directory member>.jpg' in the current directory ---
# The name comes from the source directory when -Source is a directory, and from the
# current directory when -Source is a file pattern or a single file. Either way the file
# is written to the current directory.
if ([string]::IsNullOrWhiteSpace($Target)) {
if (Test-Path -LiteralPath $Source -PathType Container) {
$nameSourcePath = (Resolve-Path -LiteralPath $Source).Path
} else {
$nameSourcePath = (Get-Location).Path
}
$nameSourcePath = [System.IO.Path]::GetFullPath($nameSourcePath)
$trimmedPath = $nameSourcePath.TrimEnd('\', '/')
$nameLeaf = Split-Path -Path $trimmedPath -Leaf
# 'C:\' trims to 'C:', which has no directory member to name the file after.
if ([string]::IsNullOrWhiteSpace($nameLeaf) -or $trimmedPath -match '^[A-Za-z]:$') {
Write-Error ('Cannot derive a target file name from ''{0}'': the path is a drive root with no directory member. Specify -Target explicitly.' -f $nameSourcePath)
exit 1
}
$Target = Join-Path (Get-Location).Path ('{0}.jpg' -f $nameLeaf)
Write-Host ('No -Target specified; using {0}' -f $Target)
}
# --- Resolve -Target and the output format ---
$TargetPath = $Target
$targetExt = [System.IO.Path]::GetExtension($TargetPath).ToLowerInvariant()