Batch Script - Devices

Hello, future programmers! Today, we're going to dive into the fascinating world of Batch scripting and explore how we can interact with various devices using these powerful little scripts. As your friendly neighborhood computer teacher, I'm excited to guide you through this journey, even if you've never written a line of code before. So, grab your virtual notepads, and let's get started!

Batch Script - Devices

What are Devices in Batch Scripting?

Before we jump into the nitty-gritty, let's understand what we mean by "devices" in the context of Batch scripting. In simple terms, devices are the various hardware components or peripherals connected to your computer. These can include your screen, keyboard, printer, and even some special files that Windows uses to represent certain functions.

Common Devices in Batch Scripting

Here's a table of some common devices you'll encounter in Batch scripting:

Device Name Description
CON Console (keyboard and screen)
PRN Default printer
NUL Null device (discards any input)
COM1-COM9 Serial ports
LPT1-LPT9 Parallel ports

Now that we have an overview, let's explore how we can use these devices in our Batch scripts!

Working with the Console (CON)

The console, represented by CON, is probably the device you'll interact with the most. It's essentially your keyboard for input and your screen for output.

Reading from the Console

Let's start with a simple example:

@echo off
set /p name=What's your name? 
echo Hello, %name%!
pause

In this script:

  1. We turn off command echoing with @echo off.
  2. We use set /p to prompt the user for input and store it in the name variable.
  3. We then use echo to display a greeting with the user's name.
  4. Finally, pause keeps the console window open so we can see the result.

When you run this script, it will wait for you to type your name and press Enter. It's like having a conversation with your computer!

Writing to the Console

Now, let's try something a bit more fun:

@echo off
echo Let's count to 5!
for /l %%i in (1,1,5) do (
    echo %%i
    ping -n 2 localhost >nul
)
echo Blast off!
pause

This script:

  1. Announces it's going to count to 5.
  2. Uses a for loop to count from 1 to 5.
  3. Displays each number with a short pause (using ping as a delay).
  4. Finishes with a "Blast off!" message.

Run this, and you'll see the numbers appear one by one, just like a rocket countdown!

The Null Device (NUL)

The NUL device is like a black hole for data. Anything sent to it simply disappears. This might sound useless, but it's actually quite handy for suppressing unwanted output.

Here's an example:

@echo off
echo This will be displayed.
echo This won't be displayed. > NUL
dir > NUL
echo Did you see the directory listing? Nope!
pause

In this script:

  1. The first echo displays normally.
  2. The second echo is redirected to NUL, so it's not displayed.
  3. The dir command's output is also sent to NUL.
  4. The final echo confirms that we didn't see the directory listing.

This is particularly useful when you want to run commands without cluttering your console with their output.

Working with Printers (PRN)

While we don't print as much these days, Batch scripts can still interact with printers. The PRN device represents the default printer.

Here's a simple example:

@echo off
echo This is a test print job. > PRN
echo Check your printer!
pause

This script sends a line of text directly to your default printer. Be careful with this one – we don't want to waste paper!

Serial and Parallel Ports (COM and LPT)

For those of you working with older hardware or specialized equipment, Batch can also interact with serial (COM) and parallel (LPT) ports.

Here's an example that sends data to a serial port:

@echo off
echo Hello, device! > COM1
echo Data sent to COM1
pause

This script sends "Hello, device!" to the first serial port. Of course, you'll need a device connected to that port to see any effect.

Conclusion

And there you have it, folks! We've taken a whirlwind tour of devices in Batch scripting. From chatting with the console to sending secret messages to the null device, and even saying hello to printers and serial ports, you now have the power to interact with various parts of your computer using simple Batch commands.

Remember, the key to mastering Batch scripting (or any programming, for that matter) is practice. So, don't be afraid to experiment with these examples, modify them, and see what happens. Who knows? You might just create the next great Batch script masterpiece!

Until next time, happy scripting!

Credits: Image by storyset