how to create an interactive batch program
TRANSCRIPT
8/3/2019 How to Create an Interactive Batch Program
http://slidepdf.com/reader/full/how-to-create-an-interactive-batch-program 1/3
How to Create an InteractiveBatch ProgramBy Josh R Bellendir - Writer, Consultant, & Businessman
www.jbellendir.com
Files on windows with the file extension ".bat" are called batch files. These
are usually small files with some lines of Windows batch code that can help
automate things for users. They are commonly used to do the following:
Add a share drive
Add a share printer
Replace shortcut files on your desktop
They can be used to do most of the common tasks we do on a windows
machine. They are usually added to a users logon profile so that they are run
every time that user logs into Windows. They can also be used as interactive
programs to do the above mentioned items. Let's say you wanted to give a
user the option of WHICH share drive he/she wanted to map. A normal batch
script will just map a predefined share drive. Well, I'm going to show you how
to write a batch script to ask the user which drive to map and then map it for
them.
Let's go over a few things so we know what each command does.
@echo - this outputs text to the users screen
So '@echo Print this' would result in the user screen displaying 'Print this'
'@echo.' with a period after it will print out a blank line after your text. This is
good to make the output easier to read.
@set is used to set a variable. A variable that we can set and assign based
on user input.
'@set VARIABLEA=' will set a variable named VARIABLEA and will set it to
null (an empty value)
Now here is how we set a value to that variable based on what the user
types in.
'@set /P VARIABLEA=TYPE IN A NUMBER. I.E. 1:%=%'
8/3/2019 How to Create an Interactive Batch Program
http://slidepdf.com/reader/full/how-to-create-an-interactive-batch-program 2/3
The above line will ask a user to TYPE IN A NUMBER. The user will enter in a
number and hit the enter key. Then VARIABLEA will have the value the user
entered.
To output the value in that variable to the screen we will do this:
'@echo %VARIABLEA%'
Another two helpful commands to learn are pause and echo off/on.
'@pause' will pause the screen and ask the user to hit a key to continue
'@echo off' will prevent the system from outputting system messages
'@echo on' will turn back on system messages
So let's now put all this into practice. Let's create a simple interactive batchscript that asks a user which share drive they would like to map and then
map it for them. In order to do this, we will need to know the windows
command to map a network drive. As long as you can execute these
commands in dos then we are good to use them. Here is the syntax to map a
network drive:
net use {drive letter}: \\{path to drive} /PERSISTENT:YES
The persistent option will tell it to map the drive with the option to
automatically map it each time the user logs on. Typically we will want to
use this option. So let's use the above code to make our script. Once you are
done, it will look something like below. Be sure to copy and paste this inside
a text file, save as .bat and then you will be able to double click it to run.
Enjoy!
DRIVE MAPPER v1.0
@echo MAP YOUR DRIVE
@echo.
@echo --- SELECT A SHARE DRIVE ---
@echo 1.) MOVIES FOLDER
@echo 2.) PHOTOS FOLDER
@echo 3.) MUSIC FOLDER
8/3/2019 How to Create an Interactive Batch Program
http://slidepdf.com/reader/full/how-to-create-an-interactive-batch-program 3/3
@echo.
@set DRIVE=
@set /P DRIVE=ENTER THE NUMBER THAT YOU WANT TO MAP. I.E. 1:%=%
@ECHO.
@set DRIVE=
@set /P DRIVELETTER=ENTER THE DRIVE LETTER YOU WANT TO USE. I.E....F:
%=%
@echo.
@echo.
@echo YOUR SHARE DRIVE WILL BE MAPPED TO LETTER: %DRIVELETTER%
@pause
@echo off
IF (%DRIVE%)==(1) net use %DRIVELETTER%: \\192.168.1.4\movies/PERSISTENT:YES
IF (%DRIVE%)==(1) net use %DRIVELETTER%: \\192.168.1.4\photos
/PERSISTENT:YES
IF (%DRIVE%)==(1) net use %DRIVELETTER%: \\192.168.1.4\music
/PERSISTENT:YES
@echo on
@echo.
@echo IF YOU DON'T SEE ANY ERRORS ABOVE, YOUR DRIVES HAVE NOW
BEEN MAPPED. IF YOU SEE ERRORS ABOVE, PLEASE RERUN THIS UTILITY AND
MAKE CHANGES. MOST ISSUES ARE BECAUSE YOU SELECTED A DRIVER
LETTER IN USE OR YOU DIDN'T SELECT A CORRECT NUMBER FROM THE LIST
ABOVE.
@pause
Written by Josh R Bellendir, 2/27/2011Check out www.jbellendir.com for more articles, stories,tutorials, and reviews!