Single line echo

I looked at the previous questions of your db and I have not tried to answer, but I try.

I would like to write the following line code:

echo Executing backup.... backup procedure echo Ok 

but the output should be:

 Executing backup... Ok 

Is it possible ?!

+7
windows echo batch-file windows-server-2003
source share
5 answers

I assume you are using dos / nt-batch.

This is possible with the set / p command, because set / p does not print CrLf

 set /p "=Executing backup...." <nul echo OK 

It is also possible to erase a line with the symbol CR. It’s important to know that the space characters at the beginning of the / p set are ignored (in Vista, not in XP), therefore! Cr! should be posted later or at the end. CR can only be displayed with delayedExpansion because% cr% works, but CR characters are deleted in the percent expansion phase (or immediately after this phase), but not in the slow expansion phase.

Example counter that uses only one line to display

 @echo off setlocal EnableDelayedExpansion EnableExtensions call :CreateCR for /l %%n in (1,1,10000) do ( set /P "=Count %%n!CR!" <nul ) echo( goto :eof :CreateCR rem setlocal EnableDelayedExpansion EnableExtensions set "X=." for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094! echo !X! > %temp%\cr.tmp echo\>> %temp%\cr.tmp for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do ( endlocal set cr=%%a goto :eof ) goto :eof 

EDIT: Explanation of how the variable cr is created (done with the trick)

After setting the variable X to one point (the character itself is unimportant), it is repeated to become 8188 characters with for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!

Then a variable, two spaces, and both CR and LF are reflected in the file with echo !X! > %temp%\cr.tmp echo !X! > %temp%\cr.tmp (note the two spaces between !X! and > , and the internal echo-internal line corrects internally)

Now we have 8192 characters, but the data buffer can only contain 8191 characters, so the last character (translation line) will be discarded!

In the next line echo\>> %temp%\cr.tmp , another CR/LF set is added to the file ( \ carriage return and line feed are not displayed in the command just for output, since echo output ECHO is ON/OFF ), this is important since a single cr cannot be read at the end of the line (in more detail).

So now the file contains <8188 .'s><SPACE><SPACE><CR><CR><LF>

for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do reads the second token, delimiters are the standard space and tab, so the second token is only one cr , since the next CR/LF is removed as a standard line.

Finally, endlocal used to return to the environment without the temporary variables X , c and a existing (as in endlocal in brackets, this allows you to set cr to endlocal really affects the end of the brackets (can also be written as for /f "tokens=2 usebackq" %%a in ("%temp%\cr.tmp") do endlocal&set cr=%%a&goto :eof )

Additionally

This was my first way to create a CR character, but it needs some time and a temporary file.
Later, I saw a simpler method for extracting CR from the copy /z command.

 for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a" 
+9
source share

Try this on a Posix system (Linux)

 echo -n "Executing backup.... " echo -n "backup procedure " echo "Ok" 

Windows is much more complicated. You will need to use something like this:

 @echo off echo|set /p ="Executing backup...." echo|set /p =" backup procedure" 

Check this post: http://www.pcreview.co.uk/forums/showpost.php?s=1a20b16775d915998b30bd76a0ec5d35&p=4432915&postcount=7 .

+2
source share

This is a bit of a hack, but here is an article describing how to do this for Windows.

From the article, the final result (edited for your setup) is as follows:

 SET /P var=Backing up %Result%....<NUL Backup_process %Result% >NUL 2>&1 IF ERRORLEVEL 1 ECHO FAIL ELSE ECHO OK 
+1
source share

I did something similar using VBScript.

Put this code in EchoNoNewline.vbs :

 If WScript.Arguments.Named.Exists("TEXT") Then WScript.StdOut.Write WScript.Arguments.Named.Item("TEXT") End If 

In your batch file, use a script like this:

 CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Executing backup...." backup procedure CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Ok" 
0
source share

at What does the forward slash in front of the pipe in cmd to remove the end of the echo line? best deal:

for echo text without line feed is very inefficient, as the channel creates two new instances of cmd.exe. It is much easier and faster to use.

  <nul set /p "=My Text" 

Redirecting from NUL will also stop waiting for user input.

-one
source share

All Articles