Count no. of records(lines) in files

February 25, 2010 at 02:31:16
Specs: Windows XP, Core2Duo/2GB
Hello everybody,

I have some files in Dir_1, and I need to count the number of records (lines) stored in each file. Well, I'm trying to make use of this:

@echo off & setLocal EnableDELAYedeXpansion

dir /b /a-d Dir_1>files_to_count.txt
move files_to_count.txt Dir_2
set count=0
for /f %%a in (Dir_2\files_to_count.txt) do ( set /a count+=1)
if %count% EQU 0 (
echo No files found in the specified folder>>error.log
) else (
for /L %%b in (1 1 %count%) do goto :count_files %%b
echo !count! folders count.>> Dir_2\files_to_count.txt
move error.log Dir_2\checkFilesLoaded
@echo off & setLocal EnableDELAYedeXpansion
for /f "tokens=* delims= " %%t in ('dir /b /a-d Dir_1\*.dat') do (
for /f "tokens=* delims= " %%a in (%%t) do (
set /a N+=1
echo total is !N!>> Dir_2\checkFilesLoaded

But when i try to execute it, i get the following:
Acces denied..!
What is going wrong???

See More: Count no. of records(lines) in files

Report •

February 25, 2010 at 03:36:59
I guess you should make it obvious to the MOVE command that the target is either a directory or a file, by means of either using parameters, or either pre-creating the file or directory.

Remember that there is virtually no DOS command that assumes a name is a directory, when it lacks an extension, or that it is a file, when it has an extension. That assumption is only done by the programmer itself.

Report •

February 25, 2010 at 08:16:24
i think your batch is trying to read and write file at same time.
i would take a whold different approach. this might work:

@echo off & setlocal enabledelayedexpansion
pushd dir1
set prev=
set cc=0
for /f "tokens=1,2 delims=:" %%a in ('findstr /n /r "." *.*') do (
:echo A: %%a B: %%b
if "!prev!" neq "%%a" (
set qq=%%a
if "!prev!" neq "" call :bb
set cc=%%b

>> filelins.txt echo file: !prev! count: !cc!
set prev=%qq%
goto :eof

i'm pretty sure findstr will skip directories. Also, findstr WILL count blank lines, which i'm not sure you want (your script will not count them i don't think)

Report •

Related Solutions

Ask Question