Solved

Creating a batch file with user input

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

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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