Search code examples
batch-filedos

set names of all files in a variable where number of lines is greater than one


Say a directory contains the following files:(no sub directories)

AError.csv - 1 line

BError.csv - 2 lines

CError.csv - 10 lines

DError.csv - 10 lines

ASuccess.csv - 1000 lines

then output should be: all files names where the file name contains error and has more than one lines.

For above output should be:

BError.csv CError.csv DError.csv

I have set number of lines in a variable

@echo off

cls

setlocal EnableDelayedExpansion

set "cmd=findstr /R /N "^^" *.csv | find /C ":""


for /f %%a in ('!cmd!') do set number=%%a

echo %number%

Solution

  • @echo off
    
        setlocal enableextensions enabledelayedexpansion
        set "_list="
        for %%f in ("*error*.csv") do (
            for /F %%l in ('type "%%~ff" ^| find /c /v "" ') do (
                if %%l gtr 1 set "_list=!_list! %%~nxf"
            )
        )
        echo %_list%
        endlocal
    

    For each file which meets the naming condition, count the number of lines and, if greater than 1 concat to variable.