Gordo54 Posted February 5, 2007 Share Posted February 5, 2007 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! Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 5, 2007 Moderators Share Posted February 5, 2007 (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 February 5, 2007 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. Link to comment Share on other sites More sharing options...
BrettF Posted February 5, 2007 Share Posted February 5, 2007 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 Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Gordo54 Posted February 5, 2007 Author Share Posted February 5, 2007 Awesome - thank you both! Link to comment Share on other sites More sharing options...
BrettF Posted February 5, 2007 Share Posted February 5, 2007 Awesome - thank you both!Thats ok SmokeN's answer is probably your best bet. Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
MrCreatoR Posted February 5, 2007 Share Posted February 5, 2007 (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 :$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 February 5, 2007 by MsCreatoR Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: 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 ProgramUDFs: 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 Examples: 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 ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 5, 2007 Moderators Share Posted February 5, 2007 (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 : $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Ý÷ Ûú®¢×Ç}ú+¶Úò¶¬(!µ·µ§_jË^«¢+Ø)Õ¹}¥± ¡É ½Õ¹Ð ÀÌØí¡¥±°ÀÌØí¥±ôÀ¤(%%ÀÌØí¥±ôÀ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 February 5, 2007 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now