gpt4 book ai didi

powershell - 如何从Powershell中7Zip的错误结果中获取错误文件名?

转载 作者:行者123 更新时间:2023-12-03 08:18:04 33 4
gpt4 key购买 nike

我浏览了许多帖子,但找不到对此问题的引用。我有一个代码,用于测试名称中具有特定模式的一组zip文件。经过测试,我看到以下格式的错误和没有错误的文件。我可以用有错误的存档来解析它,但是我无法获得错误文件的名称。
我的最终目标是从下面的7zip错误结果中获取所有错误文件的文件名。
你能帮我这个忙吗?
谢谢


7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 40021368 bytes (39 MiB)

Testing archive: C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#10-AT_2020-08-06_13268.txt.gz
--
Path = C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#10-AT_2020-08-06_13268.txt.gz
Type = gzip
Headers Size = 10


Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 40021368 bytes (39 MiB)

Testing archive: C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#210-AT_2020-08-06_13268.txt - Copy.gz
--
Path = C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#210-AT_2020-08-06_13268.txt - Copy.gz
Type = gzip
Headers Size = 10


Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 56581 bytes (56 KiB)

Testing archive: C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#DT-F@_2020-08-06_13268.txt.gz
--
Path = C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#DT-F@_2020-08-06_13268.txt.gz
Type = gzip
Headers Size = 10

Everything is Ok

Size: 504716
Compressed: 56581

最佳答案

我将在7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30行上拆分此日志的内容,并使用Where-Object过滤仅包含Archives with Errors的块。
然后使用正则表达式获取Path =文件名
对于演示,我使用Here-String。
在现实生活中,您可能需要使用以下命令从文件中加载该文件

$zipLog = Get-Content -Path 'The7ZipErrorLog.txt' -Raw
(此处需要 -Raw才能在单个多行字符串中获得全部字符串)
$zipLog = @"
7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 40021368 bytes (39 MiB)

Testing archive: C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#10-AT_2020-08-06_13268.txt.gz
--
Path = C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#10-AT_2020-08-06_13268.txt.gz
Type = gzip
Headers Size = 10


Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 40021368 bytes (39 MiB)

Testing archive: C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#210-AT_2020-08-06_13268.txt - Copy.gz
--
Path = C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#210-AT_2020-08-06_13268.txt - Copy.gz
Type = gzip
Headers Size = 10


Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 56581 bytes (56 KiB)

Testing archive: C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#DT-F@_2020-08-06_13268.txt.gz
--
Path = C:\Users\Lozzy\Documents\ARDF\broken\Cart_Weel_#DT-F@_2020-08-06_13268.txt.gz
Type = gzip
Headers Size = 10

Everything is Ok

Size: 504716
Compressed: 56581
"@

$zipLog -split '7-Zip.+Igor Pavlov.+\d{4}-\d{2}-\d{2}' | Where-Object { $_ -match 'Archives with Errors' } | ForEach-Object {
([regex] '(?im)^Path = (.+)').Match($_).Groups[1].Value
}
输出:
C:\ Users \ Lozzy \ Documents \ ARDF \ broken \ Cart_Weel_#10-AT_2020-08-06_13268.txt.gz
C:\ Users \ Lozzy \ Documents \ ARDF \ broken \ Cart_Weel_#210-AT_2020-08-06_13268.txt-Copy.gz

在正则表达式中, (?im)表示不使Match工作区分大小写,并且让 ^$ anchor 在换行符处匹配,因为每个文本bloxk是多行字符串。

关于powershell - 如何从Powershell中7Zip的错误结果中获取错误文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63440299/

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