Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scan files to give character count

Status
Not open for further replies.

hamish75

Technical User
Dec 18, 2009
39
GB
Does anyone know of an example which would allow me to create a small vbs script to scan a folder to check all the text files and then give me a character count?

I have been searching the internet for a long time but can't find anything.

Thanks.

Ah dinnae ken...
 

To find all text file in a folder, use Dir, you have a lot of samples on this Forum.

To check the number of characters in a text file you may just:
Code:
Open "C:\TextFile.txt" For Input As #1
strInputData = Input$(LOF(1), 1)
Close #1

[blue]MsgBox Len(strInputData)[/blue]

Have fun.

---- Andy
 
You might consider using the DirReaderObject whether you are using VB5 or VB6... or VBScript (note: you are in the wrong forum).

This DLL must be installed and registered on any system where your script will run. It is not part of Windows or Windows Scripting.

You can retrieve the files using a pattern like "*.txt" to eliminate some of the bulk. The result returned is an ADO Recordset that you can easily traverse to sum the file sizes. The DRO can return "huge file" sizes (>4GB) unlike some other approaches too.

It should be much faster than trying to use the FSO for this purpose.
 
>strInputData = Input$(LOF(1), 1)
>MsgBox Len(strInputData)

I wonder why don't you simply return LOF(1)?
The easiest way is to use the FileLen function. It does not even require you to Open/Close a file.

Note that none of these methods works in VBScript. To access file functions from a vbs file, you need some external library, like FileSystemObject, or the one advised by dilettante.
 
Hypetia suggestion using FileSystemObject.

Make a reference to Microsoft Scripting RunTime

Code:
Private Sub Command1_Click()

    Dim FSO, FileName, TextStream
    Dim strText As String
    Dim lngCount As Long
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Set FileName = FSO.GetFile("C:\Test.txt")
    
    Set TextStream = FileName.OpenAsTextStream(ForReading, _
          TristateUseDefault)
          
    Do While Not TextStream.AtEndOfStream
        strText = TextStream.ReadLine
        lngCount = lngCount + Len(strText)
    Loop
    
    MsgBox lngCount
    
    TextStream.Close
    Set TextStream = Nothing

End Sub
 
>I wonder why don't you simply return LOF(1)?

Because that would simply return the byte count, not the character count. In these Unicode days, these two values are not necessarily the same (even if the main body is pure ANSI text it is possible, although unlikely, that there might be a BOM at the start).

The FileSystemObject understands BOMs and Unicode (at least to a certain extent), and so can give a more accurate character count than LOF (or the DirReaderObject).

of course, if the sour file can be guaranteed to be ANSI with no BOM, then LOF works fine - in VB. Unfortunately the OP wants this to work in VBScript, which does not support LOF
 
Thanks for all the advice guys.

Much appreciated.

Ah dinnae ken...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top