Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 486
Colin Avatar asked Jul 11 '13 22:07

Colin


People also ask

How do I automatically answer yes to a prompt in cmd?

Pipe the echo [y|n] to the commands in Windows PowerShell or CMD that ask “Yes/No” questions, to answer them automatically.

What is @echo off in batch script?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.

How do I automate a response when I enter a command that asks for input?

To avoid this manual input, use the command "echo Y | del /P " in your batch script to answer the prompt. You can try this echo command to pass input (ex: answer for username and password prompts) to your console application, when it is invoked through batch script.

What does %% A mean in batch?

%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.


1 Answers

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

net time \\compname /set /yes
like image 147
Martin Binder Avatar answered Oct 02 '22 04:10

Martin Binder