回声没有新的线路

回声没有新的线路

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

问题描述

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

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

SETLOCAL DisableDelayedExpansion

这会导致:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:55