问题
I am having a problem with my life creator program. It simply chooses random numbers that stand for something and puts it together. (I have tried combining 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 variable putter together thing:
@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 OF JUST COMBINING EACH VARIABLE TO ONE AND ECHOING THAT TO ONE LINE) how do I set a line to write to so I can just write each variable to one line, creating the life-form. I am asking this because every time I echo something it goes down 1 line in the text file.
And here is an experimental version of the life creator:
@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
回答1:
Update: To output to a file, as now reflected in the question title, no special considerations apply (use the usual output redirections), which ultimately makes this question a duplicate of Windows batch: echo without new line.
If I understand you correctly, you're looking for a way to echo (print to stdout) a string without a trailing line break.
cmd.exe
's echo
does not allow you to do that, but there's 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, single line:
Hello, world: Still on the same line. Still on the same line, but ending it now.
Caveat re error level: The <NUL set /p ...
trick sets %ERRORLEVEL%
to 1
; in order to explicitly reset it to 0
, use a dummy command such as ver >NUL
.
There are limitations:
The string mustn't contain
"
chars. - while you can double them to avoid a syntax error, they will then appear doubled in the output too.The string's first non-whitespace character mustn't be a
=
- whether as a literal or as the result of variable expansion.<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"
would actually be interpreted as non-interactively setting variable/p
to value=foo
and thus produces no output at all.)
- Leading tabs and spaces are stripped from the string.
- Note that if you don't double-quote the string, the command will break if the string contains shell metacharacters such as
&
and|
.
This answer may provide a robust solution to handle all these edge cases, although it does require an aux. batch file.
How it works:
set /p <varName>=<value>
is designed to prompt the user for a variable value, by reading a line from stdin, using <value>
as the prompt string to print inline; try it with set "/pFOO=Enter FOO: "
Something like <NUL set /p ="..."
then uses this inline printing of the prompt string while suppressing the interactive prompt (by providing stdin input from the NUL
device (<NUL
, which causes set /p
to return instantly, because there's nothing to read).
Note how /p ="..."
contains no variable name before the =
, given that we're not interested in assigning to a variable here - set
is luckily fine with that (you may use a dummy variable name - e.g., /p UNUSED="..."
, but note that you may inadvertently overwrite an existing variable by that name; also, using a variable name does not help the syntax-error issue with a string with a leading =
.
来源:https://stackoverflow.com/questions/33321484/echo-to-a-file-without-trailing-a-line-break-batch