Solved

Creating a batch file with user input

Posted on 2010-09-15
5
437 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
  • 3
5 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry Bill, typing same time there I guess.

Steve
0
 

Author Closing Comment

by:DonKwizote
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now