我担心仍然患有扩张功能障碍。我试图从日志文件的开头剥离GTR而不是100行。以下看起来像它应该工作,但如图所示,for / f“跳过”近端不起作用。有人可以解释这里有什么问题吗?谢谢!
@echo off
SETLOCAL EnableDelayedExpansion
Set "logfile=abc.log"
rem ~ Get line count and exit if not greater 100
For /f %%G in ('Type "!logfile!"^|Find "" /v /c') Do SET /a linecount=%%G
If not %linecount% GTR 100 exit /b
rem ~ get number lines to skip
set /a skiplines=!linecount! -100
echo. &echo logfile=%logfile%, linecount=%linecount%, skiplines=%skiplines%
:: Above displays: logfile=abc.log, linecount=243, skiplines=143
::这就是问题:
rem ~ skip the first %skiplines% in file and write remaining to temp
for /f "skip=!skiplines! delims=*" %%H in ("!logfile!") do echo %%H >>newfile.tmp
::上面给出错误:“!skiplines!delims = *”此时出乎意料。“
for /f "skip=%skiplines% delims=*" %%H in ("!logfile!") do echo %%H >>newfile.tmp
:(正如预期的那样,将0写入newfile.tmp)
一种解决方案 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
我正在编辑以突出显示这个单行解决方案以及已接受的答案。代码需要usebackq,包含在@Magoo中。这有效:
for /f "usebackqskip=%skiplines% delims=*" %%H in ("!logfile!") do echo %%H>>newfile.tmp
答案 0 :(得分:1)
问题是在此过程中评估!var!
和%var%
的时间。 %var%
首先被评估(替换),但!var!
仅在建立循环条件后评估,因此不期望!var!
位置。
解决方案是仅按预期使用!var!
(在循环内),其中值在运行时确定,而不是在解析时确定。使用%var%
访问分析时间值。不要将!var!
和%var%
视为循环之外的等价物。对于命令他们实际上是,但不是全部。
顺便说一句 - 看看linecount
是否真的发生了什么。SKIP
不喜欢0.如果我是你,我会将GTR
更改为GEQ
...
这是一个应该展示正在发生的事情的例程。它使用我的工作区 - 毫无疑问,你需要为你的系统改变它。
@ECHO OFF
SETLOCAL
:: Just setting up data
SET "sourcedir=U:\sourcedir"
Set "logfile=abc.log"
PUSHD "%sourcedir%"
(
FOR /l %%a iN (1,1,99) DO ECHO line %%a
)>"%logfile%"
CALL :process
>>"%logfile%" ECHO line 100
CALL :process
>>"%logfile%" ECHO line 101
CALL :process
>>"%logfile%" ECHO line 102
CALL :process
popd
GOTO :eof
:process
For /f %%G in ('type "%sourcedir%\%logfile%"^|Find "" /v /c') Do SET /a linecount=%%G
ECHO %linecount% lines IN log file
IF %linecount% leq 100 EXIT /b
ECHO MORE than 100 lines!!
SET /a skiplines=linecount - 100
SET /a skiplines2=%linecount% - 100
ECHO ------- observe response from this instruction
SET /a skiplines3=!linecount! - 100
ECHO ------- this is because of the sequence-of-evaluation-and-substitution
SET skipline
:: delete the output file
DEL newfile.tmp >NUL 2>NUL
FOR /f "usebackqskip=%skiplines% delims=*" %%H IN ("%sourcedir%\%logfile%") DO ECHO %%H >>newfile.tmp
ECHO =====================
ECHO after skipping %skiplines% of %logfile% result is
TYPE newfile.tmp
ECHO =====================
EXIT /b
结果应为
99 lines IN log file
100 lines IN log file
101 lines IN log file
MORE than 100 lines!!
------- observe response from this instruction
Missing operator.
------- this is because of the sequence-of-evaluation-and-substitution
skiplines=1
skiplines2=1
skiplines3=0
=====================
after skipping 1 of abc.log result is
line 2
line 3
...
line 100
line 101
=====================
102 lines IN log file
MORE than 100 lines!!
------- observe response from this instruction
Missing operator.
------- this is because of the sequence-of-evaluation-and-substitution
skiplines=2
skiplines2=2
skiplines3=0
=====================
after skipping 2 of abc.log result is
line 3
line 4
...
line 101
line 102
=====================
答案 1 :(得分:1)
可能是因为您的文件位于!logfile!
,而不是!countfile!
。 %skiplines%应该有效,因为它没有在设置的同一语句中使用。