15 Useful Batch files programming recipes

In the Batchography book, I cover basic to advanced Batch files programming topics. Since the book was published in 2016, I kept blogging about Batch programming language.

Here’s a collection of some useful recipes:

  1. Check if the script is running as an Administrator
  2. String substitution
  3. Number counting
  4. Batch files and Unicode
  5. Read from a text file, one line at a time
  6. Switch/case in Batch files
  7. Auto reinterpret/compile changed files
  8. Reading from a file
  9. Tokenizing command output
  10. Polyglot: Python and Batch files
  11. Polyglot: Batch file + self compiling C++
  12. Embedding binaries inside Batch files
  13. Interactive Batch files
  14. Writing a game – The Hangman
  15. Batchography: Parsing INI files from a Batch file

 

flower separator
batchography-good-resDo you want to master Batch Files programming? Look no further, the Batchography is the right book for you.

Available in print or e-book editions from Amazon.

flower separator


flower separator

You might also like:

Can you solve this puzzle?

I got this silly puzzle via chain mail:

Can you solve it?

Maybe you will be able to, but don’t beat yourself up if you don’t get it right.

You might also like:

Infectious laughter recordings

Are you feeling down? Did you know that laughter is infectious?
I suggest you bookmark this page and return to it when you are feeling depressed. Listen to the laughter below and brighten up.

Enjoy!

Male laughing:

Male laughing hysterically:

Male giggling:

Female laughing:

Female laughing hysterically:

Female giggling:

All of them together:

You might also like:

Batchography: what happens when you redirect ‘cls’ to a file?

Let’s assume you have a Batch file (test.bat) with the following contents:

@echo off
echo 1
cls
echo 2

And then you run this Batch file and redirect its output to a text file called “out.txt”:

C:>test.bat >out.txt

What do you think the output would be?

At first, I thought it would be:

1
2

But little did I know that when ‘cls’ is invoked in a context where stdout is redirect to a file, then a form feed character (0xC) is emitted instead:

I was curious, so I disassembled ‘cmd.exe’ to verify my findings. Lo and behold, indeed, ‘cmd.exe’ does that:

int __stdcall eCls(struct cmdnode *a1)
{
  HANDLE hStdOut;
  HANDLE v2;
  SMALL_RECT ScrollRectangle; 
  COORD dwDestinationOrigin;
  CHAR_INFO Fill;
  struct _CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;

  if ( FileIsDevice((char *)1) )
  {
    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if ( GetConsoleScreenBufferInfo(hStdOut, &ConsoleScreenBufferInfo) )
    {
      dwDestinationOrigin.Y = -ConsoleScreenBufferInfo.dwSize.Y;
      dwDestinationOrigin.X = 0;
      *(_DWORD *)&ScrollRectangle.Left = 0;
      ScrollRectangle.Bottom = ConsoleScreenBufferInfo.dwSize.Y;
      ScrollRectangle.Right = ConsoleScreenBufferInfo.dwSize.X;
      Fill.Char.UnicodeChar = 32;
      Fill.Attributes = ConsoleScreenBufferInfo.wAttributes;
      ScrollConsoleScreenBufferW(hStdOut, &ScrollRectangle, 0, dwDestinationOrigin, &Fill);
      ConsoleScreenBufferInfo.dwCursorPosition = 0;
      v2 = GetStdHandle(0xFFFFFFF5);
      SetConsoleCursorPosition(v2, 0);
    }
    else
    {
      cmd_printf(page_feed);
    }
  }
  else
  {
    cmd_printf(page_feed);
  }
  return 0;
}

(Lines 29 and 34 are of interest)

In conclusion, be aware if you redirect a Batch file to another file and compare the result. If the Batch file uses CLS, you have to account for the form feed character showing up!
flower separator
batchography-good-resDo you want to master Batch Files programming? Look no further, the Batchography is the best book on the topic and the most up to date!

Available in print or e-book editions from Amazon.

 


You might also like:

 

Statistics about Lebanon

I found this information on the internet, if you find inaccuracies, let me know please.

Statistical facts

  1. Lebanon has 18 religious communities
  2. It has 40 daily newspapers
  3. It has 42 universities
  4. It has over 100 banks (that is banks and not branches of a bank)
  5. 70% of the students are in private schools
  6. Many of the Lebanese people are Christians (The highest percent in all the Arab countries)
  7. There’s 1 doctor per 10 people in Lebanon (In Europe & America , there’s 1 doctor per 100 people)
  8. The name LEBANON appears 75 times in the Old Testament
  9. The name CEDAR ( Lebanon ‘s tree) appears 75 times too in the Old Testament!!
  10. Beirut was destroyed and rebuilt 9 times (this is why it’s compared to The Phoenix).
  11. There’s 3.5 Million Lebanese in Lebanon
  12. There’s around 15 Million Lebanese outside Lebanon !!!
Other interesting facts:
  1. Lebanon , the country, was occupied by over 16 countries:
    (Egyptians-Hittites-Assyrians- Babylonians- Persians- Alexander the greats Army- the Roman Empire Byzantine- the Arabian Peninsula-The Crusaders- the Ottoman Empire- Britain-France- Israel- Syria)
  2. Byblos (city in Lebanon ) is the oldest, continuously living city in the world.
  3. Lebanon ‘s name has been around for 4,000 yrs non- stop (it’s the oldest country/ nation’s name in the world!)
  4. Lebanon is the only Asian/African country that doesn’t have a desert.
  5. There are 15 rivers in Lebanon (all of them coming from its own mountains)
  6. Lebanon is one of the most populated countries in its archeological sites, in the world!!!
  7. The first alphabet was created in Byblos (city in Lebanon )
  8. The only remaining temple of Jupiter (the main Roman god) is in Baalbeck ,Lebanon (The City of the Sun)
  9. The name of BYBLOS comes from the BIBLE!!!
  10. Lebanon is the country that has the most books written about it.
  11. Lebanon is the only non-dictatorial country in the Arab world (Yes, we do have a President!)
  12. Jesus Christ made his 1st miracle in Lebanon , in Qana (The miracle of Turning water into wine).
  13. The Phoenicians (Original People of Lebanon) built the 1st boat, and they were the first to sail ever!
  14. Phoenicians also reached America long before Christopher Columbus did.
  15. The 1st law school in the world was built in Lebanon , in Downtown Beirut.
  16. People say that the cedars were planted by God’s own hands (This is why they’re called ‘The Cedars of God’, and this is why Lebanon is called ‘God’s Country on Earth.’

I love Lebanon ….. may God bless my homeland forever….

First trip to Lebanon
First time To Lebanon? The Untold tips and tricks

You might also like:

Apple’s Measure App

If you have iOS 12+ then you should try Apple’s Measure app. It is really handy.

You can measure:

  • Bookshelves
  • Picture frames
  • Window sizes
  • Door sizes
  • etc…


You might also like:

If your plan is for 100 years, educate children

If your plan is for 1 year, plant rice.
If your plan is for 10 years, plant trees.
If your plan is for 100 years, educate children.

You might also like:

Excerpts from the “Blizzard Cosplay: Tips, Tricks and Hints” book

A hardcover copy of the “Blizzard Cosplay: Tips, Tricks and Hints” book was released on October 15, 2018. It has stunning CosPlay images.

Here’s a small taste of it:

You might also like: