AutoIT


Add link to Windows startup folder to auto-start an AutoIT application

posted Feb 25, 2014, 1:33 PM by Chris G   [ updated Mar 20, 2014, 11:23 AM ]

AutoIT Code Sample - Adding a shortcut in the user's Windows Startup folder in order to auto-start an AutoIT application every time a user logs-on to Windows. This script assumes that there is a checkbox defined on your Form, and will dynamically create or delete the shortcut on the check or uncheck event.

;===============================================================================
; Description:		Add link to Windows startup folder
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================
...
While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $Button1
			;Do something
		Case $Checkbox1
			If BitAND(GUICtrlRead($Checkbox1), $BN_CLICKED) = $BN_CLICKED Then
				If _GUICtrlButton_GetCheck($Checkbox1) Then
					ConsoleWrite("Checkbox checked... " & @CRLF)
					If Not FileExists(@StartupDir & "\linkName.lnk") Then
						FileCreateShortcut(@ScriptFullPath, @StartupDir & "\linkName.lnk", @ScriptDir, "", "AutoIT Script Description")
EndIf Else ConsoleWrite("Checkbox unchecked... " & @CRLF) If FileExists(@StartupDir & "\linkName.lnk") Then
FileDelete(@StartupDir & "\linkName.lnk")
EndIf EndIf EndIf EndSwitch WEnd
You may also want to check for the presence of the shortcut when starting the application in order to properly display the state of the checkbox to the user.
...
If FileExists(@StartupDir & "\linkName.lnk") Then
	 _GUICtrlButton_SetCheck($Checkbox1)
EndIf

HTTP Binary Image File Upload with Autoit

posted Feb 25, 2014, 1:33 PM by Chris G   [ updated Mar 20, 2014, 11:29 AM ]

AutoIT Code Sample - Using an Http POST request to upload any file (JPEG/JPG/PNG/PDF/etc.) to a web service.

;===============================================================================
; Description:		HTTP Binary Image File Upload
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================

$sFilePath = @ScriptDir & '\myImage.jpeg' 
Local $sFile = FileOpen($sFilePath, 16)
If $sFile = -1 Then
	MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
EndIf
$sFileRead = BinaryToString(FileRead($sFile))
FileClose($sFile)

$sBoundary = "mymultipartboundary" 

$sPD = '--' & $sBoundary & @CRLF & _
		'Content-Type: application/json; charset=UTF-8' & @CRLF & @CRLF & _
		'{ "text": "JSON Text }' & @CRLF & _
		'--' & $sBoundary & @CRLF & _
		'Content-Type: image/jpeg' & @CRLF & _
		'Content-Transfer-Encoding: binary' & @CRLF & @CRLF & _
		$sFileRead & @CRLF & '--' & $sBoundary & '--' & @CRLF

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", "http://www.yourservice.com/aaa", False)
$oHTTP.SetRequestHeader("Content-Type", 'multipart/related; boundary="' & $sBoundary & '"')
$oHTTP.Send(StringToBinary($sPD))
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

ConsoleWrite($oStatusCode & @CRLF)
ConsoleWrite($oReceived)

XML Parsing in AutoIT using Msxml2.DOMDocument

posted Feb 25, 2014, 1:33 PM by Chris G   [ updated Mar 20, 2014, 11:29 AM ]

AutoIT Code Sample - Parsing an XML document. Here is the sample XML document to parse:

<results>
<status>OK</status>
<url>
http://technotes.videreresearch.com/autoit-sample-code
</url>
<language>english</language>
<text>
AutoIT Code Sample - Adding a shortcut in the user's Windows Startup folder in order to auto-start an AutoIT application every time a user logs-on to Windows. This script assumes that there is a checkbox defined on your Form, and will dynamically create or delete the shortcut on the check or uncheck event. ... While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 ;Do something Case $Checkbox1 If BitAND(GUICtrlRead($Checkbox1), $BN_CLICKED) = $BN_CLICKED Then If _GUICtrlButton_GetCheck($Checkbox1) Then ConsoleWrite("Checkbox checked... " & @CRLF) If Not FileExists(@StartupDir & "\linkName.lnk") Then FileCreateShortcut(@ScriptFullPath, @StartupDir & "\linkName.lnk", @ScriptDir, "", "AutoIT Script Description") EndIf Else ConsoleWrite("Checkbox unchecked... " & @CRLF) If FileExists(@StartupDir & "\linkName.lnk") Then You may also want to check for the presence of the shortcut when starting the application in order to properly display the state of the checkbox to the user. ... If FileExists(@StartupDir & "\linkName.lnk") Then _GUICtrlButton_SetCheck($Checkbox1) EndIf
</text>
<keywords>
<keyword>
<text>AutoIT Code Sample</text>
<relevance>0.990765</relevance>
</keyword>
<keyword>
<text>AutoIT Script Description</text>
<relevance>0.885643</relevance>
</keyword>
<keyword>
...
</keyword>
<keyword>
<text>nMsg</text>
<relevance>0.432048</relevance>
</keyword>
</keywords>
</results>
Here is the AutoIT code sample used to parse this XML file, using Msxml2.DOMDocument.3.0:
;===============================================================================
; Description:		XML Parsing in AutoIT using Msxml2.DOMDocument
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================

$feed = "http://url.to.xml.file/"
$oOXml = _XmlFileLoad ($feed)
$oXMLRoot = $oOXml.documentElement
$content = ""

$oXmlroot = $oOXml.documentElement

$objElement = $oXmlroot.getElementsByTagName("language")
If IsObj($objElement) Then ConsoleWrite($objElement(0).childNodes(0).nodeValue &@crlf)
;For $oXmlNode In $objElement
	;ConsoleWrite($oXmlNode.nodename & " - "  & $oXmlNode.text &@crlf)
;Next

$objElement = $oXmlroot.getElementsByTagName("text")
If IsObj($objElement) Then ConsoleWrite($objElement(0).childNodes(0).nodeValue &@crlf)
;For $oXmlNode In $objElement
	;ConsoleWrite($oXmlNode.nodename & " - "  & $oXmlNode.text &@crlf)
;Next

$objElement = $oXmlroot.getElementsByTagName("keyword")
For $oXmlNode In $objElement
	;ConsoleWrite($oXmlNode.nodename & " - "  & $oXmlNode.text &@crlf)
	For $oXmlNodeD In $oXmlNode.childNodes
		;ConsoleWrite($oXmlNodeD.nodename & " - "  & $oXmlNodeD.text &@crlf)
		Select
		Case $oXmlnodeD.nodename = "text"
			ConsoleWrite($oXmlnodeD.text &":"&@crlf)
		Case $oXmlnodeD.nodename = "relevance"
			ConsoleWrite($oXmlnodeD.text &@crlf)
		EndSelect
	Next

Next



;===============================================================================
; Function Name:	 _XmlFileLoad
; Description:		Load and parse the XML file
; Parameters:		$strFile	XML file to load
; Syntax:			_XmlBuildTree($objXml,$treeview)
; Author(s):		Stephen Podhajecki 
; Returns:			Object handle on success
;					0 on failure and sets error to parser error.
;===============================================================================
Func _XmlFileLoad($strXmlFile)
	$strFile = $strXmlFile
	;if not isobj($oXml) then
	$oXml = ObjCreate("Msxml2.DOMDocument.3.0")
	$oXml.async=0
	$oXml.load ($strFile)
	If $oXml.parseError.errorCode <> 0 Then
		MsgBox(4096, "Error", "Error opening specified file: " & $strFile & @CRLF & $oXml.parseError.reason)
		SetError($oXml.parseError.errorCode)
		Return 0
	EndIf
	Return $oXml
EndFunc   ;==>_XmlFileLoad

AutoIT http POST Request

posted Feb 25, 2014, 1:33 PM by Chris G   [ updated Oct 13, 2016, 6:11 PM ]

AutoIT Code Sample - a quick way to make an http (or https) POST request to a web service and process the returned value:

;===============================================================================
; Description:		AutoIT http POST Request
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================

$sPD = 'parm1=aaa&parm2=bbb'
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", "https://my.full.url/service", False)
$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
$oHTTP.Send($sPD)
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

ConsoleWrite($oStatusCode & @CRLF)
;ConsoleWrite($oReceived)
If $oStatusCode = 200 Then
	;Process the response $oReceived
Else
	MsgBox(16, "Error " & $oStatusCode, $oReceived, 7)
EndIf

AutoIT Code Sample - Delete folder by date

posted Feb 21, 2014, 2:54 PM by Chris G   [ updated Mar 20, 2014, 11:26 AM ]

AutoIT Code Sample - Delete subfolders older than a given number of weeks


Imagine this scenario...on a particular Windows server, a faulty application is creating temporary folders and not deleting them.

This AutoIT script can be used to delete all of the subfolders that are older than a given number of weeks. The folder age is determined by the creation date of the folder itself, it does not look at any of the files inside; or the last accessed date! An event is then added to the server's event log to list the number of folders deleted as well as the space saved (in MB).Here is the source code:
;===============================================================================
; Description:		Delete folder by date
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================

#Include <file.au3></file.au3>
#Include <array.au3></array.au3>
#Include <date.au3></date.au3>

;The path of the folder containing the temporary folders to be deleted
$TempPath = "C:\Program Files\temp\dmserver\tmp"
;Files older than this given number of weeks will be deleted
$numberOfWeeks = 3

;The size of the folder before the delete
$sizeBefore = Round(DirGetSize($TempPath) / 1024 / 1024)

$FileList=_FileListToArray($TempPath, "*", 2)
If @Error=1 Then
    ;MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf
;_ArrayDisplay($FileList,"$FileList")

$NUmberOfFiles = UBound($FileList)
ToolTip($NUmberOfFiles & "Files ")

$deletedFolders = 0

for $i = 1 To $NUmberOfFiles - 1
	ToolTip(ProcessGetStats() & " - File " & $i & " of " & $NUmberOfFiles & " - " & $FileList[$i])
	if FileExists($TempPath & "\" & $FileList[$i]) Then
		$t =  FileGetTime($TempPath & "\" & $FileList[$i], 1)
		If Not @error Then
			$yyyymd = $t[0] & "/" & $t[1] & "/" & $t[2]
			ConsoleWrite($TempPath & "\" & $FileList[$i] & " - " & $yyyymd)
			if _DateDiff( 'w', $yyyymd, _NowCalc()) > $numberOfWeeks Then
				ConsoleWrite(" old" & @CRLF)
				$deletedFolders += 1
				DirRemove($TempPath & "\" & $FileList[$i], 1)
			Else
				ConsoleWrite(@CRLF)
			EndIf
		EndIf
	EndIf
Next

$sizeAfter = Round(DirGetSize($TempPath) / 1024 / 1024)
$size = $sizeBefore - $sizeAfter

;Add an event to the event log
$SystemEvent='eventcreate /T WARNING /ID 999 /L APPLICATION /SO "File Cleanup" /D "' & $deletedFolders & ' old temp folders ('& $size &' MB) were deleted in folder ' & $TempPath & '."'
RunWait(@ComSpec & " /c " & $SystemEvent, "", @SW_HIDE)
Sleep(10000)



Now all you have to do is schedule this task daily or weekly, and the files will be under control...

Using a Scripting Dictionary object in AutoIT

posted Feb 19, 2014, 2:33 PM by Chris G   [ updated Mar 20, 2014, 11:28 AM ]

AutoIT Code Sample - using a Scripting Dictionary object in AutoIT to store key-value pairs:

;===============================================================================
; Description:		Using a Scripting Dictionary object
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================

$oDictionary = ObjCreate("Scripting.Dictionary")
$oDictionary.ADD("One", "Same") $oDictionary.ADD("Two", "Car") $oDictionary.ADD("Three", "House") $oDictionary.ADD("Four", "Boat")

;List all keys For $vKey In $oDictionary ConsoleWrite("-> " & $vKey & " - " & $oDictionary.Item($vKey) & @CRLF) Next If $oDictionary.Exists('Two') Then     MsgBox(0x0, 'Item Two', $oDictionary.Item('Two'), 2) EndIf
;Delete all keys For $vKey In $oDictionary     $oDictionary.Remove($vKey) Next

Connecting to a MS-SQL server in AutoIT

posted Jan 23, 2014, 3:01 PM by Chris G   [ updated Oct 13, 2016, 6:14 PM ]

AutoIT sample code - Connecting to a MS-SQL database server:
 
;===============================================================================
; Description:		Connecting to a MS-SQL database server
; Author(s):		Videre Research, LLC - http://videreresearch.com
;===============================================================================

$sqlCon = ObjCreate("ADODB.Connection")  
$sqlCon.Open("Provider=SQLOLEDB;Data Source=change_me_server_name;Initial Catalog=change_me_database_name;Connect Timeout=150;Network Library=dbmssocn; User ID=change_me; Password=change_me;")  
IF IsObj($sqlCon) then  
 
    $rs = ObjCreate( "ADODB.RecordSet" )  
    $rs.Open( "SELECT * from change_me_table_name where last_updated > '" & @MON&"/"&@MDAY&"/"&@YEAR & "' order by last_updated", $sqlCon )  
     
   $recordcount =0  
    While $rs.eof <> TRUE  
     
    ConsoleWrite($rs.Fields( "userid" ).Value & @CRLF)  
    ConsoleWrite($rs.Fields( "log_results" ).Value & @CRLF & @CRLF)  
     
    for $x in $rs.Fields  
       ConsoleWrite($x.name)  
       ConsoleWrite(" = ")  
       ConsoleWrite($x.value & @CRLF)  
    next  
 
        $recordcount +=1  
        $rs.MoveNext  
    WEnd  
   $rs.Close  
   $sqlCon.Close  
   ConsoleWrite($recordcount & @CRLF)  
EndIf  


1-7 of 7