Windows Batch Scripting - Displaying lines

command_prompt.pngProviding feedback for the user about the status of our scripts is really important. We would like to know if everything is fine - or if not, why? Let me show you some tricks which can take the well-known ECHO command a little bit further!
Redirect / hide normal and/or error output
Kind of a must for understandable results. Redirecting can be done with the > character, which can be placed after ECHO or any other external command. When using NUL as a "file name" the text redirected will be discarded completely. What makes this more funny, Windows uses two kind of outputs for displaying text, STDOUT (normal) and STDERR (error). Normally both appears in the console, unless... :) Give it a try. NET USE > nul will show no output at all, but NET InvalidCommand will show you the usage. That's because NET command is using the error output in case of... errors :)
ECHO This will be written to a file > C:\MyFile.txt
ECHO This will be discarded completely > nul
ECHO Both error and normal outputs are ignored > nul 2>&1
ECHO Error output ignored, normal output redirected to file > C:\MyFile.txt 2>nul

Escaping
Batch reserves specific characters for specific actions. Like > is used for redirection. What happens if you want to print that character out? Escape it.
ECHO 100 ^> 99

Displaying a line without line break
I have to admit, this is one of my favorite. With a bit of trick, you can make ECHO not to jump to the next line:
@ECHO OFF
ECHO/|SET /P=Doing some stuff...
REM Let's do something, which will succeed!
NET USE > nul
IF ERRORLEVEL 1 (ECHO  Failed!) ELSE (ECHO  OK!)
ECHO/|SET /P=Doing some more stuff...
REM Now let's fail...
NET InvalidCommand > nul 2>&1
IF ERRORLEVEL 1 (ECHO  Failed!) ELSE (ECHO  OK!)

Alignment
For our scripts to be more festive, we can even align it to a specific column. This is achieved by string manipulation, which I might explain in a later post.
@ECHO OFF
FOR /F %%a IN ('DIR /B /A:D') DO CALL :DISPLAY "%%a"
GOTO :END
:DISPLAY
SET MYVAR=                    %~1
SET MYVAR=%MYVAR:~-20%
ECHO %MYVAR% - Check.
GOTO :eof
:END

Comments

1. On 2017.01.05, 17:21 by خرید اپل آیدی

This article is truly a pleasant one it assists new internet visitors, who are wishing for blogging.

2. On 2017.03.31, 16:24 by memek

If some one desires to be updated with newest technologies
afterward he must be visit this site and be
up to date everyday.

3. On 2017.03.31, 21:28 by mamhoon

Hello there I am so happy I found your blog page, I really found you by error, while I was searching on Bing for something else,
Anyhow I am here now and would just like to say cheers for a tremendous post
and a all round interesting blog (I also love the theme/design),
I don’t have time to read it all at the moment but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read a
great deal more, Please do keep up the awesome jo.

4. On 2017.04.20, 11:34 by builders Warrnambool

I love you post.. I will share on my facebook

5. On 2017.05.07, 17:35 by top Business video company

excellent points altogether, you simply received a emblem new
reader. What could you suggest about your put
up that you simply made a few days in the past? Any sure?

6. On 2017.09.17, 17:44 by paintball

Nice blog here! Also your website loads up fast! What web host are you using?
Can I get your affiliate link to your host? I wish my web site loaded up
as quickly as yours lol

Add a comment

HTML code is displayed as text and web addresses are automatically converted.