Colin
Colin

Reputation: 215

Force batch file to answer yes to prompts

I've seen this question answered here, however it doesn't seem to work for my specific example. I'm writing a brief batch file for the first time, and the command I want it to perform is:

net time \\compname /set

This normally prompts for a yes or no confirmation. I wanted to avoid this for the batch file and saw people saying you can add:

echo y | net time...

However, when I do it with this command, I can see it asks for confirmation and then immediately following this it has a line saying: "No valid response was provided."

Does anyone know if there is a flag that I am unaware of that could fix this or why in this case the echo y being piped in gives this funny response?

Upvotes: 6

Views: 22870

Answers (2)

Martin Binder
Martin Binder

Reputation: 1096

the net time command supports the (undocumented) parameter "/yes", so the answer in this case is quite simple:

net time \\compname /set /yes

Upvotes: 9

paddy
paddy

Reputation: 63501

Confirmed this behaviour. I wonder if the input stream is being cleared when NET runs. If I run it and immediately type some characters, they show up after it eventually gives the prompt, but piping or file redirection don't work. Some programs that are intended to be interactive do have this frustrating trait.

Try this work-around, which retrieves the time from the computer and then sets it using date and time which can take data from a pipe.

for /f "tokens=6-7" %a in ('net time \\compname') do (
    echo Setting system time to %a %b
    echo %a | date > nul
    echo %b | time > nul
)

And remember to use an extra % for all those variables if this is in a batch file. Thanks to Microsoft for making scripting a chore.

Upvotes: 0

Related Questions