Search
Desktop

Batch File Basics (Part 2)

Published 4 min readBy Programming4us Editorial Team

Save the Batch File

Saving the batch file is important, as we need to make the sure the encoding is correct, and that Notepad won't save it as a text file instead. With your batch file already typed in, go to File > Save As, and choose a location for the batch file, then type in a name for the batch file (in our case we imaginatively named it 'filel') and add '.bat' after its name. Now make sure that the 'Save as type:' pull down menu is displaying All Files, and not Text Documents, and check that the 'Encoding' pull down menu is ANSI, then click Save.


Make sure you save as a .bat file, not a text file

When you now look at the saved batch file in Windows Explorer, you should see it has a slightly different icon than the usual (a couple of cogs) and the Type will be identified as a Windows Batch File. To run it, if you are using Windows Vista, or Windows 7, with the UAC still active, you will have to right click, and select Run as Administrator from the menu. Why? Well because we are calling the command to view and dump the directory listing of the Program Files folder, which as you know is a folder that's protected under the UAC settings of Vista and 7. Of course, if you're running this under Windows XP, or if you have turned off the UAC then you can simply double click.

The result should be the listing of the contents of the Program Files directory, as shown in Notepad. You'll also have a blank CMD window pop up as well, but don't worry about that - it's only because you're calling from the command prompt.


You'll end up with a blank command screen, and the results in Notepad

Playing With Variables

Although that last batch file was quite rudimentary, it showed us that we can call up both DOS and Windows based commands from with a single script (i.e. the dir. command and the Windows Notepad application). Generally we can call up any Windows based application via the Command prompt; however some will require an extra syntax that is usually present when running from an icon.

For this next batch file, we'll have a play with variables, and we'll see if we can import them to improve the first batch we made. To start, open up a new Notepad instance, and type in the following:

@echo off

set /p name= What is your name? echo Hello, %name%

As we already know, the @echo off command turns off the displaying of the rest of the script's commands; the set /p name creates a variable called name, with the /p part indicating that an '^prompt string' will follow. What is your name? (note: there's a space after the question mark, it gives you an extra space and looks better when run) part will be displayed and will wait for the user to input their name, which will be stored under the variable %name%. And finally, the echo command will display the text Hello, followed by calling the variable %name%.

Save this batch file as file2.bat, for example, and make sure the encoding and all files is correct as before. To run it, drop into a Command prompt, by pressing the Windows key + R and typing in: cmd, then change directory to the saved batch file, and type in: file2 to execute.


Wonderful variables make a computer a more personal thing

Spice It Up

If we wanted to spice up this batch file a little, we could again use Notepad to display the end output, the Hello so and so part. To do this we could alter it as such:

@echo off

set /p name= What is your name? echo Hello, %name% > name.txt notepad.exe name.txt

This alteration will ask for your name, store it in the variable called %name%, output and pipe Hello, %name% to a text file called name.txt, then open up Notepad, with the output.


Piping an output into a text file is handy for calling it up in Windows applications...

As you'll notice, you're left with a file called name.txt in the directory that the batch file was run in, and the command prompt looks a little messy, so we could further spice things up by adding a few extra lines:

@echo off

cls

set /p name= What is your name? echo Hello, %name% > name.txt notepad.exe name.txt

cls

del /Q name, txt

This will clear the screen before the 'What is your name' question is asked, then display the name.txt in Notepad, then when you close down Notepad, the screen will be cleared once more and the name.txt will be deleted. Plus, you can also run this from within Windows Explorer, as everything is now neatened up a bit.