I wanted to copy a bunch of jpg/gif files with starting Name into another location. It works not as intended. It creates addition folders which stays emtpy and do not copy all subfolders. Then I run again through the whole path and remove all empty folders.
How can I prevent Copy-Item from creating addition empty folders? I want to keep the current path with a new root and add a new subdirectory with the starting letters of the filename and put all the files there.
current folder structure
F:\pics\2016\071310
|
-----> K001
|
------> 0494434-002394
------> 0494434-002394
.
.
------> K0073
wanted folder structure
C:\test\2016\071310
|
-----> K001
|
------> 0494434-002394
|
----> K-G
----> K-F
.
.
------> 0494434-002394
|
----> K-G
----> K-F
.
.
.
.
------> K0073
Here is my code
$source = "F:\pics\2016\071310"
$destination = "C:\test"
$folder = "K-G\"
$filterKG = [regex] "^K-G.*\.(jpg|gif)"
$bin = Get-ChildItem -Exclude $folder -Path $source -Recurse | Where-Object {($_.Name -match $filterKG) -or ($_.PSIsContainer)}
foreach ($item in $bin){
$new_folder = $item.FullName.ToString().Replace($source,$destination).Replace($item.Name,$folder)
if(-not (Test-Path $new_folder)){
New-Item -Type dir $new_folder
Write-Host $new_folder
}
Copy-Item $item.FullName -Destination $item.FullName.ToString().Replace($source,$destination).Replace($item.Name,$folder+$item.Name)
}
#remove empty folders which where added
$tdc="C:\test"
do {
$dirs = gci $tdc -directory -recurse | Where { (gci $_.fullName -Force).count -eq 0 } | select -expandproperty FullName
$dirs | Foreach-Object { Remove-Item $_ }
} while ($dirs.count -gt 0)