复制-项目排除子文件夹 [英] Copy-item exclude Sub-folders

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

问题描述

正在尝试让我的Copy-Item复制目录中除子文件夹以外的所有内容。我可以在文件夹和文件中排除,但不能排除子文件夹。

我尝试在Copy-Item中使用Get-Child和-Exclude,但没有像我希望的那样排除它们


$exclude = "foldercommon"

Get-ChildItem "c:	est" -Directory | 
    Where-Object{$_.Name -notin $exclude} | 
    Copy-Item -Destination 'C:ackup' -Recurse -Force

希望公用文件夹存在,但其中的内容不会被复制。

感谢您的帮助

推荐答案

我认为这应该可以满足您的需求:

$sourceFolder = 'C:	est'
$destination  = 'C:ackup'
$exclude      = @("foldercommon")  # add more folders to exclude if you like

# create a regex of the folders to exclude
# each folder will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($exclude | ForEach-Object { [Regex]::Escape($_) }) -join '|'

Get-ChildItem -Path $sourceFolder -Recurse -File | 
     Where-Object{ $_.DirectoryName -notmatch $notThese } | 
     ForEach-Object {
        $target = Join-Path -Path $destination -ChildPath $_.DirectoryName.Substring($sourceFolder.Length)
        if (!(Test-Path -Path $target -PathType Container)) {
            New-Item -Path $target -ItemType Directory | Out-Null
        }
        $_ | Copy-Item -Destination $target -Force
     }

希望能有所帮助

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

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