Echo to file without ending line break (batch)

I have a problem with my life creation program. He simply picks random numbers that mean something and combines them. (I tried combining the variables) here is the code:

@set num=1 @SET /A a=%RANDOM% * 10 / 32768 + 1 @if %a% == 10 set life%num%= ---- @if %a% == 9 set life%num%=O @if %a% == 8 set life%num%=^ @if %a% == 7 set life%num%=% @if %a% == 6 set life%num%=# @if %a% == 5 set life%num% =@ @if %a% == 4 set life%num%=) @if %a% == 3 set life%num%=() @if %a% == 2 set life%num%=~ @if %a% == 1 set life%num%=OO @echo %a% >"gencode".txt @set num=2 @SET /A a=%RANDOM% * 10 / 32768 + 1 @if %a% == 10 set life%num%=---- @if %a% == 9 set life%num%=O @if %a% == 8 set life%num%=^ @if %a% == 7 set life%num%=% @if %a% == 6 set life%num%=# @if %a% == 5 set life%num% =@ @if %a% == 4 set life%num%=) @if %a% == 3 set life%num%=() @if %a% == 2 set life%num%=~ @if %a% == 1 set life%num%=OO @set life3= %life1%-%life2% @echo %life3%>"lifeform".txt @echo %a% >>"gencode".txt 

and here is the putter variable together:

 @set final= 500 @set /a pass= %pass% + 1 @set /a cnum= %cnum% + 1 @set counterold=%counter% @set life%cnum%-%counter%= %a%-%b% @if %pass% == 1 goto start :repeat @if %ficnum% == 500 goto final @set life2%cnum%-%counter%= life%counterold%-life%counter% @set life2%cnum%-%counterold%= life%counterold%-life%counter% @set /a ficnum= %ficnum% + 1 @set life3%ficnum%-%counter%= life2%counterold%-life2%counter% @goto repeat :final @set /a final= %final% + 1 @set /a counter= %counter% + 1 @set /a ficnum= %ficnum% + 1 @set counter1= %counter% @set ficnum1= %ficnum% :finalst @set /a final= %final% - 1 @set /a counter1= %counter1% - 1 @set /a ficnum= %ficnum% - 1 @set /a ficnum1= %ficnum1% - 1 @set /a counter= %counter% - 1 @set final%final%=%life3%%ficnum%-%counter%-%life3%%ficnum1%-%counter1% @if %final% == 0 goto finalend @goto finalst :finalend @echo final0>"lifeform".txt 

So, I would like to know (INSTEAD ONLY COMBINE EVERY VARIABLE IN ONE AND SAVE THAT IN ONE LINE), how can I set the line for writing so that I can simply write each variable in one line, form. I ask about this because every time I repeat something, it goes to 1 line in a text file.

And here is the experimental version of the creator of life:

 @set num1=1 :repeat @if %counter% == 500 exit @set /a num1= %num1% + 1 @SET /A a=%RANDOM% * 10 / 32768 + 1 @if %a% == 10 set life%num%=---- @if %a% == 9 set life%num%=O @if %a% == 8 set life%num%=^ @if %a% == 7 set life%num%=% @if %a% == 6 set life%num%=# @if %a% == 5 set life%num% =@ @if %a% == 4 set life%num%=) @if %a% == 3 set life%num%=() @if %a% == 2 set life%num%=~ @if %a% == 1 set life%num%=OO @echo %a% >>"gencode".txt @set /a num2= %num% + 1 @SET /A a=%RANDOM% * 10 / 32768 + 1 @if %a% == 10 set life%num%=---- @if %a% == 9 set life%num%=O @if %a% == 8 set life%num%=^ @if %a% == 7 set life%num%=% @if %a% == 6 set life%num%=# @if %a% == 5 set life%num% =@ @if %a% == 4 set life%num%=) @if %a% == 3 set life%num%=() @if %a% == 2 set life%num%=~ @if %a% == 1 set life%num%=OO @set final= 500 @set /a pass= %pass% + 1 @set /a cnum= %cnum% + 1 @set counterold=%counter% @set life%cnum%-%counter%= %a%-%b% @if %pass% == 1 goto start :repeat @if %ficnum% == 500 goto final @set life2%cnum%-%counter%= life%counterold%-life%counter% @set life2%cnum%-%counterold%= life%counterold%-life%counter% @set /a ficnum= %ficnum% + 1 @set life3%ficnum%-%counter%= life2%counterold%-life2%counter% @goto repeat :final @set /a final= %final% + 1 @set /a counter= %counter% + 1 @set /a ficnum= %ficnum% + 1 @set counter1= %counter% @set ficnum1= %ficnum% :finalst @set /a final= %final% - 1 @set /a counter1= %counter1% - 1 @set /a ficnum= %ficnum% - 1 @set /a ficnum1= %ficnum1% - 1 @set /a counter= %counter% - 1 @set final%final%=%life3%%ficnum%-%counter%-%life3%%ficnum1%-%counter1% @if %final% == 0 goto finalend @goto finalst :finalend @echo final0>"lifeform".txt 
0
batch-file line
source share
1 answer

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.

+3
source share

All Articles