Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fetch the filename of a specific file inside a zip archive using Powershell?

I have a zip archive that I need to inspect. I need to find the file name of a specific file with the file extension .serverrevision using Powershell.

There will only be one file with this file extension in the zip archive. The file name will be something like "2.1.4.serverrevision". I need to extract the version number, i.e. 2.1.4 in this example.

I know that I can use the following method to list the contents of the zip archive:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[IO.Compression.ZipFile]::OpenRead($ziparchive.FullName).Entries.FullName | %{ "$ziparchive`:$_" }

But I cannot figure out how to search for that file extension in that list that the function returns and then bring out the filename. The file name could be something like 2.1.4.serverrevision.

Any suggestions?

>Solution :

Use a combination of the .Where() and .ForEach() array methods: .Where() to filter and .ForEach() to transform (extract the name part of interest):

[IO.Compression.ZipFile]::OpenRead($ziparchive.FullName).Entries.FullName.
  Where({ [IO.Path]::GetExtension($_) -eq '.serverrevision' }, 'First').
  ForEach({ [IO.Path]::GetFileNameWithoutExtension($_) } 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading