Powershell 仅复制具有文件夹结构的选定文件

Powershell copy only selected files with folder structure

我有一个包含很多文件的文件夹层次结构。 我需要复制所有文件夹并且只复制选定的文件。为此,我编写脚本:

$path = "D:\Drop\SOA-ConfigurationManagement - Test1"
$files = Get-ChildItem -Path $path -Recurse | ? { $_.Name -like "system.serviceModel.client.config" }
$Destination = "D:\test\"
Copy-Item $files -Destination $Destination -recurse

当我执行变量 $files 时,它 returns 正确的路径:

但是当我执行 Copy-Item 时它 returns 不是完整路径:

也许我的做法是错误的。如果是这样,如何复制整个文件夹结构,并且只复制选定的文件(在本例中为 system.serviceModel.client.config 文件)?

UPD1 好的,我发现了如何只复制文件夹:

$path = "D:\Drop\SOA-ConfigurationManagement - Test1\"
$Destination = "D:\test\"
Copy-Item $path $Destination -Filter {PSIsContainer} -Recurse -Force

但是如何只复制选定的文件并保留它们的位置? $Destination 变量中需要包含什么?

$files = Get-ChildItem -Path $path -Recurse | ? { $_.Name -like "system.serviceModel.client.config" } | % { Copy-Item -Path $_.FullName -Destination $Destination }

此代码也会使目录结构保持不变

$path = "D:\Drop\SOA-ConfigurationManagement - Test1\"
$Destination = "D:\test\"
$fileName = "system.serviceModel.client.config"
Get-ChildItem -Path $path -Recurse |  ForEach-Object {
    if($_.Name -like $fileName) {
        $dest = "$Destination$(($_.FullName).Replace($path,''))"
        $null = New-Item $dest -Force
        Copy-Item -Path $_.FullName -Destination $dest -Force
    }
}

试试这个

$path = 'D:\Drop\SOA-ConfigurationManagement - Test1\'
$Destination = 'D:\test\'

$files = Get-ChildItem -Path $path -Recurse -File | where Name -like "*system.serviceModel.client.config*" | %{

$Dir=$_.DirectoryName.Replace($path, $Destination)
$NewPAthFile=$_.FullName.Replace($path, $Destination)

#create dir if not exists
New-Item -Path $Dir -ItemType Directory -Force -ErrorAction SilentlyContinue

#copy file in new dir
Copy-Item $_.FullName $NewPAthFile 


}

我建议进行以下改动:

$path = "D:\Drop\SOA-ConfigurationManagement - Test1"
$files = Get-ChildItem -Path $path -Recurse | ? { $_.Name -like "system.serviceModel.client.config" }
$Destination = "D:\test\"
$files | % { $_ | Copy-Item -Destination $Destination -recurse }

您甚至可以将整个副本放在一行中:

$path = "D:\Drop\SOA-ConfigurationManagement - Test1"
$Destination = "D:\test\"
Get-ChildItem -Path $path -Recurse | ? { $_.Name -like "system.serviceModel.client.config" } | % { $_ | Copy-Item -Destination $Destination -recurse }

Copy-Item 可以从输入对象流中找到路径,但它似乎无法将 System.IO.FileInfo 个对象的集合作为 Path 的参数。

要复制整个文件夹结构和具有特定名称的文件,下面的代码应该做你想做的:

$Source      = 'D:\Drop\SOA-ConfigurationManagement - Test1'
$Destination = 'D:\test'
$FileToCopy  = 'system.serviceModel.client.config'

# loop through the source folder recursively and return both files and folders
Get-ChildItem -Path $Source -Recurse | ForEach-Object {
    if ($_.PSIsContainer) {
        # if it's a folder, create the new path from the FullName property
        $targetFolder = Join-Path -Path $Destination -ChildPath $_.FullName.Substring($Source.Length)
        $copyFile = $false
    }
    else {
        # if it's a file, create the new path from the DirectoryName property
        $targetFolder = Join-Path -Path $Destination -ChildPath $_.DirectoryName.Substring($Source.Length)
        # should we copy this file? ($true or $false)
        $copyFile = ($_.Name -like "*$FileToCopy*")
    }
    # create the target folder if this does not exist
    if (!(Test-Path -Path $targetFolder -PathType Container)) {
        $null = New-Item -Path $targetFolder -ItemType Directory
    }
    if ($copyFile) {
        $_ | Copy-Item -Destination $targetFolder -Force
    }
}