将文件复制到文件夹和所有子文件夹中

Copy a file into a folder and all subfolders

我有一个文件夹 'A',它有 2 个子文件夹 'A-1' & 'A-2' 和 'A-1' 还有 2 个子文件夹 'A-1-1' 和 'A-1-2' 在下面。

我想在所有这些文件夹中复制一个文件。 下面是我的 powershell script.The 问题是文件被复制到一些文件夹,对于其他文件夹,我得到一个 'path not found error' 你能建议我怎么做才能让它工作吗?

$folders = Get-ChildItem C:\DestinationFolder\A\ -Directory -Recurse
foreach ($folder in $folders) {
    Copy-Item -Path "C:\Filetocopy\abc.txt" -Destination "C:\DestinationFolder\A$($folder.name)" -Recurse
}

也许你可以使用这个:

$folders = Get-ChildItem C:\DestinationFolder\A\ -Directory -Recurse

foreach ($folder in $folders) 
{
    Copy-Item -Path "C:\Filetocopy\abc.txt" -Destination $folder.fullname
}

试试这个:

Get-ChildItem C:\DestinationFolder\A -Directory -Recurse | Copy-Item "C:\Filetocopy3.csv" -Destination {$_.fullname}

非纯粹主义者的简短版本:

gci C:\DestinationFolder\A -Dir -Rec | cpi "C:\Filetocopy3.csv" -D {$_.fullname}