Tuesday, September 17, 2013

4. Batch Files - All about echo, How to echo without newline



ECHO
One of the most frequently used command is echo
Echo can be used in 3 ways
Echo  :: will tell you echo is on or off 
echo Hello, world!  :: prints message on screen
@echo :: @ makes sure this line does not get echoed
 ::most common use of this is to turn echo off, otherwise all the commands would get printed on the screen, with echo off, only the stuff you choose to echo will get displayed
@echo off           

Echo without newline character
You will notice that whenever you use echo command, it append a new line character in the end.
Believe me, pretty soon after you start to write batch files, you would want to do echo without new line. Here is the solution for you.

Suppose you want to echo "Hello, World" without new line

echo | set /p dummyVar="Hello, World"
::Similarly if you have variable you, can echo that as well
set var1="Hello, World 2!"
echo | set /p dummyVar=%var1% 


There is a exe called "clip" which can be used to copy stuff to clipboard. you can use. This would copy "Hello, World" to clipboard without any newline character

echo | set /p dummyVar="Hello, World" | clip 



Circular Buffer - Find distance between head and Tail


Problem is to find distance between head and tail in a circular buffer, where head is always ahead of tail. But at any point, head or tail may roll over to 0th location.
Assume that we are rolling over after uint8 i.e 255 entries.
Simple case is like this. Head = 10 Tail =4.
Distance = 10- 4 =6
But say Head is at 4 and tail is at 253.
Distance = 4 – 254 = wont work out (head rolled back to zero after 255)

There is a very simple solution to this problem. .
Formula would be:
 
Distance = Head + 2’s complement of tail

e.g.
0000 1010 (10)
+
­1111 1100 (2’s complement of 4)
========================
0000 0110 (6)

Lets take other example
0000 0100 (4)
+
0000 0010 (2st complement of 254)
===========================
0000 0110 (6)