"Dazza" <darren@taylorhouse.co.uk> wrote in message
news:791e4f5b-4e5f-4a2d-bb45-2b88dd14bf00@y17g2000yqd.googlegroups.com...
> On 23 Mar, 21:50, "Pegasus [MVP]" <n...@microsoft.com> wrote:
>> "John John - MVP" <audetw...@nbnet.nb.ca> wrote in
>> messagenews:uFaKyzoyKHA.244@TK2MSFTNGP06.phx.gbl...
>>
>>
>>
>>
>>
>> > Dazza wrote:
>> >> On 23 Mar, 13:07, Dazza <dar...@taylorhouse.co.uk> wrote:
>> >>> On 23 Mar, 12:49, "Pegasus [MVP]" <n...@microsoft.com> wrote:
>>
>> >>>> "Dazza" <dar...@taylorhouse.co.uk> wrote in message
>> >>>>news:c622bf0e-e7b3-489f-b204-74c0e73071e0@15g2000yqi.googlegroups.com...
>> >>>>> Hi Guys
>> >>>>> Its one of those days when you say yes that can be done. Only to
>> >>>>> find
>> >>>>> out you start wishing you had said no!
>> >>>>> I need to inspect each .txt file in each subfolder and count the
>> >>>>> number of commas are in it and then list them
>> >>>>> i know i am missing something and must be my age
>> >>>>> @echo off
>> >>>>> setlocal enabledelayedexpansion
>> >>>>> for /F "usebackq" %%a IN ('*.txt') Do find /c /i "," %%a >>c:
>> >>>>> \commareport.txt
>> >>>>> This works fine for the folder is is in but i need to do all
>> >>>>> subfolders
>> >>>>> I have tried calling this bat from another bat but with no results.
>> >>>>> Can anyone give me a hand,
>> >>>>> Cheers
>> >>>>> Darren
>> >>>> Here you go:
>> >>>> @echo off
>> >>>> for /F %%a in ('dir *.txt /b /s') do (
>> >>>> for /F %%c in ('type "%%a" ^| find /c ","') do echo %%c - %%a
>> >>>> )- Hide quoted text -
>> >>>> - Show quoted text -
>> >>> Many Thanks for the quick reply, the only problem i have now is that
>> >>> the are spaces in the folder names, legacy dont you love it....
>>
>> >>> Darren- Hide quoted text -
>>
>> >>> - Show quoted text -
>>
>> >> Any Body got any ideas?????
>>
>> > Hang tight... you're being helped by one of the best and he won't
>> > abandon
>> > you halfway through the job. His last post was less than 27 minutes
>> > ago,
>> > he is active in several groups, give him time to come back and read
>> > your
>> > other post!
>>
>> To John: Thanks for the compliment, which is not really deserved. And
>> yes, I
>> sometimes do have some other commitments.
>>
>> To the OP: Sorry for the oversight. Here you go again:
>> @echo off
>> for /F "delims=" %%a in ('dir *.txt /b /s') do (
>> for /F %%c in ('type "%%a" ^| find /c ","') do echo %%c - %%a
>> )- Hide quoted text -
>>
>> - Show quoted text -
>
> Hi Thanks for the quick update.
>
> Sorry I did not get to you but i have been on a few days vacation.
>
> This now works a treat, but like all requests they change the goal
> posts.
>
> I have now been asked to add the number of lines in each text file
> also
>
> I am on the catch up with being off
>
> Regards
> Darren
Using a batch file to count the number of lines in a text file is
notoriously hard because batch files have the unfortunate habit of ignoring
blank lines when scanning text files. There is also the perennial issue of
poison characters such as ()"`& that have a tendency to make the batch file
unstable. The solution below uses a hybrid batch/VBS solution and is far
more robust than a pure batch file. Since your specification is rather
vague, you will probably have to tune it to deliver the results you expect.
Or, in other words, you must nail down your requirements *before* you start
coding.
@echo off
set sFolder=d
User Data
set Scr="%temp%\TempVBS.vbs"
set VB=echo^>^>%Scr%
cd 1>nul 2>%Scr%
%VB% '------------------------------------------
%VB% 'Recursively count the number of .txt files
%VB% 'and report the number of lines and commas
%VB% '29.3.2010 FNL
%VB% '------------------------------------------
%VB% Set oFSO = CreateObject("Scripting.FileSystemObject")
%VB% Set oFolder = oFSO.GetFolder("%sFolder%")
%VB% iFileCount = 0
%VB% iLineCount = 0
%VB% iCommaCount = 0
%VB% ProcessFolder(oFolder)
%VB% WScript.Echo iFileCount, iLineCount, iCommaCount
%VB% '----------------------------
%VB% 'Process a folder recursively
%VB% '----------------------------
%VB% Sub ProcessFolder(oFldr)
%VB% For Each oFile In oFldr.Files
%VB% If lcase(Right(oFile.Name, 4)) = ".txt" then Count oFile
%VB% Next
%VB% For Each oSubFolder In oFldr.Subfolders
%VB% ProcessFolder oSubFolder
%VB% Next
%VB% End Sub
%VB% '---------------------
%VB% 'Process a single file
%VB% '---------------------
%VB% Sub Count(oFile)
%VB% WScript.Echo "Processing", oFile.path
%VB% iFileCount = iFileCount + 1
%VB% if oFile.size = 0 then Exit Sub
%VB% Set oData = oFSO.OpenTextFile(oFile.path)
%VB% sString = oData.ReadAll
%VB% For i = 1 To Len(sString)
%VB% char = Mid(sString, i, 1)
%VB% if char = "," Then iCommaCount = iCommaCount + 1
%VB% If char = vbLf then iLineCount = iLineCount + 1
%VB% Next
%VB% oData.close
%VB% End Sub
for /F "tokens=1-10" %%a in ('cscript //nologo %Scr%') do set Files=%%a& set
Lines=%%b& set Commas=%%c
del %Scr%
echo %Files% files, %Lines% lines, %Commas% commas