|
param ( |
|
[string]$Path = $PSScriptRoot, |
|
[string[]] $Filter = @("*.mp4","*.mkv", "*.avi"), |
|
[string]$DestDir = $null |
|
) |
|
|
|
Clear-Host |
|
|
|
|
|
function MoveFilesToSingleDirectory{ |
|
param ( |
|
[string]$Path, |
|
[string[]] $Filter, |
|
[string]$DestDir = $null |
|
) |
|
|
|
$rootPath = Join-Path -Path $path -ChildPath "\" | Resolve-Path |
|
|
|
If ($DestDir) { |
|
If ((Test-Path -LiteralPath $DestDir) -eq $false) { |
|
|
|
Write-Host -ForegroundColor Yellow 'Destination dir not exists - creating' |
|
|
|
New-Item -Path $DestDir -ItemType Directory |
|
} |
|
|
|
$destinationPath = Join-Path -Path $DestDir -ChildPath "\" | Resolve-Path |
|
|
|
} Else { |
|
$destinationPath = $rootPath |
|
} |
|
|
|
|
|
#Remove-Item "$rootPath*" -Include *.bmp -whatif |
|
|
|
#-Include *.bmp,*.jpg |
|
Get-ChildItem -Path "$rootPath" -Include $filter -Recurse -File | % { |
|
$file = $_ |
|
Write-Host "file: " $file.FullName |
|
|
|
#$fileFolderPath = Split-Path $file.FullName -Parent |
|
#$fileFolder = Split-Path $fileFolderPath -Leaf |
|
|
|
Copy-Rename-Duplicates -sourceFile $file -destinationPath $destinationPath |
|
|
|
Write-Host "------------" |
|
} |
|
|
|
} |
|
|
|
function Copy-Rename-Duplicates |
|
{ |
|
param ( |
|
[parameter(Mandatory=$true)] |
|
[string]$sourceFile, |
|
|
|
[parameter(Mandatory=$true)] |
|
[string]$destinationPath |
|
) |
|
|
|
# [System.IO.File]::Exists($sourceFile) |
|
If ((Test-Path -LiteralPath $sourceFile) -eq $false) { |
|
Write-Host -ForegroundColor Red 'File not exists!' |
|
return |
|
} |
|
|
|
$srcFolderPath = Split-Path $sourceFile -Parent |
|
$dstDirecotry = $destinationPath.TrimEnd("\") | Resolve-Path |
|
|
|
#$fname = Split-Path -Path $sourceFile -Leaf -Resolve |
|
$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($sourceFile) |
|
$fileExtension = [System.IO.Path]::GetExtension($sourceFile) |
|
$fileName = [System.IO.Path]::GetFileName($sourceFile) |
|
|
|
$dstFilePath = Join-Path -Path $destinationPath -ChildPath $fileName |
|
|
|
#Write-Host "srcFolderPath=dstDirecotry: `t`t`t`t $srcFolderPath = $dstDirecotry" |
|
#Write-Host "resolved: fileFolderPath=dstDirecotry: `t $(Resolve-Path $srcFolderPath) == $dstDirecotry" |
|
|
|
if ($srcFolderPath -eq $dstDirecotry) { |
|
Write-Host -ForegroundColor Yellow "SAME location - ignorred" |
|
return |
|
} |
|
|
|
|
|
#Write-Host -ForegroundColor Magenta "filename: $fileName | $fileNameWithoutExtension | $fileExtension | $fname >> $dstFilePath" |
|
|
|
If ((Test-Path -LiteralPath $dstFilePath)) { |
|
Write-Host -ForegroundColor Yellow "File $fileName already exists in destination folder - renaming... >> " -NoNewline |
|
|
|
$i = 0 |
|
$newFileName = $fileName |
|
While ((Test-Path -LiteralPath $dstFilePath)) { |
|
$i += 1 |
|
$newFileName = "__$i`_$fileName" |
|
$dstFilePath = Join-Path -Path $destinationPath -ChildPath $newFileName |
|
#$dstFilePath = Join-Path -Path $destinationPath -ChildPath "$fileNameWithoutExtension($i)$fileExtension" |
|
} |
|
|
|
Write-Host "$newFileName" |
|
|
|
} Else { |
|
#New-Item -ItemType File -Path $dstFilePath -Force |
|
} |
|
|
|
Write-Host -ForegroundColor Magenta ">> $dstFilePath" |
|
|
|
#Copy-Item -Path $sourceFile -Destination $dstFilePath -Force -WhatIf |
|
Move-Item -LiteralPath $sourceFile -Destination $dstFilePath -Force #-WhatIf |
|
} |
|
|
|
|
|
MoveFilesToSingleDirectory -Path $Path -Filter $Filter -DestDir $DestDir |
|
|
|
#Write-Host "Press any key to continue ..." |
|
#$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL |
|
|
|
If (!($psISE)){ |
|
Read-Host 'Press Enter to continue…' | Out-Null |
|
} |