DESKTOP

Batch File Basics (Part 2)

10/9/2012 3:35:09 AM

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.

Description: Make sure you save as a .bat file, not a text file
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.

Description: You'll end up with a blank command screen, and the results in Notepad
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.

Description: Wonderful variables make a computer a more personal thing
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.

Description: Piping an output into a text file is handy for calling it up in Windows applications...
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.

Other  
 
Most View
Lenovo ThinkPad Twist - The Old Form Factor Starting A New Life With Windows 8 (Part 1)
Windows 8 : Managing User Access and Security - Managing Remote Access to Workstations (part 2)
ASRock Z77 Extreme11 Mainboard - An LGA 1155 Mainboard For Users That Use Multiple Drives (Part 5)
The Review Of Three Seasonic Power Supply Suits (Part 4)
Windows Phone 7 : Drawing with Vertices and Matrices - Tinting Objects
Asus PadFone 2 - The Attraction Of The Phone-In-Tablet Combination (Part 6)
Two Is Better Than One - WD My Cloud Mirror
Disgo 9104 - An Android Tablet That Runs On Ice Cream Sandwich
Sharepoint 2010 : Putting Your Site on the Web - Web Content Management (part 1)
MSI GT70 Dragon Edition Gaming Laptop Review (Part 2)
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)