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.

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
  • 162,382