Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 使用powershell脚本复制文件夹内容以保留文件夹结构_Windows_Powershell_Powershell 3.0 - Fatal编程技术网

Windows 使用powershell脚本复制文件夹内容以保留文件夹结构

Windows 使用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 | `-

我的源文件夹结构如下所示

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.pdf
你所追求的是如下。如果项目的任何部分有\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
    }
}