Jump to content

Variables


Recommended Posts

My boss gave me some tasks because I told him I'm interested in scripting.

But I've gotten to a part where I need variables, and I have absolutely no clue what to do..

This script is supposed to:

1: Create a folder (Survey) and to text files (Man.txt, and Woman.txt)

2: Give you two choices ( Man or Woman )

3: Count the number of times each button is pressed by writing "#" in either Man.txt or Woman.txt

Now the part I can't do:

4: Count with numbers instead of "#".

So when "Man" is pressed one time it will write "1". When it's pressed another time, it will write "2" etc...

This is what I have so far, and I hope you guys can help me!

;Creates a new folder
DirCreate ("C:\Survey")
;Creates a textfile unless it already exists
If Not FileExists ("C:\Survey\Man.txt") Then
FileWrite ("C:\Survey\Man.txt", "Number of men:") 
FileWriteLine ("C:\Survey\Man.txt", " ")
EndIf
If Not FileExists ("C:\Survey\Woman.txt") Then
FileWrite ("C:\Survey\Woman.txt", "Number of women:") 
FileWriteLine ("C:\Survey\Woman.txt", " ")
EndIf
;Designing the buttons and "Label"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Survey", 615, 438, 192, 124)
$Man = GUICtrlCreateButton("Man", 224, 184, 75, 25)
$Woman = GUICtrlCreateButton("Woman", 352, 184, 75, 25)
$Label1 = GUICtrlCreateLabel("Are you a man or a woman?", 248, 80, 140, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;Desides what will happen when you press a button
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
;Decides what will happen when you press "Man"
        Case $Man 
            MsgBox (0, "Man", "You are a man")
            FileWriteLine ("C:\Survey\Man.txt", "#")
            WinClose ("Survey")
;Decides what will happen when you press "Woman"    
    Case $Woman
            MsgBox (0, "Woman", "You are a woman")
            FileWriteLine ("C:\Survey\Woman.txt", "#")
            WinClose ("Survey")
    EndSwitch
WEnd
Link to comment
Share on other sites

;Creates a new folder
DirCreate("C:\Survey")
;Creates a textfile unless it already exists
If Not FileExists("C:\Survey\Man.txt") Then
    FileWrite("C:\Survey\Man.txt", "Number of men:")
    FileWriteLine("C:\Survey\Man.txt", " ")
EndIf
If Not FileExists("C:\Survey\Woman.txt") Then
    FileWrite("C:\Survey\Woman.txt", "Number of women:")
    FileWriteLine("C:\Survey\Woman.txt", " ")
EndIf
;Designing the buttons and "Label"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$iMan = 0 
$iWoman = 0

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Survey", 615, 438, 192, 124)
$Man = GUICtrlCreateButton("Man", 224, 184, 75, 25)
$Woman = GUICtrlCreateButton("Woman", 352, 184, 75, 25)
$Label1 = GUICtrlCreateLabel("Are you a man or a woman?", 248, 80, 140, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;Desides what will happen when you press a button
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            ;Decides what will happen when you press "Man"
        Case $Man
            $iMan += 1 ; increase this var by 1
            MsgBox(0, "Man", "You are a man")
            FileWrite("C:\Survey\Man.txt", $iMan)
            WinClose("Survey")
            ;Decides what will happen when you press "Woman"
        Case $Woman
            $iWoman += 1 ; increase this var by 1
            MsgBox(0, "Woman", "You are a woman")
            FileWrite("C:\Survey\Woman.txt", $iWoman)
            WinClose("Survey")
    EndSwitch
WEnd

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Thank you JohnOne! :)

But when the script closes, it kind of forgets everything.

So if I press the button one time, and start it again and press the same button again, the result is 11.

If i spam the button, the result is 12345 etc..

Does someone know how to fix this?

I've tried a few changes, but I can't get it right.

Link to comment
Share on other sites

#include <file.au3>

Dim $aMen, $aWomen

_FileReadToArray("C:\Survey\Man.txt", $aMen)
$iMen = $aMen[$aMen[0]]

_FileReadToArray("C:\Survey\Woman.txt", $aWomen)
$iWoman = $aWomen[$aWomen[0]]

Anyway lookup FileRead in the helpfile. :)

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

To continue with the data you have you need to first read it from the file, then write the updated value back to the file, replacing the original content.

By modifying the original script I'd come up with something like this:

;Designing the buttons and "Label"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

;Creates a new folder
DirCreate("C:\Survey")

;If a textfile exists, read it and convert the contents to a number. (AutoIt will treat it as a number if you try to use any math on it, but I like to do it this way)
If FileExists("C:\Survey\Man.txt") Then
    $iMan = Number(FileRead("C:\Survey\Man.txt"))
Else
    $iMan = 0
EndIf
If FileExists("C:\Survey\Woman.txt") Then
    $iWoman = Number(FileRead("C:\Survey\Woman.txt"))
Else
    $iWoman = 0
EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Survey", 615, 438, 192, 124)
$Man = GUICtrlCreateButton("Man", 224, 184, 75, 25)
$Woman = GUICtrlCreateButton("Woman", 352, 184, 75, 25)
$Label1 = GUICtrlCreateLabel("Are you a man or a woman?", 248, 80, 140, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;Desides what will happen when you press a button
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            ;Decides what will happen when you press "Man"
        Case $Man
            $iMan += 1 ; increase this var by 1
            MsgBox(0, "Man", "You are a man")
            $hFileMan = FileOpen ("C:\Survey\Man.txt", 2)
            FileWrite($hFileMan, $iMan)
            FileClose($hFileMan)
            WinClose("Survey")
            ;Decides what will happen when you press "Woman"
        Case $Woman
            $iWoman += 1 ; increase this var by 1
            MsgBox(0, "Woman", "You are a woman")
            $hFileWoman = FileOpen ("C:\Survey\Woman.txt", 2)
            FileWrite($hFileWoman, $iWoman)
            FileClose($hFilewoman)
            WinClose("Survey")
    EndSwitch
WEnd

I took the liberty of removing the extra content from the file, so the file would only ever contain the number. You could also use the sugesstion of reading the contents to an array as posted above, which allows you to store additional data in the files, but it's a pretty fragile system this way. Any manual changes to the file are likely to mess things up.

A more reliable and probably easier way to store data like this, is by using an ini file. This has the advantage of being able to store lots of different data in one file in a way that is much less likely to fail when you make adjustments to the file, or script:

;Designing the buttons and "Label"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

;Creates a new folder
DirCreate("C:\Survey")

;This will read the data for the proper keys if they exist, and otherwise use the value 0.
$iMan = Number(IniRead("C:\Survey\Data.ini","Sex","Man",0))
$iWoman = Number(IniRead("C:\Survey\Data.ini","Sex","Man",0))

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Survey", 615, 438, 192, 124)
$Man = GUICtrlCreateButton("Man", 224, 184, 75, 25)
$Woman = GUICtrlCreateButton("Woman", 352, 184, 75, 25)
$Label1 = GUICtrlCreateLabel("Are you a man or a woman?", 248, 80, 140, 49)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;Desides what will happen when you press a button
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            ;Decides what will happen when you press "Man"
        Case $Man
            $iMan += 1 ; increase this var by 1
            MsgBox(0, "Man", "You are a man")
            ;Write the data to the key. Creating the file and key if needed.
            IniWrite("C:\Survey\Data.ini","Sex","Man",$iMan) 
            WinClose("Survey")
            ;Decides what will happen when you press "Woman"
        Case $Woman
            $iWoman += 1 ; increase this var by 1
            MsgBox(0, "Woman", "You are a woman")
            $hFileWoman = FileOpen ("C:\Survey\Woman.txt", 2)
            IniWrite("C:\Survey\Data.ini","Sex","Woman",$iMan)
    EndSwitch
WEnd
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...