复制项目并排除文件夹 [英] Copy-Item and exclude folders
问题描述
我需要将我的所有 c:\inetpub
目录复制到一个新位置,但排除以下文件夹及其子文件夹:
I need to copy all of my c:\inetpub
directory to a new location but exclude the following folders and their subfolders:
c:\inetpub\custerr
c:\inetpub\history
c:\inetpub\logs
c:\inetpub\temp
c:\inetpub\wwwroot
到目前为止,我正在这样做:
So far I am doing this:
# Directory name is created with a format string
$dirName = "\\servername\folder1 _ {0}\inetpub" -f (get-date).ToString("yyyy-MM-dd-hh-mm-ss")
$dirName # Check the output
# Create dir if needed
if(-not (test-path $dirName)) {
md $dirName | out-null
} else {
write-host "$dirName already exists!"
}
#Copy Backup File to Dir
Copy-Item "\\servername\c$\inetpub\*" $dirName -recurse
推荐答案
这是您可以执行的操作的简单示例.构建要排除的父文件夹的数组.由于您是通过 UNC 路径访问它们,因此我们无法真正使用 c:\ 路径(我们可以解决这个问题,但我将要展示的内容应该足够了.)
This is a simple example of something you could do. Build an array of the parent folders that you want to exclude. Since you are accessing them via UNC paths we cannot really use the c:\ path (We can get around this but what I am about to show should be good enough.).
然后使用Get-ChildItem
获取inetpub目录下的所有文件夹.使用 -notin
过滤掉排除项并将其余部分传递给 Copy-Item
Then use Get-ChildItem
to get all the folders in the inetpub directory. Filter out the exclusions using -notin
and pass the rest to Copy-Item
$excludes = "custerr","history","logs","temp","wwwroot"
Get-ChildItem "c:\temp\test" -Directory |
Where-Object{$_.Name -notin $excludes} |
Copy-Item -Destination $dirName -Recurse -Force
您至少需要 PowerShell 3.0 才能运行.
You need at least PowerShell 3.0 for this to work.
这篇关于复制项目并排除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!