FOR循环中不同TYPE命令行为的原因是什么?

时间:2017-12-09 12:16:16

标签: batch-file for-loop types

我有以下文件来自 robocopy

-------------------------------------------------------------------------------

   ROBOCOPY     ::     Robust File Copy for Windows                              

-------------------------------------------------------------------------------

 Started : Sat Dec 09 20:40:18 2017

  Source : D:\shareME\test\
     Dest : \\remoteServer\delME\

    Files : test.zip

  Options : /TEE /COPY:DAT /Z /IS /R:1000000 /W:30 

------------------------------------------------------------------------------
                       1    D:\shareME\test\
        Newer            181.8 m    test.zip
 56.0%
 56.1%
 56.1%
 56.1%
 56.2%
 56.2%
 56.2%
 56.3%
 56.3%
 56.3%
 56.4%
 56.4%
 56.4%
 56.5%
 56.5%
 56.5%
 .
 .
 .
 *up to
 68.3%

当我在此日志文件中使用类型命令时,我得到以下内容(完全可取,因为不显示不必要的旧%):

 -------------------------------------------------------------------------------

 ROBOCOPY     ::     Robust File Copy for Windows

 -------------------------------------------------------------------------------

   Started : Sat Dec 09 20:40:18 2017

    Source : D:\shareME\test\
      Dest : \\remoteServer\delME\

     Files : test.zip

   Options : /TEE /COPY:DAT /Z /IS /R:1000000 /W:30

 ------------------------------------------------------------------------------

                            1    D:\shareME\test\
  68.6%      Newer                181.8 m        test.zip

但是,如果我在批处理文件中的 for 循环中输入相同的命令,则会显示较旧的文件复制进度百分比。

for /f "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 delims= " %%a in ('type upload2.log') do ( echo %%a %%b %%c .... %%z )

如此处所见

 -------------------------------------------------------------------------------

 ROBOCOPY     ::     Robust File Copy for Windows

 -------------------------------------------------------------------------------

   Started : Sat Dec 09 20:40:18 2017

  Source : D:\shareME\test\
     Dest : \\remoteServer\delME\

    Files : test.zip

   Options : /TEE /COPY:DAT /Z /IS /R:1000000 /W:30

 ------------------------------------------------------------------------------

                            1    D:\shareME\test\
  56.3%      Newer                181.8 m        test.zip

这是什么原因?

我必须使用类型命令,因为只要我需要阅读它就会使用该文件。

1 个答案:

答案 0 :(得分:1)

下面评论的批处理文件代码修改了upload2.log来自

-------------------------------------------------------------------------------

 Started : Sat Dec 09 20:40:18 2017

  Source : D:\shareME\test\
     Dest : \\remoteServer\delME\

    Files : test.zip

  Options : /TEE /COPY:DAT /Z /IS /R:1000000 /W:30 

------------------------------------------------------------------------------
                       1    D:\shareME\test\
        Newer            181.8 m    test.zip
 56.0%
 56.1%
 56.1%
 56.1%
 56.2%
 56.2%
 56.2%
 56.3%
 56.3%
 56.3%
 56.4%
 56.4%
 56.4%
 56.5%
 56.5%
 56.5%
 68.3%

-------------------------------------------------------------------------------

 Started : Sat Dec 09 20:40:18 2017

  Source : D:\shareME\test\
     Dest : \\remoteServer\delME\

    Files : test.zip

  Options : /TEE /COPY:DAT /Z /IS /R:1000000 /W:30 

------------------------------------------------------------------------------
                       1    D:\shareME\test\
        Newer            181.8 m    test.zip
  68.3%

我希望这是你期望的输出。

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Define name of log file.
set "LogFile=upload2.log"

rem Does this file exist at all?
if not exist "%LogFile%" goto :EOF

rem Define name of a temporary file created with name of batch
rem file with file extension TMP in folder for temporary files.
set "TmpFile=%TEMP%\%~n0.tmp"

rem Search in log file for lines starting with 0 or more leading spaces,
rem having next at least one digit, followed by 0 or more digits or a point
rem (floating point number), then a percent sign, and 0 or more trailing
rem spaces at end of line using a regular expression. But wanted for output
rem are only those lines NOT matching this expression (inVerted output).
rem The output lines not matching the expression are redirected into the
rem temporary file which is created new or overwritten if existing already.

%SystemRoot%\System32\findstr.exe /V /R /C:"^ *[01234567689][0123456789.]*%% *" "%LogFile%" >"%TmpFile%"

rem Then run again the regular expression search, but this time in a command
rem process in background started by command FOR and with printing to handle
rem STDOUT the lines matching the regular expression captured by FOR.
rem Each captured line is assigned to loop variable I being next assigned
rem to environment variable LastPercentLine. Just the last line found in
rem the log file is of interest for further processing of this batch file.

set "LastPercentLine="
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /R /C:"^ *[01234567689][0123456789.]*%% *" "%LogFile%" 2^>nul') do set "LastPercentLine=%%I"

rem If a line with a percent value could be found at all in the log file,
rem append this line to the temporary file and replace the log file with
rem the temporary file with just the last percent value line. Otherwise
rem delete the temporary file and keep the log file unmodified.

if defined LastPercentLine (
    echo %LastPercentLine%>>"%TmpFile%"
    move /Y "%TmpFile%" "%LogFile%"
) else del "%TmpFile%"

endlocal

将为处理 STDOUT 而编写的命令或控制台应用程序的输出重定向到文件会导致写入文件,输出到 STDOUT ,即使命令或应用程序使用特殊的在输出一行返回到此行的开头后,在控制台窗口中移动文本光标的代码,以便在控制台窗口中覆盖另一个文本的正确输出文本。

由于命令/应用程序使用的特殊文本光标移动代码,具有百分比指示的输出最终可在控制台窗口中按预期读取。但重定向到文件的输出包含所有输出文本,因为将文本光标移动到控制台窗口中的特定位置的特殊代码对文件没有影响。

要从日志文件upload2.log获取没有前导和尾随空格的百分比值,批处理文件可以减少到例如:

@echo off
set "LastPercentValue=0%%"
for /F %%I in ('%SystemRoot%\System32\findstr.exe /R /C:"^ *[01234567689][0123456789.]*%% *" "upload2.log" 2^>nul') do set "LastPercentValue=%%I"
echo %LastPercentValue% copied.
set "LastPercentValue="

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rem /?
  • set /?
  • setlocal /?