We help IT Professionals succeed at work.

I need a batch file that accepts user input and compares it to a fixed string.

Medium Priority
366 Views
Last Modified: 2012-05-06
I need a batch file that accepts user input and compares it to a fixed string.
I'm trying to avoid the "strike any key to continue" scenario that one might get with a pause command...i.e. batch should wait for user input, check the input against a stored value (say, username) and only close once the input string compares correctly with the stored value.
Comment
Watch Question

DonNetwork Administrator
CERTIFIED EXPERT

Commented:
something like this?



@echo off
:beginning
cls
set /p user=what is your name?
if not %user%==%username% goto beginning
echo thank you
It might be a good idea to add:

break off

If you give more details to exactly what you're trying to accomplish I'm sure we could make something very useful for you. Batch scripting is extremely powerful for buliding console-based applications.

Author

Commented:
What I'm looking for is an announcement that will persist on the user's monitor that will persist until a specific string is entered by the user.

I was originally using netsend to push the announcement to appropriate targets, but some users have disabled messenger service, and some users type right past the message and consequently don't see the announcement. Still others aren't in the same domain, and consequently don't get the netsend message.

Nice thing about netsend is that it left a record in the event logs that the message was received.
My workaround is to run a batch file locally on each target box as an "at" job.
I don't know how to make a local batch file that will meet these criteria:

The suggestion first offered here can be dismissed with the <ENTER> key, so won't be persistent.
I want to require the user to enter a specific string, so that it will be a conscious effort on their part to dismiss the popup message. In the example below, I'm trying to use "OK" as the validation string...

@echo off
cls
echo <Alert message here>

:loopit
set /p user=Enter "OK" to acknowledge receipt of this message:
if %user%==OK goto thank
if not %user%==OK echo Please type "OK"
goto loopit

:thank
echo thank you
pause
DonNetwork Administrator
CERTIFIED EXPERT

Commented:
No matter how you do it in a batch file, you can dismiss it by closing it "X"
 
I suggest doing it with VBS

Author

Commented:
I'm not concerned about users clicking the X, or using the ALT+F4 keystroke.
Both of these are deliberate.
I'm trying to prevent the batch from closing when a user types nothing but <ENTER>
How to make the DOS box persistent when a user hits the <ENTER> key?
Commented:
@echo off
set string=ok
:start
set input=
set /p input=Enter [%string%] to continue:
if "%input%"=="" goto :start
if not %input%==%string% goto :start

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
A variation to the above uses the system environment variable USERNAME.

@echo off
:start
set input=
set /p input=Enter [%username%] to continue:
if "%input%"=="" goto :start
if not %input%==%username% goto :start

Author

Commented:
Thanks for this solution...works just as I had hoped.
Great job!

Author

Commented:
Thanks t0t0...I had used the env variable %username% earlier, but didn't know how to make it not accept an  key.

All I need now is to push this to target machines and run an at job to schedule it...
Have you any pointers on the AT syntax? (Other than what's available by typing at/?)
 at 13:00 /every M,T,W,Th,F

DonNetwork Administrator
CERTIFIED EXPERT

Commented:
this is for the AT syntax

http://support.microsoft.com/kb/313565

Commented:
Acip

thank you for accepting my solution

sorry, i can't help you with AT commands at the moment

What are you trying to accomplish with the AT command? I'm an expert at Windows console scripting. Perhaps I can help.

Justin Chandler

Author

Commented:
Hi Justin -

I've resolved the issue... /cmd c works wonders.
No need for follow-up.

Acip
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.