Jump to content

Recommended Posts

Posted

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!

  • Moderators
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.

Posted

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

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

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • Moderators
Posted (edited)

  MsCreatoR said:
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.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...