0

I'm working on a batch script that requires to count the no. of lines in a file. I am currently using the below code to find the no. of lines.

set "cmd=findstr /R /N "^^" File_name.txt | find /C ":""
for /f %%a in ('!cmd!') do set nooflines=%%a

How to achieve the same in case the file is compressed in zip format? Can we bypass unzipping the file and calculating the no. of lines (Like for linux, we use zcat) ?

Arun
  • 65
  • 1
  • 9

1 Answers1

0

The zcat utility only works with the gzip or gunzip compress files. It works using the tar compression technique.

Windows also has a tar command which is more limited in use, however it can list the filenames so we can search a zips contents by filename instantly without expanding.

C:\Users\My\Downloads>tar -tf xpdf-tools-win-4.03.zip xpdf* | find "pdft"

xpdf-tools-win-4.03/bin32/pdftopng.exe
xpdf-tools-win-4.03/bin32/pdftoppm.exe
xpdf-tools-win-4.03/bin32/pdftohtml.exe
xpdf-tools-win-4.03/bin32/pdftotext.exe
xpdf-tools-win-4.03/bin32/pdftops.exe
xpdf-tools-win-4.03/bin64/pdftopng.exe
xpdf-tools-win-4.03/bin64/pdftoppm.exe
xpdf-tools-win-4.03/bin64/pdftohtml.exe
xpdf-tools-win-4.03/bin64/pdftotext.exe
xpdf-tools-win-4.03/bin64/pdftops.exe
xpdf-tools-win-4.03/doc/pdftoppm.txt
xpdf-tools-win-4.03/doc/pdftopng.txt
xpdf-tools-win-4.03/doc/pdftops.txt
xpdf-tools-win-4.03/doc/pdftotext.txt
xpdf-tools-win-4.03/doc/pdftohtml.txt

C:\Users\My\Downloads>

Using windows GUI search you can find all those files in a zip that contain "Fred" it can seem slow if the files are not pre-indexed.

Any compressed format can be "swollen" in memory with an expansion library. It would be so much simpler to use 7za or similar to expand an archive. Even VBS scripts and powershell can expand to a temporary file or folder from zipped files (a blob or RAM disk provides little value over a file cache of one zip file) then parse those %temp%orary file(s) with find.

Unzip2test.vbs filename.zip


ZipFile = WScript.Arguments.Unnamed(0)
ExtractTo ="c:\temp\test"

Set fso = CreateObject("Scripting.FileSystemObject")
sourceFile = fso.GetAbsolutePathName(ZipFile)
destFolder = fso.GetFolder(ExtractTo)
 
Set objShell = CreateObject("Shell.Application")
Set FilesInZip=objShell.NameSpace(sourceFile).Items()
objShell.NameSpace(destFolder).copyHere(FilesInZip), 16
 
Set fso = Nothing
Set objShell = Nothing
Set FilesInZip = Nothing

then dropping xpdf-tools-win-4.03.zip onto this xpdfrColons.cmd file

@echo off
Unzip2test.vbs %1
for /R "c:\temp\test" %%a in (xpdfr*.txt) do @type %%a | find /c ":" 
pause

would show as

17

press any key ...

There are some zip extractors that can very rapidly search a zip file for a string, much faster than windows search. And dedicated zipfile indexing using command line tools can knock the spots of windows word search.

K J
  • 8,045
  • 3
  • 14
  • 36