[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB script to count number of rows in text file(s)

Posted on 2012-03-29
9
Medium Priority
?
919 Views
Last Modified: 2012-03-29
Hi,

I have a directory that has a bunch of txt files.  They were derived from a much larger file.  I need to make sure the number of rows in all the files match exactly the number of rows in the original.  Just to make sure the original was separated correctly.  I would compare sizes except in the script to extract the multiple files, the " were removed thus it's not a valid comparison...  

Or maybe a script to just remove the " from a file named 'File.txt' and the result is 'File2.txt'?
0
Comment
Question by:elwayisgod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 58

Expert Comment

by:Bill Prew
ID: 37783327
In dos batch you can use The following to get a line count of a file, and then compare the two.

FIND /C /V "" <file1.txt

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37783385
or

find /c /v  "" *.txt

to report back the no. of lines in each .txt file in a dir.

Steve
0
 
LVL 11

Accepted Solution

by:
paultomasi earned 1000 total points
ID: 37783605
Nice one bill...

elwayisgod

Please run this batch file to process all your text files:

NOTE: Copy & Paste the code into Notepad and save it as a .BAT file in the folder where your text files reside.
@echo off

if "%*"=="" (
  echo Usage: %~n0 filename ^(where filename is original file^).
  exit /b 1
)

if not exist "%*" (
  echo Error: Bad path or filename.
  exit /b 1
)

set logfile=%~nx0.log
del "%logfile%" 2>nul

for /f %%a in ('find /c /v ""^<"%~f1"') do set rows=%%a

set filecount=0
set count=0

for %%a in (*.txt) do (
  set /a filecount+=1
  if not "%%~f1"=="%%~fa" (
    for /f %%b in ('find /c /v ""^<"%%~fa"') do (
      if %%b neq %rows% (
        set /a count+=1
        echo %%a %%b rows
        echo %%a ^(%%b rows^)>>%logfile%
      )
    )
  )
)

echo.
echo Original file: %~f1
echo Number of rows: %rows%
echo.
echo %filecount% files checked.
echo %count% files with unmatching number of rows.
echo.
if %count% gtr 0 start Notepad %logfile%
exit /b 0

Open in new window

NOTE: Run the batch file speciying the name of the original file as a parameter (may include the full path if the file is located elsewhere).
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1000 total points
ID: 37783806
Or a simpler way to get the count of all text files in one dir:

@echo off
setlocal enabledelayedexpansion
cd /d "c:\yourdir"

set count=0
for /f "tokens=2 delims=:" %%a in ('find /c /v  "" *.txt') do set /a count=count+%%a

echo Total count: %count%
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37784506
Steve

How do I text you diectly (privately) as one EE member to another? Can we do that in EE?
0
 

Author Comment

by:elwayisgod
ID: 37784642
I ran it but all I get is:

Usage:  Rows filename (where filename is original file).
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37784671
Yep...

Run it from say, DOS, like this:

    rows filename.txt

where filename.txt is the original text file which the other text files need to be matched against.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37784851
Paul - don't know if you can but mobile / email / Skype details in my details if you follow profile or look at http://scripts.dragon-it.co.uk/ ?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37785819
Thanks.  Paul's solution comprehensive as always, mine just quick simple one.

steve
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question