Solved

Creating a batch file with user input

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delete a folder ever 5 minutes 6 60
Internet Connection Util 8 82
Need some help in Batch file assigning variable value ? 4 66
regex to extract ip:john 17 69
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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

20 Experts available now in Live!

Get 1:1 Help Now