IT tutorials
 
Windows
 

Using Windows Home Server’s Command-Line Tools : Understanding Batch File Basics (part 1)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
5/5/2013 9:14:47 PM

The command line is still an often-useful and occasionally indispensable part of computing life, and most power users will find themselves doing at least a little work in the Command Prompt window. Part of that work might involve writing short batch file programs to automate routine chores, such as performing simple file backups and deleting unneeded files. And if you throw in any of the commands that enhance batch files, you can do many other interesting and useful things.

When you run a command in a command-line session, the command prompt executes the command or program and returns to the prompt to await further orders. If you tell the command prompt to execute a batch file, however, things are a little different. The command prompt goes into Batch mode, where it takes all its input from the individual lines of a batch file. These lines are just commands that (in most cases) you otherwise have to type in yourself. The command prompt repeats the following four-step procedure until it has processed each line in the batch file:

1.
It reads a line from the batch file.

2.
It closes the batch file.

3.
It executes the command.

4.
It reopens the batch file and reads the next line.

The main advantage of Batch mode is that you can lump several commands together in a single batch file and tell the command prompt to execute them all simply by typing the name of the batch file. This is great for automating routine tasks such as backing up the Registry files or deleting leftover .tmp files at startup.

Creating Batch Files

Before getting started with some concrete batch file examples, you need to know how to create them. Here are a few things to bear in mind:

  • Batch files are simple text files, so using Notepad (or some other text editor) is probably your best choice.

  • If you decide to use WordPad or another word processor, make sure that the file you create is a text-only file.

  • Save your batch files using the .bat extension.

  • When naming your batch files, don’t use the same name as a command prompt command. For example, if you create a batch file that deletes some files, don’t name it Del.bat. If you do, the batch file will never run! Here’s why: When you enter something at the prompt, CMD first checks to see whether the command is an internal command. If it’s not, CMD then checks for (in order) a .com, .exe, .bat, or .cmd file with a matching name. Because all external commands use a .com or .exe extension, CMD never bothers to check whether your batch file even exists!

After you’ve created the batch file, the rest is easy. Just enter any commands exactly as you would at the command line, and include whatever batch instructions you need.

Tip

If you find yourself creating and using a number of batch files, things can get confusing if you have the files scattered all over your hard disk. To remedy this, it makes sense to create a new folder to hold all your batch files. To make this strategy effective, however, you have to tell the command prompt to look in the batch file folder to find these files. To do that, you need to add the batch file folder to the PATH variable.


REM: Adding Comments to a Batch File

The first of the batch file–specific commands is REM (which stands for remark). This simple command tells the command prompt to ignore everything else on the current line. Batch file mavens use it almost exclusively to add short comments to their files:

REM This batch file changes to drive C
REM folder and starts CHKDSK in automatic mode.
C:
CHKDSK /F

Caution

It’s best not to go overboard with REM statements. Having too many slows a batch file to a crawl. You really need only a few REM statements at the beginning to outline the purpose of the file and one or two to explain each of your more cryptic commands.


ECHO: Displaying Messages from a Batch File

When it’s processing a batch file, Windows Home Server normally lets you know what’s going on by displaying each command before executing it. That’s fine, but it’s often better to include more expansive descriptions, especially if other people will be using your batch files. The ECHO batch file command makes it possible for you to do just that.

For example, here’s a simple batch file that deletes all the text files in the current user’s Cookies and Recent folders and courteously tells the user what’s about to happen:

ECHO This batch file will now delete all your cookie text files
DEL "%LocalAppData%\Microsoft\Windows\Temporary Internet Files\cookie*"
ECHO This batch file will now delete your Recent Items list
DEL "%AppData%\Microsoft\Windows\Recent Items\*.lnk"

The idea here is that when Windows Home Server stumbles on the ECHO command, it simply displays the rest of the line onscreen. Sounds pretty simple, right? Well, here’s what the output looks like when you run the batch file:

C:\>ECHO This batch file will now delete all your cookie text files
This batch file will now delete all your cookie text files
C:\>DEL "%LocalAppData%\Microsoft\Windows\Temporary Internet Files\cookie*""
C:\>ECHO This batch file will now delete your Recent Items list
This batch file will now delete your Recent Items list
C:\>DEL "%AppData%\Microsoft\Windows\Recent Items\*.lnk"

					  

What a mess! The problem is that Windows Home Server is displaying the command and ECHOing the line. Fortunately, Windows Home Server provides two solutions:

  • To prevent Windows Home Server from displaying a command as it executes, precede the command with the @ symbol:

    @ECHO This batch file will now delete all your cookie text files
  • To prevent Windows Home Server from displaying commands, place the following at the beginning of the batch file:

    @ECHO OFF

Here’s what the output looks like with the commands hidden:

This batch file will now delete all your cookie text files
This batch file will now delete your Recent Items list

Tip

You might think that you can display a blank line simply by using ECHO by itself. That would be nice, but it doesn’t work. (Windows Home Server just tells you the current state of ECHO: on or off.) Instead, use ECHO. (ECHO followed by a period).


PAUSE: Temporarily Halting Batch File Execution

Sometimes you want to see something that a batch file displays (such as a folder listing produced by the DIR command) before continuing. Or, you might want to alert users that something important is about to happen so that they can consider the possible ramifications (and bail out if they get cold feet). In both cases, you can use the PAUSE command to halt the execution of a batch file temporarily. When Windows Home Server comes across PAUSE in a batch file, it displays the following:

Press any key to continue . . .

To continue processing the rest of the batch file, press any key. If you don’t want to continue, you can cancel processing by pressing Ctrl+C or Ctrl+Break. Windows Home Server then asks you to confirm:

Terminate batch job (Y/N)?

Either press Y to return to the prompt or N to continue the batch file.

 
Others
 
- Windows 7 : Email and Newsgroups with Windows Live Mail - Finding and Reading Newsgroups
- Windows 7 : Email and Newsgroups with Windows Live Mail - Setting Up a Newsgroup Account in Windows Live Mail
- Windows 7 : Email and Newsgroups with Windows Live Mail - Using the Windows Live Mail Contacts, Newsgroups and the Internet
- Windows Server 2008 : Basic Rules When Using the Command Prompt - Understanding Variables, Understanding Switches
- Windows Server 2008 : Basic Rules When Using the Command Prompt - Using Uppercase or Lowercase, Using Quotes
- Windows 8 : Settings and Customization - Policies
- Windows 8 : Settings and Customization - The Registry
- Using Windows Home Server’s Command-Line Tools : Working at the Command Line
- Using Windows Home Server’s Command-Line Tools : Getting to the Command Line
- Windows Vista : Desktop Searching with the Windows Search Engine
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us