2

On windows 10 I have a 7z archive with about 300 top level folders and each folder contains around 20-30 text files. I want to do a search with the 7z list command in powershell for certain folder names, but the results always contain both folder names and file names. Is there some way to make the results show only folders and not files? I've read through the 7z documentation and tried messing with various switches like -i, -x, and -r to get what I want but so far no luck. I always get folders and files in the list or I get nothing at all.

I guess I could always extract the archive and search in windows explorer but I'd really rather not have to do that every time.

robynChill
  • 21
  • 2
  • 1
    I'm surprised no utility exists to just be able to look inside without having to decompress. Several such apps are available for Mac; I don't see anything for Win [or my google-fu is failing me;) – Tetsujin Mar 18 '22 at 13:53
  • 1
    @Tetsujin You don't need to decompress. See my answer. – DavidPostill Mar 18 '22 at 15:54
  • @DavidPostill - Appreciated. I'm just surprised you can't yet do such as this in Windows - https://i.stack.imgur.com/TEi6E.png - just an example file, but nothing is yet decompressed, it's just "looking in" to the file, with one doc opened in QuickLook [just tap spacebar on any selected document type to view it]. From there you could open it in Word, edit & save - still without unzipping the 7z, or pretty much any other archive format. – Tetsujin Mar 18 '22 at 16:30
  • 1
    @Tetsujin See [How to edit a file in a zip or rar archive?](https://superuser.com/a/756896) – DavidPostill Mar 18 '22 at 16:53
  • @DavidPostill - ahh, better… still a tad Rube Goldberg, though ;) – Tetsujin Mar 18 '22 at 16:55

1 Answers1

4

I want to do a search with the 7z list command in powershell for certain folder names

Here is a batch file solution. Feel free to convert it to PowerShell (it shouldn't be too difficult).

@echo off
setlocal enabledelayedexpansion
for /F "tokens=6" %%d in ('c:\apps\7-zip\7z l -ba test.7z ^| findstr /c:"D...."') do (
   echo %%d
   )
endlocal

The important bits are the -be flag on the 7z command (which is an undocument switch). This changes the list format to something that looks like:

F:\test>c:\apps\7-zip\7z l -ba test.7z
2019-11-19 21:13:50 D....            0            0  test
2020-04-02 10:31:49 D....            0            0  test\colors
2020-04-06 23:32:17 D....            0            0  test\colors\colors
2020-04-16 21:20:02 D....            0            0  test\sub 1
2021-12-26 19:26:10 D....            0            0  test\sub 1\test.bat
2020-04-16 21:20:00 D....            0            0  test\sub 2
2021-04-30 19:37:15 D....            0            0  test\txt
2021-01-27 18:40:08 ....A            0            0  test\1.mp4
2021-01-02 16:07:34 ....A            0            0  test\2.mp4
2021-01-02 16:07:34 ....A            0            0  test\3.mp4
2020-04-16 21:07:16 ....A            0            0  test\Apr
2020-04-06 23:32:29 ....A            0            0  test\colors\colors\blue.txt
2020-04-06 23:32:29 ....A            0            0  test\colors\colors\green.txt
2020-04-06 23:32:29 ....A            0            0  test\colors\colors\red.txt
2020-04-06 23:44:29 ....A            0            0  test\colors\colors\sky blue pink.txt
etc ...

The output lists the directories first and then the files.

Directories contain the string D.... in token (column) 3 so you can filter on this. The directory names are in token (column) 6.

Once you have the directory names you can then filter again to find the name you want.

Example output:

F:\test>test
test
test\colors
test\colors\colors
test\sub
test\sub
test\sub
test\txt
F:\test>

Further Reading

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
  • Ok thanks. I was hoping there was a way to do this without using fancy batch file stuff but I'm thinking the 7z list command just isn't that advanced. But I am wondering is it possible for a directory to have more than just the 'D' attribute? I don't know what the other attributes are but that could mess up the filter if the directory has them too right? – robynChill Mar 18 '22 at 23:50
  • @robynChill Yeah, maybe. I'm not entirely sure what those attributes would look like for a directory. – DavidPostill Mar 19 '22 at 07:41