41

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)?

Sathyajith Bhat
  • 61,504
  • 38
  • 179
  • 264
Misha Moroshko
  • 7,423
  • 9
  • 26
  • 31

6 Answers6

36

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

Evan
  • 1,158
  • 1
  • 10
  • 12
  • 2
    This is correct, but introduced in 7Zip version 15.01 or after 9.38beta Source: https://sourceforge.net/p/sevenzip/discussion/45797/thread/8a45fa74/ The actual Synology DSM 6.1.x includes 7zip with the version 9.20 and has no such option. – PeterCo Jul 30 '17 at 15:24
  • example: `7z -bso0 x file.zip -o./output_dir/` – SergeyR Oct 03 '22 at 11:29
32

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

Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
  • 3
    What is the colon for? – Peter Mortensen Oct 20 '15 at 23:01
  • 6
    @PeterMortensen: In DOS and Windows console, reserved device names such as `NUL` can be followed by an optional colon. As far as I know, it performs no actual function, but serves as a visual reminder that it's a device and parallels the use of a colon after a drive letter. – Dennis Williamson Oct 20 '15 at 23:14
  • 1
    To disable the standard output while letting it still display errors, add `1>NUL` instead. (or maybe `1>NUL:` if you like the trailing colon) – demoncodemonkey Feb 16 '23 at 11:40
  • Related annoying tidbit: apart from `NUL`, the ancient DOS-originated names `CON`, `LPT`, `PRN` and a whole bunch of others (mostly variants, including all of them _with any extensions_ appended! :-o ) are still reserved in Windows (11, too), so to this day you can't just call e.g. a project subdir of external resources `aux`... – Sz. Feb 20 '23 at 14:22
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"
Bruno Dermario
  • 121
  • 1
  • 2
  • Thanks for the `findstr` solution! It looks like you can somewhat shorten that call by either omitting `/b` or both the `\r` and the `\<` inside the search strings. I'd go with `findstr /b /c:"Everything is Ok" /c:"Scanning" /c:"Creating archive"` since you don't need regular expressions (the `/r` option) here - `/b` already searches only at the beginning of strings. – Oliver Feb 23 '13 at 09:43
  • if you want to use status messages to check whether the command succeeded it is better to use return codes (`0` for success and others which detail what failed). It is easier in a script to make decisions based on these values than on messages. – WoJ Mar 26 '15 at 08:29
  • 1
    Great answer. I went with `... | findstr /v /b /c:"Compressing "` to get rid of the file listing but keep the other status messages. – Duncan Smart May 11 '16 at 19:34
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.
Ory Zaidenvorm
  • 251
  • 4
  • 6
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,178
  • 7
  • 27
  • 39
  • If you're using Powershell you could also just pipe the output to null by appending `| Out-Null` to your statement. – Dan Atkinson Mar 29 '22 at 12:36
1

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
  • 151
  • 4