首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Copy-item exclude子文件夹

Copy-item exclude子文件夹
EN

Stack Overflow用户
提问于 2019-09-10 00:40:03
回答 3查看 951关注 0票数 2

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

我尝试在copy-item中使用get- tried和-exclude,但并没有像我希望的那样排除它们

代码语言:javascript
复制
$exclude = "folder\common"

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

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

谢谢你的帮助

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-09-10 02:51:28

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

代码语言:javascript
复制
$sourceFolder = 'C:\test'
$destination  = 'C:\backup'
$exclude      = @("folder\common")  # 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
     }

希望这能有所帮助

票数 6
EN

Stack Overflow用户

发布于 2019-09-10 02:52:13

我认为在Get-ChildItem上使用-exclude参数是可行的:

代码语言:javascript
复制
$exclude = 'Exclude this folder','Exclude this folder 2','Folder3'

Get-ChildItem -Path "Get these folders" -Exclude $exclude | Copy-Item -Destination "Send folders here"
票数 1
EN

Stack Overflow用户

发布于 2019-09-10 01:12:10

下面是一个示例:

代码语言:javascript
复制
$exclude= 'subfolderA'
$path = 'c:\test'

$fileslist = gci $path -Recurse

foreach ($i in 0..$fileslist){  if( -not ($i.Fullname -like "*$($exlusion)*")){ copy-item -path $i.fullname -Destination 'C:\backup'  -Force  } }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57858059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档