gpt4 book ai didi

powershell - 从 get-childitem 搜索中排除路径

转载 作者:行者123 更新时间:2023-12-02 22:38:31 30 4
gpt4 key购买 nike

我正在尝试将文件夹结构中的所有文件复制到也是该结构一部分的文件夹中。因此目标文件夹被排除在搜索之外。我还想排除其路径“.thumbnails”中包含的任何文件夹,但是当我用通配符路径(例如“D:\ZZZ_Phone_test*.thumbnails”)替换 $Skip 中的完整路径时,它将不起作用。

其次,如果可能的话,我想提高效率,以便更快地完成工作。脚本运行时,主要是 CPU 工作,而不是硬盘驱动器。

第三,有什么方法可以生成复制、跳过、错误...的输出并将其保存到日志文件中吗?

$Source = 'D:\ZZZ_Phone_test'
$Dest = 'D:\ZZZ_Phone_test\1\1\BackUp'
$Skip = 'D:\ZZZ_Phone_test\4\.thumbnails'

Get-ChildItem $Source -Directory -Recurse | ? FullName -ne $Dest | ? FullName -ne $Skip | get-ChildItem -File | Copy-Item -Exclude `
*.0,*.1,*.nomedia,*.thumbnail,*.chck,*.crypt12,*.tmp,*.db,*.crypt1,*.ini,*.pdrproj,*.pkpass,*.dat,*.enc,*.lck,*.xml,*.json,*.LOCK,*.443,*.preference `
-Destination $Dest

.

编辑:以下工作正常,但它只会排除名称以“thumbnails”或“BackUp”结尾的目录中的文件。如果“thumbnails”文件夹中有任何包含文件的目录,它们都将被处理。我想定义要排除的文件夹,即使在 $Skip 中定义的目录中有包含文件的子目录,它们也不会被处理。

$Source = 'D:\ZZZ_Phone_test'
$Dest = 'D:\ZZZ_Phone_test\1\1\BackUp'
$Skip = '*thumbnails', '*BackUp'


(Get-ChildItem -Path $Source -Directory -Recurse -Exclude $Skip).FullName |
get-ChildItem -File |
Copy-Item -WhatIf -Exclude `
*.0,*.1,*.nomedia,*.thumbnail,*.chck,*.crypt12,*.tmp,*.db,*.crypt1,*.ini,*.pdrproj,*.pkpass,*.dat,*.enc,*.lck,*.xml,*.json,*.LOCK,*.443,*.preference `
-Destination $Dest

最佳答案

尝试此操作并根据需要修改该文件排除部分...

$Source = 'D:\Temp'
$Dest = 'D:\Destination'
$Skip = '*est', '*here'

<#
Always build you code one use case at a time to ensure you are getting what
you'd expect before moving ot the next.
#>

# Get all directories off a give path
(Get-ChildItem -Path $Source -Directory -Recurse).FullName |
Select-Object -First 5
# Results
<#
D:\Temp\AddressFiles
D:\Temp\ChildFolder
D:\Temp\est
D:\Temp\here
D:\Temp\hold
#>

# Exclude named directories
(Get-ChildItem -Path $Source -Directory -Recurse -Exclude $Skip).FullName |
Select-Object -First 5
# Results
<#
D:\Temp\AddressFiles
D:\Temp\ChildFolder
D:\Temp\ChildFolder\New folder
D:\Temp\ChildFolder\temp
D:\Temp\hold

#>

# Or include only what you want
(Get-ChildItem -Path $Source -Directory -Recurse -Include $Skip).FullName |
Select-Object -First 5
# Results
<#
D:\Temp\ChildFolder\New folder\est
D:\Temp\est
D:\Temp\here
#>

# Loop directories and process files, trap errors
(Get-ChildItem -Path $Source -Directory -Recurse -Exclude $Skip).FullName |
Select-Object -First 5 |
ForEach {
Try
{
"Processing $PSItem"
$CopyItemSplat = @{
Path = (Get-ChildItem -Path $PSItem -ErrorAction Stop).FullName
Destination = $Dest
Verbose = $true
WhatIf = $true
}
}
Catch
{
Write-Warning -Message 'An error was encountered.'
$PSitem.Exception.Message
}
}
# Results
<#
Processing D:\Temp\AddressFiles
Processing D:\Temp\ChildFolder
Processing D:\Temp\ChildFolder\New folder
Processing D:\Temp\ChildFolder\temp
WARNING: An error was encountered.
The property 'FullName' cannot be found on this object. Verify that the property exists.
Processing D:\Temp\hold
WARNING: An error was encountered.
The property 'FullName' cannot be found on this object. Verify that the property exists.
#>

关于powershell - 从 get-childitem 搜索中排除路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63553577/

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