Directories in ActionScript

Here’s the translation of the Go code to ActionScript, formatted in Markdown suitable for Hugo:

Our first program will work with directories in the file system. Here’s the full source code:

package {
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;

    public class DirectoryExample {
        public function DirectoryExample() {
            // Create a new sub-directory in the current working directory
            var subdir:File = File.applicationDirectory.resolvePath("subdir");
            subdir.createDirectory();

            // Helper function to create a new empty file
            function createEmptyFile(name:String):void {
                var file:File = subdir.resolvePath(name);
                var stream:FileStream = new FileStream();
                stream.open(file, FileMode.WRITE);
                stream.close();
            }

            createEmptyFile("file1");

            // Create a hierarchy of directories, including parents
            var parentChild:File = subdir.resolvePath("parent/child");
            parentChild.createDirectory();

            createEmptyFile("parent/file2");
            createEmptyFile("parent/file3");
            createEmptyFile("parent/child/file4");

            // List directory contents
            var parentDir:File = subdir.resolvePath("parent");
            var contents:Array = parentDir.getDirectoryListing();

            trace("Listing subdir/parent");
            for each (var entry:File in contents) {
                trace(" ", entry.name, entry.isDirectory);
            }

            // Change the current working directory
            File.applicationDirectory = parentChild;

            // Now we'll see the contents of subdir/parent/child when listing the current directory
            contents = File.applicationDirectory.getDirectoryListing();

            trace("Listing subdir/parent/child");
            for each (entry in contents) {
                trace(" ", entry.name, entry.isDirectory);
            }

            // Change back to where we started
            File.applicationDirectory = File.applicationDirectory.parent.parent.parent;

            // We can also visit a directory recursively, including all its sub-directories
            trace("Visiting subdir");
            visitDirectory(subdir);
        }

        private function visitDirectory(directory:File):void {
            var contents:Array = directory.getDirectoryListing();
            for each (var entry:File in contents) {
                trace(" ", entry.nativePath, entry.isDirectory);
                if (entry.isDirectory) {
                    visitDirectory(entry);
                }
            }
        }
    }
}

In ActionScript, we use the flash.filesystem package to work with directories and files. The File class represents files and directories, and provides methods for creating, listing, and manipulating them.

We start by creating a new subdirectory using createDirectory(). Then we define a helper function createEmptyFile() to create empty files.

The getDirectoryListing() method is used to list the contents of a directory, similar to ReadDir in the original example.

To change the current working directory, we modify the File.applicationDirectory property. This is analogous to using Chdir in the original code.

Finally, we implement a recursive directory traversal using a separate visitDirectory() function, which is similar to the WalkDir functionality in the original example.

Note that ActionScript runs in a sandboxed environment, so file system operations are subject to security restrictions. This code assumes it has the necessary permissions to perform these operations.

To run this code, you would typically compile it into a SWF file and run it in the Flash Player or AIR runtime. The exact process depends on your development environment and deployment target.

$ mxmlc DirectoryExample.as
$ adl DirectoryExample.xml

This would compile the ActionScript file and run it using the AIR Debug Launcher (adl). The output would be similar to the original example, listing the created directories and files.