Reputation: 1975
I would like to set a variable based on the number of lines in a file that contain a give string.
Something like:
set isComplete = 0
%isComplete% = find /c /i "Transfer Complete" "C:\ftp.LOG"
IF %isComplete% > 0 ECHO "Success" ELSE ECHO "Failure"
Or:
set isComplete = 0
find /c /i "Transfer Complete" "C:\ftp.LOG" | %isComplete%
IF %isComplete% > 0 ECHO "Success" ELSE ECHO "Failure"
Neither of those options work, obviously.
Thanks.
Upvotes: 10
Views: 70518
Reputation: 29806
You don't need to use the for
command; find
will set the ERRORLEVEL
to one of these values, based on the result:
Since it looks like you just want to see if the transfer completed, and not the total count of times the string appears, you can do something like this:
@echo OFF
@find /c /i "Transfer Complete" "C:\test path\ftp.LOG" > NUL
if %ERRORLEVEL% EQU 0 (
@echo Success
) else (
@echo Failure
)
Upvotes: 14
Reputation: 65516
from the command line
for /f "tokens=3" %f in ('find /c /i "Transfer Complete" "C:\ftp.LOG"') do set isComplete=%f
from the batch script
for /f "tokens=3" %%f in ('find /c /i "Transfer Complete" "C:\ftp.LOG"') do set isComplete=%%f
Upvotes: 15