Jump to content

Need help with Functions


Guest Cheeseman
 Share

Recommended Posts

Guest Cheeseman

Hello all,

I've been learning AutoIt3 for the last couple of weeks and have found it very useful and easy to understand. (I have almost no other scripting experience)

I have been working up several scripts to automate various computer maintence tasks - collecting info about the system, running Spyware scans, emptying the recycle bin, defraging, etc. Each script works great independently.

I then built a GUI and wanted to tie the scripts together into one "program" so that different options could be configured in the GUI.

Here's where I'm stuck. I can't find a way to easily pass variables and info between the different scripts. I experimented wiht command line parameters, but I don't think I could rewrite every script to use them for what I need. I also use a textfile to save some parameters but because of the long process of editing the logfile I'm using (read the text in the file, make it an array, use that array to define variables, update the variables in the seperate scripts, define the array with updated variables, rewrite the logfile and save it) it doesn't seem practical to use this logfile for every variable I needed.

Apologies for the long introduction, I'll get to my point eventually. After all this, I figured that combining all my scripts into one large script would fix alot of my problems. I could use global variables and all my problems would be solved. Each seperate script became a function in my GUI script and the #includes and RunWaits in each script were replaced with the function names . The problem... AutoIt is throwing all kinds of errors - Undefined global variables, Syntax errors, variables used before declaration.

Some examples. Heres two of the scripts ManageLog.au3 and AutoInfo.au3.

#include-once

#include <file.au3>
#include <Array.au3>

Dim $AutoMateActive = 0, $AutoMateInstalled = 0
Dim $LogFileArray
Dim $LogFileChanged = 0
If Not _FileReadToArray("C:\Documents and Settings\SpencerC\Desktop\AutoMate Scripts and Stuff\Logfile.txt",$LogFileArray) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $LogFileArray[0]
;Msgbox(0,'Record:' & $x, $LogFileArray[$x], 1)
Next

$AutoMateActive = $LogFileArray[1]; Changed in The GUI
$AutoMateInstalled = $LogFileArray[2]; Changed in the GUI on first run
$RestoreDate = $LogFileArray[3]; _NowCalcDate at beginning of AutoRestore.exe
$SysInfoDate = $LogFileArray[4]; _NowCalcDate at beginning of SysInfo.exe
$AutoScanDate = $LogFileArray[5]; _NowCalcDate at beginning of AutoScan.exe
$AutoKillDate = $LogFileArray[6]; _NowCalcDate at beginning of AutoKill.exe
$DefragMateDate =  $LogFileArray[7]; Date of Last DefragMate Running
$DriveNumber  =  $LogFileArray[8]; Includes All Drives - Edit with DriveGetDrive ("All")
$HardDriveNumber   = $LogFileArray[9]; DriveGetDrive "Fixed" [0]
;$HardDriveInfoArray [Drive Letter][% Free][]; DriveGetDrive
$ProcessNumber   = $LogFileArray[11]; ProcessList in Sysinfo
$MemoryAmt       = $LogFileArray[12]; From SysInfo 
$MemoryInUse       = $LogFileArray[13]; From SysInfo
$SpybotInstalled   = $LogFileArray[14]; From AutoScan after Installation 
$CleanUpInstalled  = $LogFileArray[15]; From AutoScan after Installation
$CleanUpSucess   = $LogFileArray[16]; From AutoScan After Running of CleanUp
$CWSSuccess     = $LogFileArray[17]; From AutoScan after running of CWS
$SpyBotSuccess  = $LogFileArray[18]; From AutoScan after running of Spybot
$IgnoreListExists  = $LogFileArray[19]; From AutoKill after Ignore List Created
;$IgnoreList Array
$ThreatsIdentified = $LogFileArray[21]; From AutoKill after each scan
$KillCount       = $LogFileArray[22]; At end of AutoKill
$RestorePointCount = $LogFileArray[23]; From AutoRestore - Counter

;_ArrayDisplay ($LogFileArray, "The Array")


While $LogFileChanged = 1 
$LogFileArray[1] = $AutoMateActive 
$LogFileArray[2] = $AutoMateInstalled
$LogFileArray[3] = $RestoreDate
$LogFileArray[4] = $SysInfoDate
$LogFileArray[5] = $AutoScanDate 
$LogFileArray[6] = $AutoKillDate 
$LogFileArray[7] = $DefragMateDate 
$LogFileArray[8] = $DriveNumber
$LogFileArray[9] = $HardDriveNumber
;$HardDriveInfoArray [Drive Letter][% Free][] 
$LogFileArray[11] = $ProcessNumber
$LogFileArray[12] = $MemoryAmt
$LogFileArray[13] = $MemoryInUse
$LogFileArray[14] = $SpybotInstalled
$LogFileArray[15] = $CleanUpInstalled
$LogFileArray[16] = $CleanUpSucess
$LogFileArray[17] = $CWSSuccess
$LogFileArray[18] = $SpyBotSuccess
$LogFileArray[19] = $IgnoreListExists
;$IgnoreList Array
$LogFileArray[21] = $ThreatsIdentified 
$LogFileArray[22] = $KillCount       
$LogFileArray[23] = $RestorePointCount 
_ArrayDisplay ($LogFileArray, "The Array")

Run ("Notepad.exe")
For $i = 1 to $LogFileArray[0]
    ControlSend ("Untitled - Notepad", "" , "Edit1" , $LogfileArray[$i])
    If @error = -1 Then ExitLoop
    Next

Wend

Here's AutoInfo.au3... Its not finished quite yet but it will automate all my other scripts.

#include-once

#include "ManageLog.au3"
#include <date.au3>


;Calculate Date. Figure out if its time to run Scans
$Date = _NowCalcDate ()

$LastInfo = _DateDiff ("D", $SysInfoDate, $Date)
$LastDefrag = _DateDiff ("D", $DefragMateDate, $Date)
$LastScan = _DateDiff ("D", $AutoScanDate, $Date)
$LastKill = _DateDiff ("D", $AutoKillDate, $Date)


If $LastInfo > 14 and $AutoMateActive = 1 Then
;#include "SysInfo Enhanced.au3"
    ElseIf $LastInfo > 14 Then
    MsgBox (1, "This would be in the GUI" , "Need to Collect New Info!")
    Else
EndIf
    
If $LastDefrag > 90 and $AutoMateActive = 1 Then
;#include "SysInfo Enhanced.au3"
    ElseIf $LastDefrag > 90 Then
    MsgBox (1, "This would be in the GUI" , "Need to Analyze Hard Drive!")
    Else
    EndIf
    
    If $LastScan > 14 and $AutoMateActive = 1 Then
;#include "SysInfo Enhanced.au3"
    ElseIf $LastScan > 14 Then
    MsgBox (1, "This would be in the GUI" , "Need to run Spyware Scans!")
    Else
EndIf
    
    If $LastKill > 14 and $AutoMateActive = 1 Then
;#include "SysInfo Enhanced.au3"
    ElseIf $LastKill > 14 Then
    MsgBox (1, "This would be in the GUI" , "Need to run AutoKill!")
    Else
EndIf

Now, in the big script.

Func ManageLog()

Dim $AutoMateActive = 0, $AutoMateInstalled = 0
Dim $LogFileArray
Dim $LogFileChanged = 0
If Not _FileReadToArray("C:\Documents and Settings\SpencerC\Desktop\AutoMate Scripts and Stuff\Logfile.txt",$LogFileArray) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $LogFileArray[0]
;Msgbox(0,'Record:' & $x, $LogFileArray[$x], 1)
Next

$AutoMateActive = $LogFileArray[1]; Changed in The GUI
$AutoMateInstalled = $LogFileArray[2]; Changed in the GUI on first run
$RestoreDate = $LogFileArray[3]; _NowCalcDate at beginning of AutoRestore.exe
$SysInfoDate = $LogFileArray[4]; _NowCalcDate at beginning of SysInfo.exe
$AutoScanDate = $LogFileArray[5]; _NowCalcDate at beginning of autoscan.exe
$AutoKillDate = $LogFileArray[6]; _NowCalcDate at beginning of AutoKill.exe
$DefragMateDate =  $LogFileArray[7]; Date of Last DefragMate Running
$DriveNumber  =  $LogFileArray[8]; Includes All Drives - Edit with DriveGetDrive ("All")
$HardDriveNumber   = $LogFileArray[9]; DriveGetDrive "Fixed" [0]
;$HardDriveInfoArray [Drive Letter][% Free][]; DriveGetDrive
$ProcessNumber   = $LogFileArray[11]; ProcessList in Sysinfo
$MemoryAmt       = $LogFileArray[12]; From SysInfo 
$MemoryInUse       = $LogFileArray[13]; From SysInfo
$SpybotInstalled   = $LogFileArray[14]; From AutoScan after Installation 
$CleanUpInstalled  = $LogFileArray[15]; From AutoScan after Installation
$CleanUpSucess   = $LogFileArray[16]; From AutoScan After Running of CleanUp
$CWSSuccess     = $LogFileArray[17]; From AutoScan after running of CWS
$SpyBotSuccess  = $LogFileArray[18]; From AutoScan after running of Spybot
$IgnoreListExists  = $LogFileArray[19]; From AutoKill after Ignore List Created
;$IgnoreList Array
$ThreatsIdentified = $LogFileArray[21]; From AutoKill after each scan
$KillCount       = $LogFileArray[22]; At end of AutoKill
$RestorePointCount = $LogFileArray[23]; From AutoRestore - Counter

;_ArrayDisplay ($LogFileArray, "The Array")

While $LogFileChanged = 1 
$LogFileArray[1] = $AutoMateActive 
$LogFileArray[2] = $AutoMateInstalled
$LogFileArray[3] = $RestoreDate
$LogFileArray[4] = $SysInfoDate
$LogFileArray[5] = $AutoScanDate 
$LogFileArray[6] = $AutoKillDate 
$LogFileArray[7] = $DefragMateDate 
$LogFileArray[8] = $DriveNumber
$LogFileArray[9] = $HardDriveNumber
;$HardDriveInfoArray [Drive Letter][% Free][] 
$LogFileArray[11] = $ProcessNumber
$LogFileArray[12] = $MemoryAmt
$LogFileArray[13] = $MemoryInUse
$LogFileArray[14] = $SpybotInstalled
$LogFileArray[15] = $CleanUpInstalled
$LogFileArray[16] = $CleanUpSucess
$LogFileArray[17] = $CWSSuccess
$LogFileArray[18] = $SpyBotSuccess
$LogFileArray[19] = $IgnoreListExists
;$IgnoreList Array
$LogFileArray[21] = $ThreatsIdentified 
$LogFileArray[22] = $KillCount       
$LogFileArray[23] = $RestorePointCount 
_ArrayDisplay ($LogFileArray, "The Array")

Run ("Notepad.exe")
For $i = 1 to $LogFileArray[0]
    ControlSend ("Untitled - Notepad", "" , "Edit1" , $LogfileArray[$i])
    If @error = -1 Then ExitLoop
    Next
Wend

EndFunc

; AutoInfo (AI) Script for AutoMate
; Date Aug 21, 2005
Func AutoInfo()

ManageLog ()

;Calculate Date. Figure out if its time to run Scans
$Date = _NowCalcDate ()

$LastInfo = _DateDiff ("D", $SysInfoDate, $Date)
$LastDefrag = _DateDiff ("D", $DefragMateDate, $Date)
$LastScan = _DateDiff ("D", $AutoScanDate, $Date)
$LastKill = _DateDiff ("D", $AutoKillDate, $Date)


If $LastInfo > 14 and $AutoMateActive = 1 Then
;#include "SysInfo Enhanced.au3"
    ElseIf $LastInfo > 14 Then
    MsgBox (1, "This would be in the GUI" , "Need to Collect New Info!")
    Else
EndIf
    
If $LastDefrag > 90 and $AutoMateActive = 1 Then
;#include "SysInfo Enhanced.au3"
    ElseIf $LastDefrag > 90 Then
    MsgBox (1, "This would be in the GUI" , "Need to Analyze Hard Drive!")
    Else
    EndIf
    
    If $LastScan > 14 and $AutoMateActive = 1 Then
;#include "SysInfo Enhanced.au3"
    ElseIf $LastScan > 14 Then
    MsgBox (1, "This would be in the GUI" , "Need to run Spyware Scans!")
    Else
EndIf
    
    If $LastKill > 14 and $AutoMateActive = 1 Then
;#include "SysInfo Enhanced.au3"
    ElseIf $LastKill > 14 Then
    MsgBox (1, "This would be in the GUI" , "Need to run AutoKill!")
    Else
EndIf

EndFunc

Executed from the two scripts - no errors.

Executed from the big script with functions throws this -

C:\Documents and Settings\SpencerC\Desktop\AutoMate Scripts and Stuff\AutoMate.au3(1222,41) : WARNING: $SysInfoDate: possibly used before declaration.

$LastInfo = _DateDiff ("D", $SysInfoDate,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Documents and Settings\SpencerC\Desktop\AutoMate Scripts and Stuff\AutoMate.au3(1223,46) : WARNING: $DefragMateDate: possibly used before declaration.

$LastDefrag = _DateDiff ("D", $DefragMateDate,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Documents and Settings\SpencerC\Desktop\AutoMate Scripts and Stuff\AutoMate.au3(1224,42) : WARNING: $AutoScanDate: possibly used before declaration.

$LastScan = _DateDiff ("D", $AutoScanDate,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Documents and Settings\SpencerC\Desktop\AutoMate Scripts and Stuff\AutoMate.au3(1225,42) : WARNING: $AutoKillDate: possibly used before declaration.

$LastKill = _DateDiff ("D", $AutoKillDate,

There are other errors and warnings, but I didn't want to post all 1000 lines of code. Maybe there is one fix for everything?

I've searched the forums and helpfiles and can't see a solution. Any help at all would be appreciated... if anyone actually read this far :whistle: .

Link to comment
Share on other sites

Okay I have to admit, I only read the introduction. I didnt check out the scripts. If you could maybe revert back to the way you had it before. Then what you would want to do is use something like INIRead() and INIWrite() to setup your 'text database' so as to store the information neatly and orderly. The values can then be read from any AutoIt script you deem necissary.

I hope this helps. Let me know if you need more information.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Guest Cheeseman

Thanks for the reply.... I think I've scared away most everyone else. Do you have an ini file or script that uses one that I can look at?

Summary of My First Post: I need the quickest/easiest way to pass a lot of information and update different variables between several different scripts.

Thanks.

Link to comment
Share on other sites

Thanks for the reply.... I think I've scared away most everyone else. Do you have an ini file or script that uses one that I can look at?

Summary of My First Post: I need the quickest/easiest way to pass a lot of information and update different variables between several different scripts.

Thanks.

<{POST_SNAPBACK}>

a few other options for you:

1) use the registry... the registry is a good place to put information to be used alot, or in different environments, because you don't need to worry about relative paths etc etc.

2)one approach to handling your project (which you may have implemented, i didn't read your code either) would be to turn all of your little scripts into UDF's and include them in the script that generates the GUI. then the values you want to pass can be stored in global variables at run time, and accessed by any of the included scripts. then you don't have to worry about extra I/O functions to send to registry/ini etc. and your code may come out looking a little more tidy...(and i don't mean to say it's not already immaculate, i just haven't looked at it to know if it needs to be cleaned up or anything)

Link to comment
Share on other sites

I need the quickest/easiest way to pass a lot of information and update different variables between several different scripts.

Assuming all of these scripts are running concurrently and need the updates on the fly, you could use AdlibEnable("checkINI", 60000) to check the timestamp on the INI file every minute and reload it into your settings array when it changes. All of your programs would automatically update themselves about a minute after you change the options in your main program. This could cause problems if they're all performing IniReads at exactly the same time.

If they only need to know what settings to use at the time of execution, forget the AdlibEnable thing.

Now for the reading and writing of INI files:

To check the timestamp, do your IniRead followed by this at the beginning of the script:

$iniTimestamp = FileGetTime($iniFilename, 0, 1)
$iniTimestamp = $iniTimestamp[0] & "/" & $iniTimestamp[1] & "/" & $iniTimestamp[2] & " " & $iniTimestamp[3] & ":" & $iniTimestamp[4] & ":" & $iniTimestamp[5]
$oldTimestamp = 0

Then have your Adlib function do this:

If _DateDiff("n", $oldTimestamp, $iniTimestamp) < 10 Then; Changed less than 10 minutes ago
   ...call your IniRead function to reload values...
   $oldTimestamp = $iniTimestamp
   $iniTimestamp = FileGetTime($iniFilename, 0, 1)
   $iniTimestamp = $iniTimestamp[0] & "/" & $iniTimestamp[1] & "/" & $iniTimestamp[2] & " " & $iniTimestamp[3] & ":" & $iniTimestamp[4] & ":" & $iniTimestamp[5]
EndIf

To create one (IniWrite will create the file if it doesn't exist, and add the section name if it doesn't exist):

IniWrite($iniFilename, "Collect Info", "Next Run", _DateAdd( "D", 1, _NowCalcDate()) & " 12:00:00")
IniWrite($iniFilename, "Defrag", "Next Run", _DateAdd( "D", 1, _NowCalcDate()) & " 1:00:00")

To read those values in the defrag script:

$timeNextRun = IniRead($iniFilename, "Defrag", "Next Run", -1)
If $timeNextRun = -1 Then
   $timeNextRun = _DateAdd("D", 1, _NowCalcDate() & " 1:00:00")
   IniWrite($iniFilename, "Defrag", "Next Run", $timeNextRun)
EndIf

The _DateAdd() thing is so that if the INI file doesn't exist or the "Next Run" value isn't set, it will create the INI and write the setting for tomorrow at 1:00 AM.

Then, in your timer, use:

If _DateDiff("n", $timeNextRun, _NowCalc()) < 5 Then
   ...Run defrag program...
EndIf

FYI: This might not be coherent as it took me over an hour to write because of distractions at work. When will these people learn what's important?! lol

Edited by c0deWorm

My UDFs: ExitCodes

Link to comment
Share on other sites

WARNING: $SysInfoDate: possibly used before declaration.

$LastInfo = _DateDiff ("D", $SysInfoDate,

Maybe I'm missing something, but when I added:

dim $SysInfoDate, $DefragMateDate, $AutoScanDate, $AutoKillDate, $AutoMateActive

to the top of your AutoInfo.au3 script, the errors went away.

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Guest Cheeseman

Maybe I'm missing something, but when I added:

dim $SysInfoDate, $DefragMateDate, $AutoScanDate, $AutoKillDate, $AutoMateActive

to the top of your AutoInfo.au3 script, the errors went away.

<{POST_SNAPBACK}>

Yes, that would fix it, but if my functions worked properly it shouldn't throw the error.

It doesn't matter one or another I have fixed the problem using a .ini file. Thanks for everyone's help!

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...