Search code examples
variablesbatch-filestoredelete-filefindstr

Batch file to delete files that have less than 3 lines within them


I need a batch file that will check the amount of lines contained in files within a specified folder that have a .txt extension and delete any of those files that have less than 3 lines.

So far my code prints the total amount of lines within each .txt file within the specified folder. The problem is that I can't figure out how to capture that information into a variable:

@echo off
for %%A in ("D:\test\*.txt") do (
    findstr /R /N "^" %%A | find /C ":"
)

My attempts to modify this code to perform what I need it to has failed:

@echo off
set count=0
for %%A in ("D:\test\*.txt") do (
    set /a count=findstr /R /N "^" %%A | find /C ":"
    if %count% LSS 3 del %%A
)

Please could someone offer some advice or a solution to this issue.

Any help will be greatly appreciated.


Solution

  • This works here:

    @echo off
    for %%a in ("d:\test\*.txt") do for /f %%b in ('find /c /v "" ^< "%%a" ') do if %%b LSS 3 del "%%a"