Numerous fixes + a new command

+= ConvertTo-Zip - autodetects unrar, prefers it on 7-zip - resolves
    issues with some RAR extraction methods 7-zip does not support
+= Invoke-ExpressionEx - Added suppot for -WhatIf switch
++ Move-3Way - folder content comparative duplication
    was written to normalize a comics media folder
== Moved aliases to base, they were wrongly created in docker.
This commit is contained in:
lksz 2021-10-11 20:41:46 -04:00
parent f5e1f3e9af
commit 79e85bb21d
8 changed files with 98 additions and 14 deletions

View file

@ -9,10 +9,11 @@
[string]$MoveProcessedTo="done",
[switch]$Flatten,
[switch]$MuteSkipped,
[switch]$Force
[switch]$Force,
[ScriptBlock]$GetExtractCmd
)
function Get7zExtractCmd {
$local:Get7zExtractCmd = {
param([string]$ArchivePath,[string]$TmpPath)
$local:7zCmd = "7z "
if( $Flatten ) {
@ -24,6 +25,16 @@ function Get7zExtractCmd {
$7zCmd
}
$local:GetUnrarExtractCmd = {
param([string]$ArchivePath,[string]$TmpPath)
"unrar x $(Get-ShellSafePath $ArchivePath)"
}
if( -not $GetExtractCmd ) {
$GetExtractCmd = $Get7zExtractCmd
if( Get-Command unrar -ErrorAction Ignore ) {
$GetExtractCmd = $GetUnrarExtractCmd
}
}
function Get7zCompressCmd {
param([string]$ArchivePath),[string]$CompressOptions
@ -41,12 +52,11 @@ $local:DonePath = [string]::Empty
if( $MoveProcessedTo ) {
$DonePath = Get-Path $MoveProcessedTo
}
Write-Verbose "`n`tTempPath: $TempPath;`n`tDestPathRoot: $DestPathRoot;`n`tDonePath: $DonePath"
$local:runTimeSpan = [System.Diagnostics.Stopwatch]::StartNew()
$local:lastStatus = $runTimeSpan.Elapsed
$SourcePath = Get-Path "$SourcePath/"
$SourcePath = Get-Path $SourcePath
$local:srcSet = Get-ChildItem -Recurse:$Recurse -LiteralPath $SourcePath -Include $SourceSet
$local:progress = [ordered]@{
@ -56,6 +66,8 @@ $local:progress = [ordered]@{
Elapsed = $runTimeSpan.Elapsed
}
Write-Verbose "`n`tSourcePath: $SourcePath;`n`tTempPath: $TempPath;`n`tDestPathRoot: $DestPathRoot;`n`tDonePath: $DonePath"
foreach( $local:srcFile in $srcSet ) {
Write-Verbose "Processing $($srcFile.Name)..."
$progress.ItemProgress++
@ -66,41 +78,43 @@ foreach( $local:srcFile in $srcSet ) {
if( Test-Path -LiteralPath $TempPath ) {
Remove-Item -Recurse -Force -LiteralPath $TempPath -ErrorAction Stop
}
$null = New-Item -ItemType Directory -Force -Path $TempPath -ErrorAction Stop -WhatIf:$false
$local:relPath = $srcFile.DirectoryName.Replace($SourcePath,[string]::Empty)
$local:destPath = $srcFile.DirectoryName.Replace($SourcePath,$DestPathRoot)
$local:destZipPath = $(Join-Path $destPath "$($srcFile.BaseName)$ZipExtension")
# Write-Verbose "relPath = $relPath`ndestPath = $destPath`ndestZipPath = $destZipPath"
$local:ActionStatus = "Failed"
Write-Verbose "Dest: $destZipPath"
if( -not $Force -and (Test-Path -LiteralPath $destZipPath) ) {
Write-Verbose "Skipping, dest already exists: `"$($destZipPath.Replace($DestPathRoot,[string]::Empty))`""
$ActionStatus = "Skipped"
} else {
$local:nextCmd = Get7zExtractCmd -ArchivePath $srcFile.FullName -TmpPath $TempPath
Push-Location -LiteralPath $TempPath -ErrorAction Stop
$local:nextCmd = Invoke-Command $GetExtractCmd -ArgumentList @( $srcFile.FullName, $TempPath )
$null = Invoke-ExpressionEx $nextCmd
if( -not (Test-Path -PathType Container -LiteralPath $TempPath) ) {
if( -not (Get-ChildItem -Recurse -File -LiteralPath $TempPath).Length ) {
Write-Warning "Failed to convert file:`n`t$($srcFile.FullName)"
$ActionStatus = "Failed"
} else {
Push-Location -LiteralPath $TempPath -ErrorAction Stop
$local:destFile = Join-Path $destPath "$([System.io.path]::GetRandomFileName()).zip"
$nextCmd = Get7zCompressCmd -ArchivePath $destFile
$null = Invoke-ExpressionEx $nextCmd
Pop-Location
if( -not (Test-Path -LiteralPath $destPath ) ) {
New-Item -Type Directory -LiteralPath $destPath
$null = New-Item -Type Directory -Path $destPath
}
$null = Move-Item -Force:$Force -LiteralPath $destFile $destZipPath
$ActionStatus = "Processed"
}
Pop-Location
}
if( Test-Path -PathType Container -LiteralPath $TempPath ) {
Remove-Item -Recurse -Force -LiteralPath $TempPath
Remove-Item -Recurse -Force -LiteralPath $TempPath -WhatIf:$false
}
if( $DonePath ) {