Ashirwad.nivalkar
Ashirwad.nivalkar

Reputation: 23

How can I get only Error file names from error result of 7Zip in Powershell?

I went through many posts but couldn't find a reference to this issue. I have a code that tests a set of zip files with a certain pattern in the name. After testing I see below format for Errored and none errored file. I can parse it with Archives with Error but I am not able to get the name of File in Error. My final aim is to get Filename of all errored files from the below Error result of 7zip.

Can you help me over this, please? Thank you


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

Upvotes: 0

Views: 729

Answers (1)

Theo
Theo

Reputation: 61093

I would split the content of this log on the 7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30 lines and use a Where-Object to filter for only blocks that have Archives with Errors in it.

Then use a regex to get the Path = file name

For demo I'm using a Here-String. In real life you'd probably need to load this from file with

$zipLog = Get-Content -Path 'The7ZipErrorLog.txt' -Raw

(the -Raw is needed here to get all in a single multiline string)

$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
}

Output:

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

In the regex, (?im) means make the Match work case-insensitive and let ^ or $ anchors match at line breaks, because each text bloxk is a multiline string.

Upvotes: 1

Related Questions