Sign in to follow this  
Followers 0
Gordo54

Count characters in a text file

7 posts in this topic

I have looked through the library and done multiple searches, so forgive me if this has been asked a hundred times before - I just couldn't find a solution.

How can I open a text file and return the number of characters that are in it?

Thanks in advance!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

MsgBox(64, 'Info', 'There are: ' & StringLen(FileRead('FileNameLocation')) & ' characters in this file, including Carriage Returns and Line Feeds')oÝ÷ ØGb´ l¡«­¢+Ù5Í ½à ØаÌäí%¹¼Ìäì°ÌäíQ¡ÉÉèÌäìµÀ쥱ÑM¥é Ìäí¥±9µ1½Ñ¥½¸Ìä줵ÀìÌäì¡ÉÑÉÌ¥¸Ñ¡¥Ì¥±°¥¹±Õ¥¹
ÉÉ¥IÑÕɹ̹1¥¹ÌÌäì¤

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Try this:

$file = FileOpen("test.txt", 0)
$charcount = 0 

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

Do 
    $chars = FileRead($file, 1)
    $charcount += 1
Until @error = -1
MsgBox (0, "Characters", $charcount)
FileClose($file)
Exit

Its a edit on fileread in the helpfile

Share this post


Link to post
Share on other sites

Awesome - thank you both!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I was write function, that help for me in that problem...

If $Flag is set to 0 (Default), then returned count of all characters including carriage returns and line feeds (@CR and @LF), if it set to 1, then the count will not include @CR and @LF, if the flag is set to 2 then returned all characters except the @CR, and if you set it to 3, then returned count will not include @LF :whistle: :

$FileName = @ScriptDir & "\Test.txt"
MsgBox(64, "File Characters Count", "In the file <" & $FileName & "> was found [" & _FileCharCount($FileName, 0) & "] characters (including Carriage Returns and Line Feeds)")

Func _FileCharCount($FileName, $Flag=0)
    Local $CharsCount = 0
    $fRead = FileRead($FileName, FileGetSize($FileName))
    $fReadArr = StringSplit($fRead, "")
    If IsArray($fReadArr) Then
        For $i = 1 To $fReadArr[0]
            If ($Flag = 1 And $fReadArr[$i] <> @CR And $fReadArr[$i] <> @LF) Or ($Flag = 2 And $fReadArr[$i] <> @CR) Or ($Flag = 3 And $fReadArr[$i] <> @LF) Or $Flag = 0 Then $CharsCount += 1
        Next
    EndIf
    Return $CharsCount
EndFunc
Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I was write function, that help for me in that problem...

If $Flag is set to 0 (Default), then returned count of all characters including carriage returns and line feeds (@CR and @LF), if it set to 1, then the count will not include @CR and @LF, if the flag is set to 2 then returned all characters except the @CR, and if you set it to 3, then returned count will not include @LF :whistle: :

$FileName = @ScriptDir & "\Test.txt"
MsgBox(64, "File Characters Count", "In the file <" & $FileName & "> was found [" & _FileCharCount($FileName, 0) & "] characters (including Carriage Returns and Line Feeds)")

Func _FileCharCount($FileName, $Flag=0)
    Local $CharsCount = 0
    $fRead = FileRead($FileName, FileGetSize($FileName))
    $fReadArr = StringSplit($fRead, "")
    If IsArray($fReadArr) Then
        For $i = 1 To $fReadArr[0]
            If ($Flag = 1 And $fReadArr[$i] <> @CR And $fReadArr[$i] <> @LF) Or ($Flag = 2 And $fReadArr[$i] <> @CR) Or ($Flag = 3 And $fReadArr[$i] <> @LF) Or $Flag = 0 Then $CharsCount += 1
        Next
    EndIf
    Return $CharsCount
EndFuncoÝ÷ Ûú®¢×Ç}ú+¶Úò¶¬(!µ·µ§_^­«­¢+Ø)Õ¹}¥±
¡É
½Õ¹Ð ÀÌØí¡¥±°ÀÌØí¥±ôÀ¤(%%ÀÌØí¥±ôÀQ¡¸IÑÕɸ¥±ÑM¥é ÀÌØí¡¥±¤(%1½°ÀÌØíÍMÑÉ¥¹ô¥±I ÀÌØí¡¥±¤(%%ÀÌØí¥±ôÄQ¡¸IÑÕɸMÑÉ¥¹1¸¡MÑÉ¥¹IÁ± ÀÌØíÍMÑÉ¥¹°
I1°ÌäìÌä줤(%%ÀÌØí¥±ôÈQ¡¸IÑÕɸMÑÉ¥¹1¸¡MÑÉ¥¹MÑÉ¥Á
H ÀÌØíÍMÑÉ¥¹¤¤(%%ÀÌØí¥±ôÌQ¡¸IÑÕɸMÑÉ¥¹1¸¡MÑÉ¥¹IÁ± ÀÌØíÍMÑÉ¥¹°1°ÌäìÌä줤(%IÑÕɸMÑÉÉ½È Ä°À°ÌäìÌäì¤)¹Õ¹oÝ÷ ØGb´:'ßÛky©eÊwtºj]7êÄ­®)àjëh×6Func _FileCharCount($hFile, $iFlag = 0)
    If $iFlag = 0 Then Return FileGetSize($hFile)
    If $iFlag = 1 Then Return StringLen(StringReplace(FileRead($hFile), @CRLF, ''))
    If $iFlag = 2 Then Return StringLen(StringStripCR(FileRead($hFile)))
    If $iFlag = 3 Then Return StringLen(StringReplace(FileRead($hFile), @LF, ''))
    Return SetError(1, 0, '')
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0