Ӕ Central

Welcome to the hidden lair of AE Central. From cars and beer to hardcore software development, you might find interesting topics in all areas. Feel free to browse!

To menu | To search

Windows Batch Scripting - Displaying lines

, 12:50 - Permalink

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

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/|SET /P=Doing some stuff...
REM Let's do something, which will succeed!
NET USE > nul
ECHO/|SET /P=Doing some more stuff...
REM Now let's fail...
NET InvalidCommand > nul 2>&1

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.
FOR /F %%a IN ('DIR /B /A:D') DO CALL :DISPLAY "%%a"
SET MYVAR=                    %~1
ECHO %MYVAR% - Check.
GOTO :eof


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

7. On 2018.01.04, 20:26 by Maple

I blog quite often and I seriously thank you for your content.
Your article has really peaked my interest. I will bookmark
your site and keep checking for new information about once per week.

I opted in for your Feed too.

8. On 2018.04.26, 11:39 by Alexandra Morely

One of the best blog

9. On 2018.09.17, 07:21 by Colton Gens

It’s a good shame you don’t contain a give money button! I’d definitely give money for this fantastic webpage! That i suppose for the time being i’ll be satisfied bookmarking together with including RSS feed

Add a comment

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

This post's comments feed