1.

Solve : user input of data to a command?

Answer»

hello all,


'm having issues with how running the command, net use \\xxx.xxx.xxx.xxx username admin password 123 (where xxx is a ip address entered by user) via a batch file.

also sc \xxx.xxx.xxx.xxx start {service} where {service} is a service entered again by the user.

any ideas?Well, when you say you are having issues, what do you mean by that? Do you need the user to input their IP data?hey,

yeah, i'm trying to write a batch file to save me typing the following when starting a service remotley;

net use \\xxx.xxx.xxx.xxx
enter user name; end21
enter password; spotty dog

and then

sc \\xxx.xxx.xxx.xxx start awhost32

I need the ip address, service and either stop or start to be entered by the user.

this is what i have so far but it's no-where near finished.

@echo off

:loop
echo.
echo.
echo. Welcome to the Remote services Program
echo.
echo.
echo. A. Enter a IP address
echo.
echo. B. Enter a Service to be started or stopped
echo.
echo. Q. Quit
echo.
echo.
echo.



SET Choice=
SET /P Choice=Choose your option and then press Enter (A,B or Q):

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
ECHO.
IF /I '%Choice%'=='A' GOTO IP
IF /I '%Choice%'=='B' GOTO Service
IF /I '%Choice%'=='Q' GOTO End
ECHO "%Choice%" is not valid. Please try again.
ECHO.

:IP



set /p '%IP%'="Enter IP Address of remote Pc: "

net use \\'%IP%' user end21 password 82mass30

goto loop

:service


set /p Service="Enter a service to be started or stopped: "

goto loop

:end

exit

what do u think??? any suggestions???
Just a first glance...

1. what's with all those single quotes?

2. IF NOT '%Choice%'==''

Did you mean IF NOT "%Choice%"==''" ?

3. This doesn't look right

set /p '%IP%'="Enter IP Address of remote Pc: "

In short, your batch needs a thorough overhaul.





thats why i have posted it on here.

any suggestions?I'll try and post something this evening when I have more time. What you done so far makes me think you are on the right trail, but maybe I could offer some help tidying it up a bit to make it work.
i would really appreactice that, thanks.

i look forward to seeing what u can do.try this
Quote


@echo off

:loop
echo.
echo.
echo. Welcome to the Remote services Program
echo.
echo.
echo. A. Enter a IP address
echo.
echo. B. Enter a Service to be started or stopped
echo.
echo. Q. Quit
echo.
echo.
echo.


:getchoice
SET Choice=
SET /P Choice=Choose your option and then press Enter (A,B or Q):

IF NOT "%Choice%"=="" SET Choice=%Choice:~0,1%
ECHO.
IF /I "%Choice%"=="A" GOTO IP
IF /I "%Choice%"=="B" GOTO Service
IF /I "%Choice%"=="Q" GOTO End
ECHO "%Choice%" is not valid. Please try again.
goto getchoice
ECHO.

:IP

set /p IP="Enter IP Address of remote Pc: "

net use \\%IP% user end21 password 82mass30

goto loop

:service

set /p Service="Enter a service to be started or stopped: "

REM presumably some code to start or stop a service here?

goto loop

:end

exit
hi,

thanks. yeah that works, (without the user name and password bit) as that just brings up "the santax of the command is" and then DETAILS of how to use net use.

I've removed that bit, but yes that will work.

I'll tweak it a bit and get to stop or start the service later. I'll post the results here either later tonight or TOMORROW at work.

Many thanks!!!!right the finished thing.

if you know of a way to make look a bit neater, please let me know.

@echo off

:loop
echo.
echo.
echo. Welcome to the KFC Remote services Program
echo.
echo.
echo. A. Enter a IP address
echo.
echo. B. Enter a Service to be re-started
echo.
echo. Q. Quit
echo.
echo.
echo.


:getchoice
SET Choice=
SET /P Choice=Choose your option and then press Enter (A,B or Q):

IF NOT "%Choice%"=="" SET Choice=%Choice:~0,1%
ECHO.
IF /I "%Choice%"=="A" GOTO IP
IF /I "%Choice%"=="B" GOTO Service
IF /I "%Choice%"=="Q" GOTO End
ECHO "%Choice%" is not valid. Please try again.
goto getchoice
ECHO.

:IP

set /p IP="Enter IP Address of remote Pc: "
net use \\%IP% /user:end21 82mass30

goto next

:service

set /p Service="Enter a service to be re-started: "

sc \\%IP% stop %service%

sc \\%IP% start %service%

goto loop

:next


echo.
echo.
echo.
echo.
echo. Welcome to the KFC Remote services Program
echo.
echo.
echo.
echo.
echo.
echo.
echo. A. Enter a IP address
echo.
echo. B. Enter a Service to be re-started
echo.
echo. Q. Quit
echo.
echo.
echo.


:getchoice
SET Choice=
SET /P Choice=Choose your option and then press Enter (A,B or Q):

IF NOT "%Choice%"=="" SET Choice=%Choice:~0,1%
ECHO.
IF /I "%Choice%"=="A" GOTO IP
IF /I "%Choice%"=="B" GOTO Service
IF /I "%Choice%"=="Q" GOTO End
ECHO "%Choice%" is not valid. Please try again.
goto getchoice
ECHO.

:end

exitI don't understand why you have the Welcome to the KFC Remote services Program" section and the :getchoice section appearing twice. You can't have the :getchoice label twice anyway. Usually, if the same code appears more than once, it's time to stop typing and start thinking.


i've put that in twice so that when the command is run the "command compleated susscefly" is at the top if the command prompt. ( the second one has a few extra echo. on it.

it also makes it look a bit neater.

I should say the th user name and password isn't the real thing and i have sub'ed it for the end21 and 82mass30 for sercerity reasons.

thanks for all your help.

I'm very pleased with the way it works.Quote from: blastman on July 31, 2007, 12:15:27 AM
i've put that in twice so that when the command is run the "command compleated susscefly" is at the top if the command prompt. ( the second one has a few extra echo. on it.

it also makes it look a bit neater.

I meant why did you put all this in a second time? Is it even needed?

How does the code know which :getchoice label to GO to?

Quote
echo.
echo.
echo.
echo.
echo. Welcome to the KFC Remote services Program
echo.
echo.
echo.
echo.
echo.
echo.
echo. A. Enter a IP address
echo.
echo. B. Enter a Service to be re-started
echo.
echo. Q. Quit
echo.
echo.
echo.


:getchoice
SET Choice=
SET /P Choice=Choose your option and then press Enter (A,B or Q):

IF NOT "%Choice%"=="" SET Choice=%Choice:~0,1%
ECHO.
IF /I "%Choice%"=="A" GOTO IP
IF /I "%Choice%"=="B" GOTO Service
IF /I "%Choice%"=="Q" GOTO End
ECHO "%Choice%" is not valid. Please try again.
goto getchoice
ECHO.

i see what u mean.

It does work but maybe this would be better;

echo.
echo.
echo.
echo.
echo. Welcome to the KFC Remote services Program
echo.
echo.
echo.
echo.
echo.
echo.
echo. A. Enter a IP address
echo.
echo. B. Enter a Service to be re-started
echo.
echo. Q. Quit
echo.
echo.
echo.



SET Choice=
SET /P Choice=Choose your option and then press Enter (A,B or Q):

IF NOT "%Choice%"=="" SET Choice=%Choice:~0,1%
ECHO.
IF /I "%Choice%"=="A" GOTO IP
IF /I "%Choice%"=="B" GOTO Service
IF /I "%Choice%"=="Q" GOTO End
ECHO "%Choice%" is not valid. Please try again.
goto getchoice
ECHO.


remove the second :getchoice label to allow it to return to the first one.

I have put it in the 2nd time because when it is run, considering that the "command completed" line appears in the window, the 2nd one has a few extra (echo.) to move the "command completed" line to the top of the window making it easier for users to see if it has worked.

what do u reakon?? or do u think that a better laied out first menu would SUIT???Writing the same code twice is bad programming.


Discussion

No Comment Found