需要递归地将单个文件复制到所有子文件夹

Need to copy single file to all subfolders recursively

谁能帮我创建一个 powershell 或 CMD 脚本,尽可能简单(1 行?),它将执行以下操作...

-取文件(c:\test.txt) - 复制到给定文件夹内的所有子文件夹,包括多个层次 例如,c:\test1\ 和 c:\test2\ -如果该文件已经存在则不覆盖该文件 -不更改任何现有文件。如果原始txt文件不存在,直接添加。

我已经尝试了一堆我在网上找到的脚本,并对其进行了更改,但都没有成功。它要么覆盖现有文件,要么不深入多个级别,要么跳过 top/bottom 级别之间的所有文件夹,或者抛出错误。我放弃了。

谢谢 马特

不是单线的,但是给你:

$rootFolder = 'PATH OF FOLDER CONTAINING ALL THE SUBFOLDERS'
$fileToCopy = 'c:\test.txt'
$fileName   = [System.IO.Path]::GetFileName($fileToCopy)
Get-ChildItem -Path $rootFolder -Recurse -Directory | ForEach-Object {
    if (!(Test-Path -Path (Join-Path -Path $_.FullName -ChildPath $fileName) -PathType Leaf)) {
        Copy-Item -Path $fileToCopy -Destination $_.FullName
    }
}

这样的事情怎么样...

$folders = Get-ChildItem -Recurse -Path C:\temp -Directory
$file = "c:\temp\test.txt"

foreach($folder in $folders){
    $checkFile = $folder.FullName + "\test.txt"
    $testForFile=Test-Path -Path $checkFile
    if(!$testForFile){
        Copy-Item $file -Destination $folder.FullName
    }  
}