Jump to content

Autoit total numbers inside notepad file (solved)


Recommended Posts

any idea to make an autoit script auto total the numbers inside notepad file and show the result in msgbox?

like' 133+34+55+41+639.3 = msgbox the total amount.

 

 

below is notepad user.txt

-------------------------------------------------------

[user]

133

34

55

41

639.3

 

Input=0

--------------------------------------------------------

Edited by KANlFUSA
Link to comment
Share on other sites

it is not the best way but should work.

#include <Array.au3>
Local $sData=fileread("user.txt")
$sData=StringMid($sData,1,StringInStr($sData,"Input")) ;just for avoid last number (0) no needed if is 0
Local $aData=StringRegExp($sData,"[-+]?[0-9]*\.?[0-9]+",3)

;~ _ArrayDisplay($aData) array result of numbers

Local $sResult=""
For $i= 0 to UBound($aData)-1
    $sResult&= (($i <> 0) ? "+" : "")  & $aData[$i]

Next

MsgBox(0,"",$sResult & " = " & Execute($sResult))

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

here is another way without using regex...you decide which is better for you

$Handle = FileOpen("user.txt",0)
$Read = FileRead($Handle)
$Counter = 0
$Sum = 0
$Exit = True
$Readline = ""
while $Exit 
$Counter += 1
$ReadLine = FileReadLine("user.txt",$Counter)
if @error <> 0 Then
    $Exit = False
    EndIf
$Sum += $ReadLine
WEnd

MsgBox("","",$Sum)

 

Edited by CrypticKiwi
Edit for a better way to detect end of File
Link to comment
Share on other sites

probably edge cases aplenty, but shorter:

#include <Array.au3>
Local $sData=FileRead("user.txt")
Local $aData = StringRegExp($sData , "\n(\d+\.*\d*)" , 3)
msgbox(0, '' , execute(_ArrayToString($aData , "+")))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

This is much better in that it adds the first line and allows for various line terminations, but we might as well allow signed values:

MsgBox(0, 'Results', Execute(StringRegExpReplace($sData, "([+-]?\d+\.?\d*)\R*", "\1+") & '0'))

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@kanifusa if you want read the numbers directly from the running notepad rather than from a file you can use this snippet

if WinExists("[CLASS:Notepad]", "") then $sData = WinGetText(WinGetHandle("[CLASS:Notepad]", ""))

MsgBox(0, 'Results', Execute(StringRegExpReplace($sData, "([+-]?\d+\.?\d*)\R*", "\1+") & '0')) ; <- this by jchd

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
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
 Share

  • Recently Browsing   0 members

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