回声到文件而不尾随换行符(批处理) [英] Echo to a file without trailing a line break (batch)
问题描述
我的人生创造者计划遇到问题. 它只是选择代表某物的随机数并将其放在一起. (我尝试过合并变量) 这是代码:
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
所以我想知道,(只是简单地将每个变量组合为一行,然后将其组合为一行),我如何设置要写的行,这样我就可以将每个变量都写到一行,从而创造生命.形式. 我之所以这样问,是因为每次我回声时,它都会在文本文件中下降1行.
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
推荐答案
更新:要输出到文件(如问题标题所示), 否特殊注意事项适用(使用通常的输出重定向),最终使该问题成为 Windows批处理的重复:回声而无需换行.
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
的echo
不能 允许您执行此操作,但是有一种解决方法:
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.
这将输出以下单行:
Hello, world: Still on the same line. Still on the same line, but ending it now.
注意事项:错误级别:<NUL set /p ...
技巧将%ERRORLEVEL%
设置为1
;为了将其显式重置为0
,请使用虚拟命令,例如ver >NUL
.
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
.
有限制:
-
字符串不能包含
"
字符.-尽管您可以对它们进行 double 以避免语法错误,但它们随后将在也输出.
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"
和<nul set /p ==foo
会导致语法错误(!). - (
<nul set /p =^=foo
也会引起语法错误,实际上<nul set "/p==foo"
会被解释为非交互地将变量/p
设置为值=foo
,因此根本不会产生输出.)
<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.)
此答案可能会提供一个健壮的解决方案来处理所有这些极端情况,尽管它确实需要辅助.批处理文件.
This answer may provide a robust solution to handle all these edge cases, although it does require an aux. batch file.
工作方式:
set /p <varName>=<value>
旨在通过从stdin读取一行,并使用<value>
作为提示字符串来打印,从而提示用户输入变量值. >内联;尝试使用set "/pFOO=Enter FOO: "
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: "
类似<NUL set /p ="..."
的东西然后在抑制交互式提示的同时使用提示字符串的这种内联打印(通过从NUL
设备提供set /p
立即返回,因为没有内容阅读).
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).
请注意,/p ="..."
在=
之前如何不包含任何变量名,因为我们对此处的变量不感兴趣-set
对此很好(您 may 请使用虚拟变量名称-例如/p UNUSED="..."
,但请注意,您可能会无意中用该名称覆盖现有变量;此外,使用变量名称 not 不会帮助解决字符串的语法错误问题与领先的=
.
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 =
.
这篇关于回声到文件而不尾随换行符(批处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!