Search code examples
batch-filecommand-lineerror-suppression

How to ignore a specific command-line error output from batch script?


I have made a batch script that, among other things, merges our DEV branch to our TEST branch using the following command:

tf merge $/Proj/Dev $/Proj/Test /recursive >nul

This command always triggers the following output:

TF401190: The local workspace [workspace];[name] has 110500 items in it, which exceeds the recommended limit of 100000 items. 
To improve performance, either reduce the number of items in the workspace, or convert the workspace to a server workspace.

I know I can avoid all errors/ouput by adding "2>&1" to the end of the command like so:

tf merge $/Proj/Dev $/Proj/Test /recursive >nul 2>&1

Ideally I would just like to ignore/suppress specifically the TF401190 error. I feel like there has to be a way to do this, even if it means checking the output for a specific token/string before allowing it to print. I'm still very new to the command-line and batch scripts. Any help would be greatly appreciated! Thanks.

NOTE: I'm not interested in addressing a solution for the error itself. This question is only concerned with how to suppress any specific error.


Solution

  • In the bash shell, you can filter out specific errors like this:

    ls /nothere
    
    ls: cannot access /nothere: No such file or directory
    

    To suppress that specific error message:

    ls /nothere 2>&1 | grep -v 'No such file'
    

    (error message is suppressed)

    Checking if other error messages get through:

    ls /root 2>&1 | grep -v 'No such file'
    ls: cannot open directory /root: Permission denied
    

    (other error messages get through fine)