44

I use 7-Zip to compress files inside a batch file like this:

...\right_path\7z a output_file_name.zip file_to_be_compressed

I got the following output:

7-Zip 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
Scanning

Creating archive output_file_name.zip

Compressing  file_to_be_compressed

Everything is Ok

Is it possible to disable this output (that is, I don't want anything to be printed)?

6 Answers6

41

You can use the -bs command to control where output goes. To stop anything but error output, I would add -bso0 -bsp0.

Evan
  • 1,208
33

Just add > NUL: to the end of your command.

12

It is highly recommended to view status messages in the process. To avoid long messages, display only confirmations:

...\right_path\7z a output_file_name.zip file_to_be_compressed | findstr /b /r /c:"\<Everything is Ok" /c:"\<Scanning" /c:"\<Creating archive"
5

Improving Bruno Dermario answer, I wanted to also report errors and be able to check them manually.

...\right_path\7z a output_file_name.zip file_to_be_compressed > 7z_log.txt
type 7z_log.txt | findstr /b /c:"Everything is Ok" /c:"Scanning" /c:"Creating archive" /c:"Error"
echo.
echo (In case of Error check 7z_log.txt)
echo.
3

In case PowerShell is an option or somebody could use it, here's what I did, based on the idea of the findstr answer.

& $sevenZipBin a "$archiveFile" * | where {
    $_ -notmatch "^7-Zip " -and `
    $_ -notmatch "^Scanning$" -and `
    $_ -notmatch "^Creating archive " -and `
    $_ -notmatch "^\s*$" -and `
    $_ -notmatch "^Compressing "
}
if (-not $?)
{
    # Show some error message and possibly exit
}

In normal operation, this leaves only the "Everything is Ok" line. Should anything unusual be printed, it remains visible (except for empty lines as they appear so often in regular output).

This is tested for 7z format output. Other archive formats may produce other messages than "Compressing". Extracting will likely also produce different messages. But you can easily adapt the filter to your needs.

A more complex idea would be to redirect all output to a buffer and only print it in case the command returns an error exit code. This is a method that works with all commands that allow redirecting and provide an accurate error exit code.

ygoe
  • 2,480
  • 8
  • 29
  • 46
2

Sharing my findstr solution:

%ZIP% a -tzip %FILE% %Folder% | findstr /I "archive everything"

So the original 14-lines output:


7-Zip 18.01 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-01-28

Scanning the drive:
4 folders, 13 files, 88957 bytes (87 KiB)

Creating archive: Releases\Archive.zip

Add new data to archive: 4 folders, 13 files, 88957 bytes (87 KiB)


Files read from disk: 13
Archive size: 33913 bytes (34 KiB)
Everything is Ok

shrink to the 4-lines:

Creating archive: Releases\Archive.zip
Add new data to archive: 4 folders, 13 files, 88957 bytes (87 KiB)
Archive size: 33912 bytes (34 KiB)
Everything is Ok

it shrinks only the sOut, warnings and errors goes to the sErr, so you still will see them

yalov
  • 171