我想复制一堆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个字母命名。 –