Example:
Searching the desktop for text files

Actions used:

Control Structure - END WHILE

Control Structure - WHILE

File - Search

Global List - Add

String - Count Delimited Strings

String - Get Delimited String

String - Parse Path

Variable - Set Value

In this example, we'll use a "File - Search" action to search for text files on the user's desktop. Then, we'll use a simple WHILE loop to get the name of each file, one at a time, and add the name to a global list.

Here's what the action list looks like:

First, we use a "File - Search" action with "*.txt" as the filename (pattern) to search for, and "%Desktop%" as the custom folder path to search in. We need to specify a variable name to receive the list of matching file paths when the action is performed—in other words, to receive the result of the action. In this case, we'll just use the default variable name, %Files%.

Notice that we've disabled the Local fixed drives (hard drives) option, so the file search action won't look all over the user's hard drive for text files. (We just want to search on the user's desktop.)

If the user has three text files on their desktop (Passwords.txt, Readme.txt and LoveLetter.txt), this action will set the value of %Files% to a delimited list of file paths, something like this:

C:\Desktop\Passwords.txt;;C:\Desktop\Readme.txt;;C:\Desktop\LoveLetter.txt

If the user doesn't have any text files on their desktop, the file search won't find any matching file paths, and %Files% will be set to an empty string ("").

Luckily, we can use the "String - Count Delimited Strings" action to find out how many file paths were found.

Now that we have a variable (%Files%) containing a list of paths to all of the text files on the user's desktop, and another variable (%FileCount%) with the number of files found, we can set up a while loop to get each "part" of the delimited string, one at a time.

The first step in any while loop is initializing a variable to act as a counter. To do this, we just use a "Variable - Set Value" action to set our counter variable (%w%) to "0".

The WHILE action itself is simple; the condition of "%w% < %FileCount%" means that each step through the loop will only happen if %w% is less than %FileCount%.

The first thing to do each time through the loop is to get the sub-string at index %w% in the delimited list with a "String - Get Delimited String" action. (We'll be incrementing %w% each time through the loop, so the first time through the loop, this will get the sub-string at index 0; the second time, it will get the sub-string at index 1; etc.) Since this is the full path to one of the text files, we'll store it in a variable called %full_path%.

Next, we'll extract just the filename from the full path with a "String - Parse Path" action, and store the filename in a variable called %filename%.

Now we can add the filename to the global list. (In order to store stuff in a global list at run time, you have to set it up in advance at design time; for this example, we used the Global Lists dialog to set up a list named "Desktop Text Files".) To add the filename to the list, we'll just use a "Global List - Add" action, pick our list's name from the drop down list, set it to add this item to the end of the list, and put our %filename% variable as the text we want to add.

Finally, at the end of the while loop we'll use a "Variable - Set Value" action to increment %w% by one, so the loop can continue with the next path in the delimited list (assuming there are any more paths to process in the delimited list).

Note that 1 is added to whatever the current value of %w% is, and then the result of the addition is stored back in %w%. The end result is that the value in %w% is increased by 1.

Adding 1 to a variable like this ("incrementing" it) is a very useful technique that you will use often when building while loops.

Here's a link to the finished example, as an AutoPlay Media Studio 4.0 actions XML file that you can import into an action list:

(Note: save this file to your hard drive and use a tool like WinZip to extract the XML file)