【问题标题】:Copy a file into a folder and all subfolders将文件复制到文件夹和所有子文件夹中
【发布时间】:2018-06-23 23:17:11
【问题描述】:

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

我想在所有这些文件夹中复制一个文件。 下面是我的powershell脚本。问题是文件被复制到一些文件夹&对于其他文件夹我得到一个“找不到路径错误”你能建议我怎么做才能让它工作吗?

$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
}

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


    【解决方案1】:

    也许你可以用这个:

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

    【讨论】:

    • 第一行的select不是必须的。
    【解决方案2】:

    试试这个:

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

    没有纯粹主义者的短版:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 2022-01-13
      • 2011-07-19
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      相关资源
      最近更新 更多