Update . For output to a file, as is now reflected in the title of the question, special considerations are not applied (use the usual output redirects), which ultimately makes this question a duplicate of Windows Package: echo without a new line .
If you understand correctly, you are looking for a way to echo (print to stdout) a line without breaking the line.
cmd.exe echo does not allow you to do this, but there is a workaround :
set "var=world" <NUL set /p ="Hello, %var%: " <NUL set /p ="Still on the same line. " echo Still on the same line, but ending it now.
This outputs the following, one line:
Hello, world: Still on the same line. Still on the same line, but ending it now.
Caveat re error level : trick <NUL set /p ... sets %ERRORLEVEL% to 1 ; to explicitly reset it to 0 , use a dummy type command, for example ver >NUL .
There are limitations :
The line should not contain " chars. - while you can double them to avoid a syntax error, they will also be displayed in double output.
The string first non-femoral character should not be = - either as a literal or as a result of the expansion of a variable.
<nul set /p ="=foo" and <nul set /p ==foo inexplicably cause a syntax error (!).- (
<nul set /p =^=foo also causes a syntax error, and <nul set "/p==foo" actually interpreted as a non-interactive setting of the /p variable for the value =foo and therefore produces no output.)
- Leading tabs and spaces are highlighted from the line.
- Note that if you do not duplicate the line, the command will be split if the line contains shell metacharacters such as
& and | .
This answer can provide a reliable solution to handle all of these edge cases, although aux is required for this. batch file.
How it works:
set /p <varName>=<value> intended to request the user the value of a variable by reading a string from stdin, using <value> as the prompt string to print inline; try using set "/pFOO=Enter FOO: "
Something like <NUL set /p ="..." then uses this built-in print of the prompt line when suppressing the interactive prompt (by providing stdin input from the NUL device ( <NUL ), which leads to the immediate return of set /p , because read nothing).
Note that /p ="..." does not contain the variable name before = , if we are not interested in assigning the variable here - set , fortunately, excellent with this (you can use the name of the dummy variable - for example, /p UNUSED="..." , but note that you can inadvertently overwrite an existing variable with this name, and using the variable name does not help with a syntax error with the leading = string.