DESKTOP

Batch File Basics (Part 1)

10/9/2012 3:34:13 AM

There was a time when batch files ruled. They were created by PC users who wanted to load up certain drivers at boot, or to quickly rename a group of files, or perhaps to create a menu for the user to choose whether they wish to run Windows 3.11, a game or just to drop into DOS with loads of conventional RAM free. Whatever the reason, and whatever the situation there were some of us who would automatically create a batch file to fulfill a need.

Description: Windows 3.11

Windows 3.11

Batch files these days are few and far between, with most Windows experiences being handled by a nice looking GUI, but there is still room for the humble batch file. And once you've started using them, either for the first time, or just to remember how they were laid out, you'll begin to wonder why you ever left them behind.

Over the next few issues we'll be looking at some batch file basics, from outputting onto the screen, to storing variables to creating menu, and maybe having a look at the odd game here and there. Who ever said DOS was boring?

What Is A Batch File?

A batch file is simply a text file that contains lines with commands that are executed in sequence, one after the other, much like the old ZX Spectrum BASIC, for example. They have special extensions, either BAT or CMD, and can be executed by simply typing in their name, as with other system commands.

Description: start programs with batch file

start programs with batch file

You don't need any fancy compilers, just Notepad will do - but not Word, WordPad or anything else like that, as they contain special formatting that the batch file won't recognize. And one final thing, you don't necessarily have to learn a programming language; there are instances where a simple knowledge of how a program will work will aid you, but generally speaking, as long as you know the syntax of a command, then you'll be fine when creating a batch file.

Getting Started

First off, open Notepad and on the first line type in:

@echo off

By default a batch file will display all the commands that it runs through, line by line, what the @echo off command does is turn that feature off for the whole script, with the '©' (at) sign to apply that command to itself.

The next part of the script can be entirely up to you, and what you want the batch file to achieve, but in this instance we'll go through some basic examples and see where we end up.

Getting an Output

To keep things simple, let's add the following command to the second line of the batch file:

dir "C:\Program Files\” > c:\progs.txt

This line will grab the directory listing of your C:\Program Files folder, and dump the contents into a text file called progs.txt, in the root of C:\. Now add the following:

notepad.exe c:\progs.txt

This last command will now launch notepad and display the contents of the progs.txt file you just created.

Set Command

While we are creating variables using the set command, we have to be careful not to create a variable that will ultimately wipe out the system specific variables. To find out what these are, drop into a command prompt and type in:

set

This will display all the system variables, along with any that you've since created. It's also a good place to check if the system already has a variable in place to handle the current user's name; which is already stored as %USERNAME%. So rather than asking for the users' name in a batch file, you could simply start with:

@echo off

Echo Hello, %USEKNAME%, what are you doing?

Other  
 
Most View
Olympus Stylus 1 - The Pied Piper (Part 1)
Kingston SSDNow V300 Solid State Hard Drive (Part 2)
Windows Vista : Managing Windows Images - Executing Common Tasks (part 2) - Incorporating scripts in a Windows PE image
Using the Android Development Environment for Real Applications (part 1)
Windows Server 2003 : Configuring Zone Properties and Transfers (part 2)
Cooler Master JAS PRO - A ‘Permanent’ Stand For Home Or Office Use
What’s New In Speakers? – April 2013 (Part 1)
How To Transfer Data From SSD To HDD
ADO.NET Programming : Microsoft SQL Server (part 2) - Using SQL Server Stored Procedures
2014 Volkswagen Scirocco R Review (Part 1)
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Top 10
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
OPEL MERIVA : Making a grand entrance
FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
BMW 650i COUPE : Sexy retooling of BMW's 6-series
BMW 120d; M135i - Finely tuned
PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
Java Tutorials : Nested For Loop (part 1)