Windows batch file - automatic answer to all prompts - windows

I am running a program that generates a batch file. The batch file has a bunch of prompts that I want to answer with RETURN:
Example:
Type X to quit or <RETURN> to proceed
I have tried:
echo. | batchfile.bat
This only answers the first prompt. I want to answer all prompts, and there are a lot of prompts.

You have 3 ways to do it.
1. As #MarcB mentioned in the comment you can create a file with as much returns as you need, then use the type file.txt| batchfile.bat command
2. Similar like the 1st one, but the command is: batchfile.bat < file.txt
3. Chaining commands (answer with 3 returns): (echo.&echo.&echo.) | batchfile.bat normally, this also can be formatted like this:
(
echo.
echo.
echo.
) | batchfile.bat
Also if your answer (in your case not) contains some special character which is an operation like & | > < you will need to escape them with ^ sign. Like (echo Stan ^& Pan&echo old series) result will be in this case:
Stan & Pan
old series
Side note: Also I don't know what was your usage, but if I had access to modify that batch file, than I would add a new option (like y) and if that option is present, I don't ask anything in the batch file and I answer the question inside the batch file (if there's other programs which asks something). This will make a bit easier the script usage (like: batchfile.bat y).

Related

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?
the net time command supports the (undocumented) parameter "/yes", so the answer in this case is quite simple:
net time \\compname /set /yes
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.

Writing to file with batch

I'm trying to get a batch file to take user input and place it in a file... here is my code so far.
set /p input path=Path:
echo %path% >> log.txt
when I turn echo off, it's putting a "1" infront of the chevrons like so:
echo C:/Example/Path 1>> log.txt
the system can not find the file specified.
Please can anyone explain this
Certainly - I'll answer the question asked.
Originally, >somewhere or any redirection sent the data to the destination specified.
With the changes to the NT version, this was expanded. A digit DIRECTLY preceding a redirector means logical file number where 0=STDIN (standard-input) 1=STDOUT (standard output) and 2=STDERR (Standard error.) The others are undefined. This can cause problems where the data (typically) to be output ends with a digit, hence the >filename echo ... syntax
Consequently, for backward compatibility, (eg) >nul is still processed as it always has been, but it's ECHOed in its explict form 1>nul - sending STDOUT to nul to distinguish it from 2>somewhere_else
try this:
set /p "MyNewPath=Path: "
>>log.txt echo %MyNewPath%
Your SET statement is wrong. :-) Also, PATH is already assigned in the environment (the Windows PATH, and altering it may cause problems with running other commands in your batch file.
Try this instead:
set /p "Input=Path: "
echo %Input% >> log.txt

windows comp command in batch script: remove prompts

Every time I use the Windows comp command, a message containing:
Compare more files (Y/N) ?
is shown. Can I avoid it by typing N by default?
thanks
You can pipe an N in there:
echo N | comp file1.txt file2.txt
You could also consider using another, also built-in, command, FC. It can perform binary comparison (you need to specify the /B switch: FC /B file1 file2), which seems closer to what COMP does, although the results are still displayed in a different format.
Anyway, it doesn't prompt you for more files to compare, it just does the job and terminates.

How to automate application based on command line?

I have a Java based application which I run using command prompt. I am currently testing it manually. I want to automate the process. What tool or scripting should I use?
For example:
First I will run the java command to run the .java file.
My application will give me 5 options. For example
i. Add
ii. Subtract
iii. Multiply
iv. Divide.
It should select one of the options and add the number and verify the result with the expected result. All this will be done using command prompt.
I realize that you're asking for a command prompt solution, but you really should look into JUnit. It will be a much more maintainable solution for a larger variety of test conditions. If you use the command line, you'll have to parse all of the input and output. In order to truly test your code, you shouldn't have to go through the intermediate of the command line (stdin, stdout); you should have Java code that runs your Java code, since it can access the variables.
If you really are set on using the command line, you would have to give us more details on how your program runs. This is just a shot in the dark:
inputs.txt (These are the inputs that will be fed to your program)
Add
4
6
test.bat
#ECHO OFF
:: I just assume that your program prompts for input and waits for stdin
java YourMainClass < inputs.txt > testOutput.txt
:: Program prompts for an operation; "Add" is read from inputs.txt
:: Program prompts for values; "4" and "6" are read from inputs.txt
:: Program prints out the result, which is redirected to testOutput.txt
:: Now you have to read in the testOutput.txt file and look at the last line
FOR /F %%i IN (testOutput.txt) DO SET RESULT=%%i
:: The variable RESULT now contains your program's answer
:: Create a variable to compare your actual result with the expected result
SET EXPECTED=10
IF %RESULT% == %EXPECTED% ECHO "You were correct"
IF NOT %RESULT% == %EXPECTED% ECHO "You were not correct"
You need to use a batch file. Put your commands in a notepad file and save it as .bat.
If they are not native batch commands though you may need to change some things, but you would need to post you commands before I can confirm.
Standard input can be fed into java application. If you're using bash it can be done like the following:
java myapp.java < test1
Where myapp.java is your application with main-method, and test1 being your test input (a plain old textfile).

What does a forward slash before a pipe in cmd do to remove the line ending of an echo?

This code:
#echo off
echo/|set /p ="Executing backup...."
echo/|set /p =" backup procedure"
... came from Echoing in the same line and produces the below output in a cmd window:
Executing backup....backup procedure
However, I cant seem to find an explanation through google on what the forward slash does to the ¿pipe? to cause set's output to be echoed to the console / stdout
If anyone could also suggest a good website for learning more about cmd / cmd programs' features like this, it would be appreciated.
The echo/ is simply a way of printing only an empty line, instead of ECHO IS ON for a single echo.
But in this case it's completly unimportant, as the only use of the echo is for creating some stuff for the pipe, so the set /p will not wait for user input.
But this way to echo text without a linefeed is very inefficient, as a pipe creates two new instances of cmd.exe.
It's much simpler and faster to use
<nul set /p "=My Text"
The redirect from NUL will also stop the waiting for user input.
Care of https://stackoverflow.com/users/1201210/tenterhook
1) echo prints the result of set /p =... with or without the / before the pipe, so I'm not sure what your question is asking
2) (It will also print set /p =... with random junk after the echo, too, since it's reading the piped stuff and not the arguments it receives.)
I will hence suggest edits to the referenced SO post, to prevent others confusion.

Resources