Work with Tables and Files (Tablo ve Dosyalarla Çalışma)

guclusat

Tanınmış Üye
Süper Moderatör
Work with Tables and Files
In AutoPlay Media Studio, tables are frequently used when working with files. Specifically, we will look at how the results of a Dialog.FileBrowse action are stored in a table, and how it is possible to work with a text file loaded into a table.

Tables, in their basic form, are simply variables with many rows. The Dialog.FileBrowse action stores one file path per row in a table. When reading a text file into a table, every line in the text file is a separate row in the table.

Example 1
Using the Dialog.FileBrowse action, we will allow the user to browse a folder, and select multiple files. We will open each file with its default application:

Kod:
-- Present the user with a file browse dialog (with multiple select set to true)
tFiles = Dialog.FileBrowse(true, "Multiple Select", "", "All Files(*.*)|*.*|", "", "", true, true);
-- Ensure that tFiles contains something
if tFiles then
   -- Check if the user pressed "Cancel"
   if tFiles[1] == "CANCEL" then
      -- The user pressed cancel, do nothing
   else
      -- The user did not press cancel, traverse the table of files.
      for nIndex, sFile in tFiles do
         -- Open file with the default program.
         File.Open(sFile, "", SW_SHOWNORMAL);
      end
   end
end
In the example above, the Dialog.FileBrowse action returns a table containing all the files that were selected. If the cancel button is pressed, the first row in the table (referenced by tFiles[1]) is set to 'CANCEL'. Otherwise, each row in the table contains the path to one file.

Example 2
Using the TextFile actions, we will load the contents of a text file into a table, and insert the text "Line two is the best line ever!!" into the second line of the text file:

Kod:
-- Present the user with a file browse dialog (with multiple select set to false)
tFiles = Dialog.FileBrowse(true, "Single Select", "", "All Files(*.*)|*.*|", "", "", false, true);

-- Ensure that tFiles contains something
if tFiles then
   -- Check if the user pressed "Cancel"
   if tFiles[1] ~= "CANCEL" then
      -- The user did not press Cancel, continue

      -- Read the selected text file to a table
      tTextFileContents = TextFile.ReadToTable(tFiles[1]);

      -- Insert the text into line (row) two
      Table.Insert(tTextFileContents, 2, "Line two is the best line ever!!");

      -- Write the altered table back to the text file selected (DON'T append)
      TextFile.WriteFromTable(tFiles[1], tTextFileContents, false);
   end
end
 
Yukarı