英文:
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\<username>
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 "tokens=* %%A in ('ver') do set "verion=%%A"
echo The verion "%version%" was found
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论