Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Count Rows in a Text File  (Read 2858 times)

0 Members and 1 Guest are viewing this topic.

billsack

  • Guest
Count Rows in a Text File
« on: October 31, 2006, 09:05:48 AM »
Hi Chaps,

Has anyone ever done this before?

I have a text file and want to count the number of rows it has. I know I can do this by importing into a database but this isn't feasible in the context of what I am doing.

I have some code (help from here already!!!!!!!) that provides me with a list of files in a directory eg

Filename1
Filename2
Filename3

This works great. However I would like to get the batch file to also count the number of rows in the text file and put this in eg

Filename1 25
Filename2   2
Filename3 178

Is this possible. I thought of perhaps counting the number of times the line feed appears in the file but am not really sure where to start.

I would really appreciate any help.

Cheers

QBasicMac

  • Guest
Re: Count Rows in a Text File
« Reply #1 on: October 31, 2006, 09:37:28 AM »
Well, any programming language supports that. But I am just curious: DIR already reports file size. Why would you focus on number of lines rather than file size?

Mac

GuruGary



    Adviser
    Re: Count Rows in a Text File
    « Reply #2 on: October 31, 2006, 11:34:54 AM »
    What OS are you running?  If Windows 2000 / XP / 2003 / Vista, you can use:
    Code: [Select]
    @echo off
    setlocal
    if not {%1}=={} goto :Continue
    echo No parameter specified. Syntax: %0 filename
    goto :EOF
    :Continue
    for /f "tokens=1 delims=:" %%a in ('findstr /n . %1') do set lines=%%a
    echo The file %1 has %lines% lines.

    DosItHelp



      Intermediate
      Re: Count Rows in a Text File
      « Reply #3 on: November 02, 2006, 12:17:13 AM »
      billsack,

      Based on your other post I think this is what you want:

      [edit](for /f "delims=" %%a in ('DIR *.xxx /b /s') do (
          for /f %%b in ('type "%file%"|find "" /v /c') do echo.%%a %%b
      ))>MyDIR.txt[/edit]

      right or not? ;)

      billsack

      • Guest
      Re: Count Rows in a Text File
      « Reply #4 on: November 02, 2006, 08:17:43 AM »
      Hey guys,

      Thanks for this. Cant get the code to work yet though.

      Mac - the reason i am doing this is to get a row count for an import log. I don't need to know the filesize, just the number of records it contains!

      The code to create the txt file with the file list is:

      dir G:\share\CRS\FTPDownloads/b s>G:\share\CRS\FTPDownloads\CDSDataImport.txt

      This works great.

      I have tried:

      (for /f "delims=" %%a in (G:\share\CRS\FTPDownloads*.txt* /b /s') do ( for /f %%b in ('type "%file%"|find "" /v /c') do echo.%%a %%b
      ))>G:\share\CRS\FTPDownloads\CDSDataImport2.txt

      But I cant get it to work.

      Cheers for all your help gents!

      GuruGary



        Adviser
        Re: Count Rows in a Text File
        « Reply #5 on: November 02, 2006, 04:16:00 PM »
        Try this:
        Code: [Select]
        @echo off
        setlocal enabledelayedexpansion
        for /f "tokens=1 delims=:" %%a in ('findstr /n . G:\share\CRS\FTPDownloads\CDSDataImport.txt') do set lines=%%a
        echo The file has !lines! lines.

        DosItHelp



          Intermediate
          Re: Count Rows in a Text File
          « Reply #6 on: November 02, 2006, 06:16:31 PM »
          Should have tried it, sorry  ::):

          [edit](for /f "delims=" %%a in ('DIR G:\share\CRS\FTPDownloads\*.txt* /b /s') do (
              for /f %%b in ('"type "%%a"|find "" /v /c"') do echo.%%a %%b
          ))>G:\share\CRS\FTPDownloads\CDSDataImport2.txt[/edit]

          Now, how is that?  ;D
          « Last Edit: November 02, 2006, 06:17:10 PM by DosItHelp »