如何使用FINDSTR从robocopy日志文件中去除%行?
问题描述:
我想从我的robocopy日志文件中去除所有具有%progress-value的行。该线路是这样的:如何使用FINDSTR从robocopy日志文件中去除%行?
*EXTRA Datei 7.3 g test.pst
Neuer 7.3 g huge.PST
0.0%
0.0%
0.0%
0.1%
0.1%
0.1%
0.1%
99.8%
99.9%
99.9%
99.9%
99.9%
99.9%
100%
Neue Datei 7.5 g another.PST
0.0%
我不是成功的,这种尝试
FINDSTR.exe /V "%" LogFile.txt > LogFileWithoutPercentageLines.txt
因为这减少过多行范围(在上面的例子与huge.PST
和another.PST
整条生产线)
%-lines包含0.0%之前的两个空格和一个CR。
%-lines包含10.0%之前的一个空格和一个CR。
%-line在100.0%之前不包含空格,但在100%之后包含两个空格和一个CR。
我该如何摆脱这个毫无用处的0.0%,直到只有FINDSTR的100%行?
答
使用robocopy
with /NP
switch,CF 记录选项在robocopy /?
:
/NP :: No Progress - don't display percentage copied.
例如,robocopy "%_source%" "%_target%" /log:"%temp%\38610436.log" /NP
然而,接下来的评论脚本表示去除一切不必要的可能方式从现有的杂草出没日志文件(findstr
本身不足以对这个复杂的任务):
@ECHO OFF >NUL
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_source=d:\bat\odds and ends\B" rem change to match your circumstances
set "_target=d:\bat\odds and ends\Bcopy" rem detto
set "_logFileRawData=%temp%\38610436.log" rem detto
set "_logFileCleaned=%temp%\38610436a.log" rem detto
rem /NP : No Progress - don’t display % copied
rem robocopy "%_source%" "%_target%" /log:"%temp%\38610436.log" /NP
robocopy "%_source%" "%_target%" /log:"%temp%\38610436.log"
>"%_logFileCleaned%" (
for /f "usebackq tokens=1,* delims=:" %%G in (`findstr /N "^" "%_logFileRaw%"`) do (
set "_line=%%H"
call :strip_Progress
SETLOCAL EnableDelayedExpansion
echo(!_line!
ENDLOCAL
)
)
ENDLOCAL
goto :eof
:strip_Progress
set "_zero=" rem zero percent indicator
set "_line=%_line%" rem remove all Carriage Return characters
rem after above command, _line with progress could be, excluding Square Brackets:
rem [New File 0 ZeroFile.ext100% ]
rem [New File 51808 TinyFile.txt 0% 100% ]
rem [Newer 1.1 m Mid_File.ext 0% 21% 42% 64% 85% 100% ]
rem [New File 162.1 m Big_File.ext 0.0% 0.6% 1.2% … 99.3% 99.9%100% ]
if "%_line%"=="" goto :eof
if "%_line:~-6%"=="100%% " call :removeProgress
goto :eof
:removeProgress
set "_line=%_line:~0,-6%" rem strip six trailing characters
if defined _zero goto :eof
rem as yet, zero percent indicator not set
:stripTrailingSpace
if "%_line:~-2%"==" " set "_line=%_line:~0,-1%" & goto :stripTrailingSpace
if "%_line:~-4%"==" 0%%" set "_zero=4" & set "_line=%_line%x%%"
if "%_line:~-6%"==" 0.0%%" set "_zero=6"
if "%_line:~-1%"=="%%" goto :removeProgress
goto :eof
更多资源(必读的,不完全):
- (命令参考)An A-Z Index of the Windows CMD command line
- (有益的特殊性)Windows CMD Shell Command Line Syntax
- (
%G
,%H
等特殊页面)Command Line arguments (Parameters) - (特殊页)EnableDelayedExpansion
- (
>
,&
等特殊页)Redirection - (
%_line:~0,-6%
等)Extract part of a variable (substring) - (
%%
一倍百分号)Escape Characters, Delimiters and Quotes
非常感谢您对你的解决方案仅用于记录:在这种情况下执行没有百分比进度的robocopy不是一个选项,因为用户必须在屏幕上看到像7.5 GB * .pst这样的大文件的进度。没有办法使用/ NP仅用于日志,AFAIK。 – PeterCo