Search code examples
filebatch-filelines

How to check if a line in a file exceeds a number of characters


I am wondering if there is an easy way to check for files in a directory that contain a line that exceeds a certain number of characters. For example, I have a directory with 10000 files and I would like to see which files have at least one line that has over 1000 characters. Is it possible to check this via a batch script? Thank you.

This is for Windows 7 Enterprise, 64-bit, Service Pack 1


Solution

  • Easiest and fastest way would be to use the grep binary from GnuWin32. I believe this syntax would work:

    grep -Pl ".{1000}" *
    

    Which will perform a perl-syntax regular expression search in * for any line containing 1000 characters, and output the filename if a match is found.

    It would definitely be possible to accomplish what you are asking with a pure batch script, but a for loop looping through 10,000 files with who-knows-how-many lines each, would take forever and a day.


    OK Prof. Pickle, here's your batch file. I went with using variable substring extraction for speed. Also, if a line with 1000 characters is encountered, immediately move to the next file. I still reckon grep will be faster and simpler. o°/

    @echo off
    setlocal enabledelayedexpansion
    for %%a in (*) do (
        call :look "%%a"
    )
    
    goto :EOF
    
    :look
    for /f "usebackq delims=" %%I in ("%~1") do (
        set "line=%%I"
        if "!line:~999,1!" neq "" echo %~1 && exit /b
    )