Windows批处理:如何执行命令以使其不后跟换行符。

huangapple go评论54阅读模式
英文:

Windows batch: how to execute a command so that its not followed by a newline

问题

考虑这个:

cd ..

上面命令的输出是:

C:\Users\rodio>cd ..
                         <--- 去掉这一空行。
C:\Users>

然而,我想要实现的是:

C:\Users\rodio>cd ..
C:\Users>

在批处理文件(.bat.cmd)中是否可能实现?

英文:

Consider this:

cd ..

The output of the above command is:

C:\Users\rodio>cd ..
                         <--- Get rid of this empty line.
C:\Users>

What I want to achieve, however, is:

C:\Users\rodio>cd ..
C:\Users>

Is it possible in a batch file (.bat, .cmd)?

答案1

得分: 1

以下是您要翻译的内容:

也许这是一个X-Y问题,需要删除换行符是由于您对未明确说明的要求提出的解决方案引起的?行为与cmd.exe有关,而不是特定于cd命令。

在批处理文件中,您可以使用@echo off来阻止所有命令的回显和提示输出。例如:

@echo off
echo Start
cd ..
echo End

输出:

Start
End

如果您需要报告当前工作目录,那么可以使用不带参数的cd来输出,例如:

@echo off
echo Start
cd ..
cd
echo End

C:\Users\<username>开始执行时输出:

Start
C:\Users
End
英文:

Perhaps this is an X-Y problem, and the need to remove the newline is caused by your solution to an unstated requirement? The behaviour is with cmd.exe rather than the cd command specifically.

In a batch file you can block all command echo and prompt output with @echo off. So for example:

@echo off
echo Start
cd ..
echo End

Outputs:

Start
End

If you need to report the current working directory, then that is output by cd with no parameter: e.g:

@echo off
echo Start
cd ..
cd
echo End

executed starting from C:\Users\&lt;username&gt; outputs:

Start
C:\Users
End

答案2

得分: 0

以下是要翻译的内容:

只是猜测你真正的问题...

@echo off
echo 当前路径是 %CD%,这里没有换行符

或者对于没有影子变量的其他命令

@echo off
REM *** 获取命令的输出
FOR /F "tokens=* %%A in ('ver') do set "版本=%%A"
echo 版本 "%version%" 已找到

英文:

Only guessing your real question...

@echo off
echo The current path is %CD% and here is no line feed

or for other commands without a shadow variable

@echo off
REM *** Get the output of a command
FOR /F &quot;tokens=* %%A in (&#39;ver&#39;) do set &quot;verion=%%A&quot;
echo The verion &quot;%version%&quot; was found

huangapple
  • 本文由 发表于 2023年5月13日 13:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定