Dazza said:
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