PowerShell_Scripts/base/Move-3Way.ps1

57 lines
2.1 KiB
PowerShell

[CmdletBinding(SupportsShouldProcess,ConfirmImpact='Medium')]param(
[string]$BasePath,
[Parameter(Mandatory,Position=1)]
[string]$SourcePath,
[Parameter(Mandatory,Position=2)]
[string]$DestinationPath,
$Logic = @(@{ FilterScript = { $flase } }),
[ScriptBlock]$SrcTransform,
[switch]$NonRecursive,
[switch]$NoPassThru,
$SubtitutionTable,
[int]$Limit,
[Alias("Do")]
[switch]$NoSimulate
)
$SourcePath = Join-Path $SourcePath '.' | Get-Item | Select-Object -ExpandProperty FullName
if( $BasePath ) {
$BasePath = Join-Path $BasePath '.' | Get-Item | Select-Object -ExpandProperty FullName
} else {
$BasePath = $SourcePath
}
$DestinationPath = Join-Path $DestinationPath '.' | Get-Item | Select-Object -ExpandProperty FullName
if( -not (Test-Path $BasePath,$SourcePath) ) {
throw "Path does not exist"
}
$local:limitParam = @{}
if( $Limit ) { $limitParam.First = $Limit }
Get-ChildItem -LiteralPath $BasePath -File -Recurse | Where-Object {
foreach( $local:condition in $Logic ) {
if( -not ($_ | Where-Object @condition) ) {
return $false
}
}
$true
} | Sort-Object -Property FullName |
Select-Object @limitParam | ForEach-Object {
$local:srcPath = $_.FullName
if( $PSBoundParameters.ContainsKey('SrcTransform') ) { $srcPath = Invoke-Command $SrcTransform -ArgumentList @($_) }
$local:path = (Split-Path -Parent $srcPath.Replace($BasePath,'')) -replace '^[/\\]',''
$local:dstPath = Join-Path $DestinationPath $path $(Split-Path -Leaf $srcPath)
foreach( $local:s in $SubtitutionTable.Keys ) {
$dstPath = $dstPath -replace $s, $SubtitutionTable[$s]
}
if( $srcPath -ceq $dstPath ) { continue }
if( $NoSimulate ) {
# Create destination path if it doesn't exists
$null = New-Item -ItemType Directory -Path $(Join-Path $DestinationPath $path) -Force
Move-Item -LiteralPath $srcPath -Destination $dstPath
} else {
"in $path found: $($_.Name)`nmove : $srcPath`nto +-> $dstPath"
}
}