AutoIt Script Tutorial 001: Reading Files

Ξ June 8th, 2008 | → | ∇ AutoIT |

If you don’t know too much about AutoIt, or haven’t already read my What is AutoIt Scripting Language post, it may be helpful for you to check it out before reading this.

So, how do you read files with AutoIt? Don’t worry, it’s very easy.

  • The first thing that you need to do is open the file using the FileOpen function in AutoIt.
    • $variableName = FileOpen( fileName , mode )
      • $fileVariableName is a handle for the file you are opening, if you are going to be opening multiple files in your program ALWAYS use a unique handle.
      • fileName is the full path to the file you wish to openmod, if the file is in the same folder as your script is running, you can leave out the full path.
      • mode is the file access mode that you want to open the file with, for our example we will use 0 because we just want to open the file for reading.
        • File Modes:
          • 0 - Read Mode
          • 1 - Write Mode (Append writes to the end of the file)
          • 2 - Write Mode (Erase previous contents of the file)
          • 4 - Read Raw Mode
          • 8 - Create directory structure if it does not exist
  • Now that you’ve issued the FileOpen command, you want to make sure that AutoIt was able to open it properly. To do this you want to make sure that the FileOpen command didn’t generate an error return code value of -1…
    • If $fileVariableName = -1 then
      • MsgBox(0,”Failure”,”Failed to open file”)
      • Exit
    • EndIf
  • Now that we’re sure the file opened successfully, you can read all of the lines in the file:
    • While 1
      • $lineVariableName = FileReadLine($fileVariableName)
      • If @error = -1 Then ExitLoop
      • MsgBox(0, “MyLine = “, $lineVariableName)
    • WEnd
      • The code between While 1 and WEnd will be executed as long as each line is successfully read (not reached end of file).
      • $lineVariableName is the variable name that will store the line read as a string.
      • $fileVariableName is the file handle that you used when you opened the file.
      • MsgBox will display a message box with a title of “MyLine = ” and contain the string read from the file. You don’t have to use a message box for this, I’ll usually process the string that was read in, or pass it to a function to process it.
  • Now that we’ve read all the lines from the file, we have to remember to close the file:
    • FileClose($fileVariableName)
      • $fileVariableName is the file handle that you used when you opened the file.
  • That’s all there is to it!

Check back soon for a tutorial on how to manipulate strings so that you can read in a structured file, such as a CSV (comma separated values) file!

Full Sample Code:

$fileVariableName = FileOpen("input.txt", 0)
If $fileVariableName = -1 Then
MsgBox(0,”Failure”,”Failed to open file”)
Exit
EndIf
While 1

$lineVariableName = FileReadLine($fileVariableName)
If @error = -1 Then ExitLoop
MsgBox(0, “MyLine = “, $lineVariableName)
WEnd
FileClose($fileVariableName)

 

Leave a reply


About

    Place for about text

    The Detox theme is built with PS, brushes and patterns by milo IIIIVII.

    Open right sidebar.php in the theme folder to edit this message.
    Check my other themes too.

     

FlickR

    Solitude
    Solitude
    Solitude
    Solitude