Solved

Creating a batch file with user input

Posted on 2010-09-15
5
441 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

734 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