使用PowerShell在源服务器的同一目录结构中的文件夹和子文件夹中复制项目文件

时间:2013-07-24 19:07:22

标签: powershell powershell-v2.0 powershell-v3.0

我很难在下面的脚本中努力工作,以正确的结构(作为源服务器)复制文件夹和子文件夹中的文件。

可以说,下面提到了文件夹:

主文件夹:文件aaa,文件bbb

子文件夹a:文件1,文件2,文件3

子文件夹b:文件4,文件5,文件6

使用的脚本:

Get-ChildItem -Path \\Server1\Test -recurse | ForEach-Object {
Copy-Item -LiteralPath $_.FullName -Destination \\server2\test |
Get-Acl -Path $_.FullName | Set-Acl -Path "\\server2\test\$(Split-Path -Path $_.FullName -Leaf)"

}

输出: 文件aaa,文件bbb

子文件夹a(空文件夹)

子文件夹b(空文件夹)

文件1,文件2,文件3,文件4,文件5,文件6.

我希望将文件复制到各自的文件夹(如源文件夹)。任何进一步的帮助都非常感谢。

5 个答案:

答案 0 :(得分:58)

这可以使用Copy-Item完成。无需使用Get-Childitem。我想你只是在思考它。

Copy-Item -Path C:\MyFolder -Destination \\Server\MyFolder -recurse -Force

我刚试过它,它对我有用。

答案 1 :(得分:1)

我一次找到此脚本,此副本文件夹和文件,并在目标位置保留源的相同结构时,可以尝试一下。

# Find the source files
$sourceDir="X:\sourceFolder"

# Set the target file
$targetDir="Y:\Destfolder\"
Get-ChildItem $sourceDir -Include *.* -Recurse |  foreach {

    # Remove the original  root folder
    $split = $_.Fullname  -split '\\'
    $DestFile =  $split[1..($split.Length - 1)] -join '\' 

    # Build the new  destination file path
    $DestFile = $targetDir+$DestFile

    # Move-Item won't  create the folder structure so we have to 
    # create a blank file  and then overwrite it
    $null = New-Item -Path  $DestFile -Type File -Force
    Move-Item -Path  $_.FullName -Destination $DestFile -Force
}

答案 2 :(得分:0)

如果您想将相同的内容从源镜像到目的地,请尝试遵循以下内容。

function CopyFilesToFolder ($fromFolder, $toFolder) {
    $childItems = Get-ChildItem $fromFolder
    $childItems | ForEach-Object {
         Copy-Item -Path $_.FullName -Destination $toFolder -Recurse -Force
    }
}

测试:

CopyFilesToFolder "C:\temp\q" "c:\temp\w"

答案 3 :(得分:0)

我无法回答最流行的答案(思考过多)。它将AFolder放在\ Server \ MyFolder \ AFolder中,我希望在MyFolder中找到AFolder及以下内容。这没用。

Copy-Item -Verbose -Path C:\MyFolder\AFolder -Destination \\Server\MyFolder -recurse -Force

另外,我需要过滤并仅复制* .config文件。

使用“ \ *”无效,因为它没有递归

Copy-Item -Verbose -Path C:\MyFolder\AFolder\* -Filter *.config -Destination \\Server\MyFolder -recurse -Force

我最终放弃了路径字符串的开头,以相对于我从中进行递归的位置获取childPath。这适用于所讨论的用例,并包含许多子目录,而其他一些解决方案则没有。

Get-Childitem -Path "$($sourcePath)/**/*.config" -Recurse | 
ForEach-Object {
  $childPath = "$_".substring($sourcePath.length+1)
  $dest = "$($destPath)\$($childPath)" #this puts a \ between dest and child path
  Copy-Item -Verbose -Path $_ -Destination $dest -Force   
}

答案 4 :(得分:-1)

我想要一个解决方案来复制在特定日期和时间之后修改的文件,这意味着我不需要使用通过过滤器管道的Get-ChildItem。以下是我提出的建议:

$SourceFolder = "C:\Users\RCoode\Documents\Visual Studio 2010\Projects\MyProject"
$ArchiveFolder = "J:\Temp\Robin\Deploy\MyProject"
$ChangesStarted = New-Object System.DateTime(2013,10,16,11,0,0)
$IncludeFiles = ("*.vb","*.cs","*.aspx","*.js","*.css")

Get-ChildItem $SourceFolder -Recurse -Include $IncludeFiles | Where-Object {$_.LastWriteTime -gt $ChangesStarted} | ForEach-Object {
    $PathArray = $_.FullName.Replace($SourceFolder,"").ToString().Split('\') 

    $Folder = $ArchiveFolder

    for ($i=1; $i -lt $PathArray.length-1; $i++) {
        $Folder += "\" + $PathArray[$i]
        if (!(Test-Path $Folder)) {
            New-Item -ItemType directory -Path $Folder
        }
    }   
    $NewPath = Join-Path $ArchiveFolder $_.FullName.Replace($SourceFolder,"")

    Copy-Item $_.FullName -Destination $NewPath  
}