我想複製一堆jpg/gif文件與起始名稱到另一個位置。它不符合預期。它會創建添加文件夾,保持靜態並且不會複製所有子文件夾。然後我再次運行整個路徑並刪除所有空文件夾。powershell - 複製項目創建空文件夾
如何防止Copy-Item創建添加空文件夾?我想用新的根目錄保留當前路徑,並用文件名的起始字母添加一個新的子目錄,並將所有文件放在那裏。
當前文件夾結構
F:\pics\2016\071310
|
-----> K001
|
------> 0494434-002394
------> 0494434-002394
.
.
------> K0073
想要的文件夾結構
C:\test\2016\071310
|
-----> K001
|
------> 0494434-002394
|
----> K-G
----> K-F
.
.
------> 0494434-002394
|
----> K-G
----> K-F
.
.
.
.
------> K0073
這裏是我的代碼
$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)
我不是你想要什麼明確的。你說你想要_「保留當前路徑和一個新的根目錄,並添加一個新的子目錄與文件名的起始字母,並把所有的文件」._。但想要的文件夾結構看起來像複製的文件在目的地'K001'的根目錄,並且有空的子目錄'K-G'等? – gms0ulman
在覈心中,我想只複製以最深的子文件夾中最深的文件夾中最深的子文件夾中以K-G開頭的所有文件,該子文件夾以該文件的前3個字母命名。 –