There are a number of things that you can do with AutoIt that only take a few lines of code. I’ve decided to put a number of these into a some really quick scriptlets that you can then use to augment your scripts.
Determine Drive or Network Share
This script determines whether it is being run from a local drive or a network share. It will then display that drive or share.
Dim $LocationType Dim $Location If StringMid(@ScriptDir,2,1)=":" Then $LocationType = "Drive" Else $LocationType = "Network" EndIf Switch $LocationType Case "Drive" $Location = StringLeft(@ScriptDir,2) Case "Network" $Location = StringLeft(@ScriptDir,StringInStr(@ScriptDir,"\",-1,4)) EndSwitch MsgBox(0,$LocationType,$Location)
Download this code: RunFrom.au3
Replace Text in File
Use this code to replace every instance of a text string in a text file with a different text string.
$TextFileName = "TextFile.txt" $FindText = "dog" $ReplaceText = "cat" $FileContents = FileRead($TextFileName) $FileContents = StringReplace($FileContents,$FindText,$ReplaceText) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents)
Download this code: ReplaceText.au3
Internet File Downloader
You can generate your own custom download progress bar with this script.
$FileURL = "http://www.DailyCupOfTech.com/Downloads/TorparkSetup.exe" $FileName = "TorparkSetup.exe" $FileSaveLocation = FileSaveDialog("Save Location...",@ScriptDir,"All (*.*)",18,$FileName) $FileSize = InetGetSize($FileURL) InetGet($FileURL,$FileName,0,1) ProgressOn("","") While @InetGetActive $Percentage = @InetGetBytesRead * 100 / $FileSize ProgressSet($Percentage,"Downloaded " & @InetGetBytesRead & " of " & $FileSize & " bytes","Downloading " & $FileName) Sleep(250) Wend ProgressOff() MsgBox(0, "Done","Download Complete!")
Download this code: FileDownloader.au3
Create a Shortcut
This script creates a shortcut on the desktop to notepad.
$FileName = "C:\Windows\Notepad.exe" $LinkFileName = @DesktopDir & "\Text Editor.lnk" $WorkingDirectory = @DesktopDir $Icon = "C:\Windows\system32\SHELL32.dll" $IconNumber = 57 $Description = "This is the text editor that comes with Windows" $State = @SW_SHOWMAXIMIZED ;Can also be @SW_SHOWNORMAL or @SW_SHOWMINNOACTIVE FileCreateShortcut($FileName,$LinkFileName,$WorkingDirectory,"",$Description,$Icon,"",$IconNumber,$State)
Download this code: CreateShortcut.au3
Display Drive Information
Retrieve specific information about each of the drives connected to the system.
$DriveArray = DriveGetDrive("all") If Not @error Then $DriveInfo = "" For $DriveCount = 1 to $DriveArray $DriveInfo &= StringUpper($DriveArray[$DriveCount]) $DriveInfo &= " - File System = " & DriveGetFileSystem($DriveArray[$DriveCount]) $DriveInfo &= ", Label = " & DriveGetLabel($DriveArray[$DriveCount]) $DriveInfo &= ", Serial = " & DriveGetSerial($DriveArray[$DriveCount]) $DriveInfo &= ", Type = " & DriveGetType($DriveArray[$DriveCount]) $DriveInfo &= ", Free Space = " & DriveSpaceFree($DriveArray[$DriveCount]) $DriveInfo &= ", Total Space = " & DriveSpaceTotal($DriveArray[$DriveCount]) $DriveInfo &= ", Status = " & DriveStatus($DriveArray[$DriveCount]) $DriveInfo &= @CRLF Next MsgBox(4096,"Drive Info", $DriveInfo) EndIf
Download this code: DriveInfo.au3
Determine If A Process is Running
Checks to see if a specific named process is running and indicates its status.
$ProcessName = "Notepad.exe" If ProcessExists($ProcessName) Then MsgBox(0,"Running",$ProcessName & " is running.") Else MsgBox(0,"Not Running",$ProcessName & " is not running.") EndIf
Download this code: ProcessRunning.au3
Generate a Random Number
Generates a random number between two specified values.
$LowerLimit = 104 $UpperLimit = 213 $RandomNumber = Random($LowerLimit,$UpperLimit,1) MsgBox(0,"Random Number",$RandomNumber)
Download this code: RandomNumber.au3
Count Up Timer
This script will count the seconds that have passed until a certain number has been reached.
SplashTextOn("Timer","0 Seconds",125,25) $BeginTime = TimerInit() $CountTo = 10 $SecondsLapsed = 0 While $SecondsLapsed<$CountTo $TimeDifference = TimerDiff($BeginTime) $SecondsLapsed = Round($TimeDifference/1000,0) ControlSetText("Timer", "", "Static1", $SecondsLapsed & " Seconds") Sleep(1000) WEnd
Download this code: Timer.au3
Suggest More Scriptlets
I understand that this is just the tip of the iceberg when it comes to possible scriptlets. If you have any scriptlets of your own or any suggestions on some good scriptlets, just let me know and I will see what I can do.
|Trackback link - http://www.dailycupoftech.com/useful-autoit-scriptlets/trackback/|