PowerShell Copy-Item 中的排除列表似乎不起作用 [英] Exclude list in PowerShell Copy-Item does not appear to be working

查看:212
本文介绍了PowerShell Copy-Item 中的排除列表似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 PowerShell 脚本片段:

I have the following snippet of PowerShell script:

$source = 'd:\t1\*'
$dest = 'd:\t2'
$exclude = @('*.pdb','*.config')
Copy-Item $source $dest -Recurse -Force -Exclude $exclude

它可以将所有文件和文件夹从 t1 复制到 t2,但它只排除根"/一级"文件夹中的排除列表,而不排除子文件夹中的排除列表.

Which works to copy all files and folders from t1 to t2, but it only excludes the exclude list in the "root"/"first-level" folder and not in sub-folders.

如何让它排除所有文件夹中的排除列表?

How do I make it exclude the exclude list in all folders?

推荐答案

我认为最好的方法是在 Copy-Item 命令中使用 Get-ChildItem 和管道.

I think the best way is to use Get-ChildItem and pipe in the Copy-Item command.

我发现这有效:

$source = 'd:\t1'
$dest = 'd:\t2'
$exclude = @('*.pdb','*.config')
Get-ChildItem $source -Recurse -Exclude $exclude | Copy-Item -Destination {Join-Path $dest $_.FullName.Substring($source.length)}

基本上,这里发生的事情是您正在逐个浏览有效文件,然后将它们复制到新路径.最后的Join-Path"语句是为了在复制文件时也保留目录.该部分获取目标目录并将其与源路径后的目录连接.

Basically, what is happening here is that you're going through the valid files one by one, then copying them to the new path. The 'Join-Path' statement at the end is so that the directories are also kept when copying over the files. That part takes the destination directory and joins it with the directory after the source path.

我从这里得到这个想法,然后修改让它适用于这个例子.

I got the idea from here, and then modified it a bit to make it work for this example.

我希望它有效!

这篇关于PowerShell Copy-Item 中的排除列表似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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