Skip to content

Instantly share code, notes, and snippets.

@kkoziarski
Last active August 3, 2016 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkoziarski/4c5259d36576c23efb0a53078231232d to your computer and use it in GitHub Desktop.
Save kkoziarski/4c5259d36576c23efb0a53078231232d to your computer and use it in GitHub Desktop.
Moves all files in given directory and subdirectories to single director

Moves all files in given directory and subdirectories to single folder.

PS> .\MoveFilesToSingleDirectory.ps1 -Path ".\src" -DestDir ".\dst" -Filter *.bmp,*.avi

All parameters are optional

-Path - source directory path, when not provided, script's directory is taken

-Dest - destination directory path, when not provided, -Path directory is taken

-Filter - filter file types to be moved, when not provided, default are taken

When there are duplicates, files will be removed and __{i}_ will be appended to the beginnig of the each duplicated file.

###GIVEN

+-- rootDir
| `-- file-duplicate.txt
| +-- dir1
| | `-- file-1.txt
| | `-- file-duplicate.txt
| | +-- dir1-1
| | | `-- file-1-1.txt
| | | `-- file-1-2.txt
| +-- dir2
| | `-- file-2.txt
| | `-- file-duplicate.txt

###RESULT

+-- destDir
| `-- file-duplicate.txt
| `-- __1_file-duplicate.txt
| `-- __2_file-duplicate.txt
| `-- file-1.txt
| `-- file-1-1.txt
| `-- file-1-2.txt
| `-- file-2.txt
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment