Script to minus 1 from a txt file.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
68martin68
Posts: 2
Joined: 15 Jul 2017, 04:13

Script to minus 1 from a txt file.

15 Jul 2018, 10:03

Hi,

I am having an issue with the below code. The idea is that when the script runs, it reads the file and uses its contents as the variable (it is a number between 1 and 800), then minuses said number by 1, before deleting the old file and creating a new file with the original number, minus 1.

The problem is that even though I am deleting the old file, when AHK writes the new file, it is adding the variable minus one to the original number, i.e 766 becomes 766765, rather than just overwriting 766 with 765.

Can anyone point me in the right direction? Or suggest a better/more reliable was of doing this?

Thanks

Martin

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#Persistent
#SingleInstance ignore

FileRead, Count, CountedPrints.txt

EnvSub, Count, 1

FileDelete, CountedPrints.txt

Sleep, 2000

FileAppend, %Count%, CountedPrints.txt

ExitApp
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: Script to minus 1 from a txt file.

15 Jul 2018, 10:59

you can use GuiControl or ini-file instead always delete a file
example with filedelete

Code: Select all

f1=%a_scriptdir%\CountedPrints.txt
ifnotexist,%f1%
   fileappend,800`r`n,%f1%
FileReadLine, A, %f1%, 1
c:=(a-1)
FileDelete,%f1%
sleep,2000
FileAppend, %c%`r`n,%f1%
run,%f1%
ExitApp
Guest

Re: Script to minus 1 from a txt file.

16 Jul 2018, 06:08

Thanks for your reply Garry, however I am still experiencing issues even with this script.

Sometime the script will launch, but the file will not be deleted and when the script runs file open the number within the text file has not changed. Also if the script runs while the text file is open , it will add the new number below the old i.e.

734
733

Many Thanks

Martin

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ReyAHK, Spawnova and 281 guests