窗户CMD:回声没有新的线路,但CR [英] Windows cmd: echo without new line but with CR

查看:107
本文介绍了窗户CMD:回声没有新的线路,但CR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows批处理文件在一个循环内的同一行写。
例如:

  SETLOCAL EnableDelayedExpansion
设置file_number = 0
在%% F(*)做(
  集/ A file_number + = 1
  回声立案数量的工作!file_number!
  something.exe %%˚F

SETLOCAL DisableDelayedExpansion

这会导致:


  

回声文件上工作数1


  
  

回声在文件号工作2


  
  

回声在文件编号工作3


  
  


  。
  


我想所有这些是在同一行上。
我发现一个黑客以去除新线(如这里: Windows批处理:无回声新行),但是这将产生一个长行。

谢谢!


解决方案

 关闭@echo
    SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion    FOR / F %%一个在(复制%〜F0NUL / Z')不要将CR = %%一个    设置算= 0
    对于一个%%的(*)做(
        集/ A计数+ = 1
        < NUL集/ P!=文件上工作数CR!!
    )

第一个执行命令离开该变量中一个回车符复制操作。

现在,在文件循环,每行使用回荡< NUL套/ P 将输出的提示字符串没有一个换行符,并无需等待输入(我们是从 NUL 读)。但呼应里面的数据,我们包括pviously获得的回车符$ P $。

但它的工作,在 CR 变量需要延迟扩展呼应。否则将无法正常工作。

如果由于某种原因,你需要禁用延迟扩展,这样可以不用CR变量使用做了命令替换参数

 关闭@echo
    SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion    FOR / F %%一个在(复制%〜F0NUL / Z')做(
        在/ L %% B(0 1 1000)做(
            < NUL集/ P。=这是该行%% b %%一个
        )
    )

I would like to write on the same line inside a loop in a windows batch file. For example:

setlocal EnableDelayedExpansion
set file_number=0
for %%f in (*) do (
  set /a file_number+=1
  echo working on file number !file_number!
  something.exe %%f
)
setlocal DisableDelayedExpansion

This will result in:

echo working on file number 1

echo working on file number 2

echo working on file number 3

. . .

I would like all of them to be on the same line. I found a hack to remove the new line (e.g. here: Windows batch: echo without new line), but this will produce one long line.

Thanks!

解决方案

@echo off
    setlocal enableextensions enabledelayedexpansion

    for /f %%a in ('copy "%~f0" nul /z') do set "CR=%%a"

    set "count=0"
    for %%a in (*) do (
        set /a "count+=1"
        <nul set /p ".=working on file !count! !CR!"
    )

The first for command executes a copy operation that leaves a carriage return character inside the variable.

Now, in the file loop, each line is echoed using a <nul set /p that will output the prompt string without a line feed and without waiting for the input (we are reading from nul). But inside the data echoed, we include the carriage return previously obtained.

BUT for it to work, the CR variable needs to be echoed with delayed expansion. Otherwise it will not work.

If for some reason you need to disable delayed expansion, this can be done without the CR variable using the for command replaceable parameter

@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f %%a in ('copy "%~f0" nul /z') do (
        for /l %%b in (0 1 1000) do (
            <nul set /p ".=This is the line %%b%%a"
        )
    )

这篇关于窗户CMD:回声没有新的线路,但CR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆