FileAppend string to txt and sum total numbers Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

FileAppend string to txt and sum total numbers

03 Dec 2016, 09:57

I will explain my purpose in directly:

sum.ahk - first run

Clipwait
MyVar1 := Clipboard ;a number, for example 5000
Fileappend, %MyVar1%, D:\sum.txt

sum.ahk - second run

Clipwait
MyVar1 := Clipboard ;a number, for example 1000
Fileappend, %MyVar1%, D:\sum.txt ;how can i use fileread and controlsend {Enter} for newline and paste %MyVar1%

... ...so on

now i got a txt file, the content will like this:

5000
1000
2000
3000
4000

how can i sum the numbers from this txt file and save the result (15000)? save it as a new %Var% or Fileappend it a new txt?

sorry i'm still a newbie, thank you all.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: FileAppend string to txt and sum total numbers

03 Dec 2016, 11:12

Instead of doing a FileRead or ControlSend to get the newline in there, you can just put it in the text to append. FileAppend, %MyVar1%`n, D:\sum.txt.

To get the sum, you can use Loop, Read to run through each line:

Code: Select all

Loop, Read, D:\sum.txt
    var+=A_LoopLine
MsgBox %var%
return
Then you can do with the var variable whatever you wish.
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: FileAppend string to txt and sum total numbers  Topic is solved

03 Dec 2016, 11:27

example if you want copy numbers

Code: Select all

;- mark text and
;   F7= save number
;   F8= show result
#warn
#NoEnv
SendMode, Input
SetWorkingDir, %A_ScriptDir%
SetTitleMatchMode 2
SetBatchLines, -1
F1=%a_scriptdir%\sum.txt
var=0

;- save marked text
$F7::
clipboard:=""
sendinput, ^c{right}
clipwait,2
msgbox, 262208,You copied : ,%clipboard%,1
Fileappend, %clipboard%`r`n,%f1%
clipboard=
return


$F8::
Loop, Read,%f1%
    var+=A_LoopReadLine
msgbox, 262208,Result,Result is=`n%var%
return
;===========================================
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: FileAppend string to txt and sum total numbers

04 Dec 2016, 01:42

Exaskryz wrote:Instead of doing a FileRead or ControlSend to get the newline in there, you can just put it in the text to append. FileAppend, %MyVar1%`n, D:\sum.txt.

To get the sum, you can use Loop, Read to run through each line:

Code: Select all

Loop, Read, D:\sum.txt
    var+=A_LoopLine
MsgBox %var%
return
Then you can do with the var variable whatever you wish.
thanks! this is exactly what i want to do, but the sum result is always to be zero?
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: FileAppend string to txt and sum total numbers

04 Dec 2016, 01:49

Exaskryz wrote:Instead of doing a FileRead or ControlSend to get the newline in there, you can just put it in the text to append. FileAppend, %MyVar1%`n, D:\sum.txt.

To get the sum, you can use Loop, Read to run through each line:

Code: Select all

Loop, Read, D:\sum.txt
    var+=A_LoopLine
MsgBox %var%
return
Then you can do with the var variable whatever you wish.
According to garry's answer, the code you wrote about got a mistake:

Loop, Read, D:\sum.txt
var+=A_LoopReadLine ;after change A_LoopLine to A_LoopReadLine, it works :thumbup:
MsgBox %var%
return
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: FileAppend string to txt and sum total numbers

04 Dec 2016, 02:33

Oh yep, my apologies. It was untested code, and while I meant to double check I had the variable right per the documentation, I forgot to do that part. (I initially thought I wanted a Loop, Parse, but then figured out I wanted a Loop, Read. I was satisfied with checking the documentation to fix one mistake and forgot to check the other ^^). Glad you were able to figure it out :thumbup:
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: FileAppend string to txt and sum total numbers

04 Dec 2016, 04:45

thank you Exaskryz , I used your script , first I used fileread / parse , your code was shorter ... :)
( forget to mention the small failure ( typo ) )

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Mateusz53, MrDoge, peter_ahk and 346 guests