Solved

Creating a batch file with user input

Posted on 2010-09-15
5
440 Views
Last Modified: 2012-05-10
Hello Everyone,
I am fairly new to batch scripting and I am learning it bit by bit.
I would like to create a batch file that maps drives for users when they are in the office.
The batch file will ask the user "Are you in the office?" and if they answer Yes, the script will map the drives and if they answer No, the script will exit.

This is my first try and I know it's wrong because it doesn't execute.
Please correct my mistakes.....

echo Are you working in the Head office?

If Y goto :Y
else
exit

:Y
net use j: \\joburg\admin
net use k: \\kathmandu\data
net use r: \\rome\project
net use v: \\valencia\group

exit
0
Comment
Question by:DonKwizote
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33681503
set /P Reply="Are you working in the Head office? (Y/N) "
If /I "%Reply%" EQU "Y" (
  goto :Y
) else (
  exit
)

:Y
net use j: \\joburg\admin
net use k: \\kathmandu\data
net use r: \\rome\project
net use v: \\valencia\group
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33681546
The only native input in a batch file is using the set /p command, which is not ideal.  There are other ways with the choice command which is on most OS's - I think it is in Win 7, Vista, XP certainly but think it was missing in 2000?

@Echo off
choice /T 10 /D Y /M "Are you in the office [Y/N]"
if errorlevel 255 echo ERROR & goto office
if errorlevel 2 goto not
if errorlevel 1 goto office
echo Escaped from the question, asssume not
goto not
:office
  echo In the office
exit /b
:not
  echo Not in the office
exit /b

or without using choice

@Echo off
set /P office=Are you in the office [Y/N]?

if /I "%office:~0,1%"=="Y" goto office
if /I "%office:~0,1%"=="N" goto not
echo Not sure where they are, maybe assume not
goto not
:office
  echo In the office
exit /b
:not
  echo Not in the office
exit /b

That checks case insensitively (/I) the first character of the response made against Y or N.  You have to press enter with Set /p

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33681553
Sorry Bill, typing same time there I guess.

Steve
0
 

Author Closing Comment

by:DonKwizote
ID: 33681676
Thanks Bill for the rapid response and the solution. Thanks also Steve for helping me understand more about batch files. :-)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33681724
You can always split up points you know.... or sometimes we feel like we waste our time.  BTW if the user types Yes or yes into mine it will do the same as Y or y and please look if your machines have choice on as users will not necessarily be expecting to hit Y then return etc.

You can also do some other simplish checks such as checking the network range of the network you are on or similar from DHCP if you have hopefully something other than a 192.168.0.x range that someone is probably also using at home:

http://scripts.dragon-it.co.uk/links/batch-get-tcpip-subnet?OpenDocument

Steve
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question