如何复制带有子文件夹的文件夹? [英] How to copy folder with subfolders?

查看:91
本文介绍了如何复制带有子文件夹的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此脚本在 PowerShell 中完美运行.它复制具有特定类型的所有文件.但我想用文件夹和文件夹复制文件子文件夹.

This script works perfectly in PowerShell. It copies all files with specific type. But I want copy files with it folders & subfolders.

$dest  = "C:\example"
$files = Get-ChildItem -Path "C:\example" -Filter "*.msg" -Recurse

foreach ($file in $files) {
    $file_path = Join-Path -Path $dest -ChildPath $file.Name

    $i = 1

    while (Test-Path -Path $file_path) {
        $i++
        $file_path = Join-Path -Path $dest -ChildPath
        "$($file.BaseName)_$($i)$($file.Extension)"
    }

    Copy-Item -Path $file.FullName -Destination $file_path
}

推荐答案

PowerTip:使用 PowerShell 复制项目并保留文件夹结构

来源: https://blogs.technet.microsoft.com/heyscriptingguy/2013/07/04/powertip-use-powershell-to-copy-items-and-retain-文件夹结构/

问题:如何使用 Windows PowerShell 3.0 将文件夹结构从驱动器复制到网络共享,并保留原始结构?

Question: How can I use Windows PowerShell 3.0 to copy a folder structure from a drive to a network share, and retain the original structure?

答案: 使用 Copy-Item cmdlet 并指定 –Container 切换参数:

Answer: Use the Copy-Item cmdlet and specify the –Container switched parameter:

$sourceRoot = "C:\temp"
$destinationRoot = "C:\n"

Copy-Item -Path $sourceRoot -Filter "*.txt" -Recurse -Destination $destinationRoot -Container

这篇关于如何复制带有子文件夹的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆