小编gre*_*ine的帖子

如何使用子文件夹复制文件夹?

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

$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
}
Run Code Online (Sandbox Code Playgroud)

powershell powershell-2.0 powershell-3.0

17
推荐指数
1
解决办法
7万
查看次数