Search code examples
windowsbashbatch-fileechobatch-processing

How to echo. only if previous line is not empty?


How in my batch I can echo. ONLY if the previous line outputted is non-empty ? As you understood I want to avoid to have 2 /n/n


Solution

  • Write a function for echo, that remember if the last line was empty.

    @echo off
    call :func1
    call :func2
    
    :func1
    call :echo "Func1"
    call :echo ""
    call :echo ""
    exit /b
    
    :func2
    call :echo ""
    call :echo "Func2"
    exit /b
    
    :echo
    set "text=%~1"
    setlocal EnableDelayedExpansion
    if "!text!!last_line!" NEQ ""  (
      echo(!text!
    )
    endlocal
    set "last_line=%~1"
    exit /b
    

    Output:

    Func1

    Func2