Windows 使用powershell脚本复制文件夹内容以保留文件夹结构
我的源文件夹结构如下所示 c:\TestResults |-- Log | |-- xyz.pdf | `-- Reports | `-- rp.pdf |-- Keywords | |-- key.txt | | `-- pb.ea | `-- reports |-- Test | |-- 11.pdf | |-- 12 | `-- Log | |-- h1.pdf | `-- Reports | `-- h2.pdf `-- Dev |-- st |-- ea `-- Log `-- Reports `-- h4.pdfWindows 使用powershell脚本复制文件夹内容以保留文件夹结构,windows,powershell,powershell-3.0,Windows,Powershell,Powershell 3.0,我的源文件夹结构如下所示 c:\TestResults |-- Log | |-- xyz.pdf | `-- Reports | `-- rp.pdf |-- Keywords | |-- key.txt | | `-- pb.ea | `-- reports |-- Test | |-- 11.pdf | |-- 12 | `-- Log | |-- h1.pdf | `-- Reports | `-
你所追求的是如下。如果项目的任何部分有\log,它将复制项目和目录
$gci = Get-ChildItem -Path "C:\TestResults" -Recurse
Foreach($item in $gci){
If($item.FullName -like "*\log*"){
Copy-Item -Path $item.FullName -Destination $($item.FullName.Replace("C:\TestResults","C:\Work\Logs\TestResults")) -Force
}
}
这是一个关于codez的问题。我建议您先看看这里,然后尝试一些代码。我将编辑问题,以包含我编写的代码。我遇到的问题是,文件夹结构正在变平,无法维护层次结构。
$baseDir = "c:\TestResults"
$outputDir = "c:\Work\Logs"
$outputLogsDir = $outputDir + "\TestResults"
$nameToFind = "Log"
$paths = Get-ChildItem $baseDir -Recurse | Where-Object { $_.PSIsContainer -and $_.Name.EndsWith($nameToFind)}
if(!(test-path $outputLogsDir))
{
New-Item -ItemType Directory -Force -Path $outputLogsDir
}
foreach($path in $paths)
{
$sourcePath = $path.FullName + "\*"
Get-ChildItem -Path $sourcePath | Copy-Item -Destination $outputLogsDir -Recurse -Container
}
$gci = Get-ChildItem -Path "C:\TestResults" -Recurse
Foreach($item in $gci){
If($item.FullName -like "*\log*"){
Copy-Item -Path $item.FullName -Destination $($item.FullName.Replace("C:\TestResults","C:\Work\Logs\TestResults")) -Force
}
}