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.
FileAppend string to txt and sum total numbers Topic is solved
Re: FileAppend string to txt and sum total numbers
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:
Then you can do with the var variable whatever you wish.
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
Re: FileAppend string to txt and sum total numbers Topic is solved
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
;===========================================
Re: FileAppend string to txt and sum total numbers
thanks! this is exactly what i want to do, but the sum result is always to be zero?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:
Then you can do with the var variable whatever you wish.Code: Select all
Loop, Read, D:\sum.txt var+=A_LoopLine MsgBox %var% return
Re: FileAppend string to txt and sum total numbers
According to garry's answer, the code you wrote about got a mistake: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:
Then you can do with the var variable whatever you wish.Code: Select all
Loop, Read, D:\sum.txt var+=A_LoopLine MsgBox %var% return
Loop, Read, D:\sum.txt
var+=A_LoopReadLine ;after change A_LoopLine to A_LoopReadLine, it works
MsgBox %var%
return
Re: FileAppend string to txt and sum total numbers
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
Re: FileAppend string to txt and sum total numbers
thank you Exaskryz , I used your script , first I used fileread / parse , your code was shorter ...
( forget to mention the small failure ( typo ) )
( forget to mention the small failure ( typo ) )
Who is online
Users browsing this forum: Google [Bot], peter_ahk and 304 guests