gpt4 book ai didi

powershell - 复制项目并排除文件夹

转载 作者:行者123 更新时间:2023-12-04 17:08:37 26 4
gpt4 key购买 nike

我需要复制我所有的 c:\inetpub目录到新位置,但排除以下文件夹及其子文件夹:

c:\inetpub\custerr
c:\inetpub\history
c:\inetpub\logs
c:\inetpub\temp
c:\inetpub\wwwroot

到目前为止,我正在这样做:
# 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:\路径(我们可以解决这个问题,但我将要展示的内容应该足够好了。)。

然后使用 Get-ChildItem获取 inetpub 目录中的所有文件夹。使用 -notin 过滤掉排除项并将其余的传递给 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 才能工作。

关于powershell - 复制项目并排除文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32098220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com