7 Replies Latest reply on Nov 3, 2010 10:55 PM by Joe ... Ward

    Returning list of files in directory not working.

    celebritymusic Level 1

      Gidday

       

      I'm using the following code to try to put a listing of files in a dir into an array.

       

      I created a dir call myDir in the same place as my .swf and .air, and placed a file in it.  But I get error messages telling me the File or directory does not exist.

       

      Why would this be?

       

      var dir:File = File.desktopDirectory.resolvePath("myDir");
      var files:Array = dir.getDirectoryListing();

       

      Thanks for taking a look.

        • 1. Re: Returning list of files in directory not working.
          Joe ... Ward Level 4

          This will only work for what you seem to intend if "myDir" ws created on the desktop directory. Is there a "myDir" folder on your desktop?

          • 2. Re: Returning list of files in directory not working.
            celebritymusic Level 1

            Ahh - no.

             

            So how do I chane the AS3 if myDir is not on the desktop, but a folder that is in the same place as the swf or .exe Flash file?

             

            Thanks Joe

            • 3. Re: Returning list of files in directory not working.
              Joe ... Ward Level 4

              The File class defines a number of standard directories that you can reference. (File.desktopDirecory is one of those.)

               

              The File.applicationDirectory references the directory in which your application is installed. This directory and the files it contains can be read, but not written too.

               

              So with that caveat in mind, you could change your code to this:

              var dir:File = File.applicationDirectory.resolvePath("myDir");

              var files:Array = dir.getDirectoryListing();

               

              (When you are developing the app and running it from Flash CS, Flash Builder, or ADL, the application directory is the one that contains the application descriptor file. Most likely this is the same location as the SWF file, but it is possible that you have them in different directories.)

              1 person found this helpful
              • 4. Re: Returning list of files in directory not working.
                celebritymusic Level 1

                That makes sense.

                 

                Is there a stnadard directory to use that allows the writing of files?  The app I have in mind has XML files that need to be updated according to what the user does in the app.

                 

                I found this function that saves the contents of a text field in a text file when the user clicks a button:

                 

                function saveHandler(event:Event):void {
                var file:File = File.desktopDirectory.resolvePath( "myjot.txt" );
                var jot:String = taEditor.text;
                var stream:FileStream = new FileStream();
                stream.open( file, FileMode.WRITE );
                stream.writeMultiByte( jot, File.systemCharset );
                stream.close();
                }

                 

                So if files can be written top the desktop, but not the application, how do I stipulate another folder within the application folder to write to?

                 

                Thanks for your help and all your answers this morning Joe (I'm in Aussie) - it's shedding a lot of light for me.

                 

                Shaun

                • 5. Re: Returning list of files in directory not working.
                  Joe ... Ward Level 4

                  The File.applicationStorage directory is designed for such a purpose. The only caveat is that you shouldn't store hundreds of Megabytes there since it is in a location on the file system intended for application preferences.

                  1 person found this helpful
                  • 6. Re: Returning list of files in directory not working.
                    celebritymusic Level 1

                    OK (still trying to get my head around the folder structure)

                     

                    Say my project exe is called test.exe, and  I also have a folder called image_files and another called text_files - how do I access the image_files folder, and how do I write txt documents to the text_files folder?  In other words, I don't want to access the user's folder, or the application storage folder, just specific folders that I have set up in my Flash as3 project.

                     

                    Or does security prevent accessing and writing to such folders?

                     

                    So how would I alter the following code to write to the text_files folder?

                     

                    function saveHandler(event:Event):void {
                    var file:File = File.applicationStorageDirectory.resolvePath( "myjot.txt" );
                    var jot:String = taEditor.text;
                    var stream:FileStream = new FileStream();
                    stream.open( file, FileMode.WRITE );
                    stream.writeMultiByte( jot, File.systemCharset );
                    stream.close();
                    }

                     

                    I want the user to be able to copy image and video files to the image_files folder, and then I have access to these in the air app.  So I don't want to put them in the application Storage Directory because A) like you said, it's not designed for large amounts of data, and B) I don't want the user to have to navigate to the app data folder - I want to keep navigation simple - so program files/my app/image_files (or preferably give them the option of choosing the folder where they copy their image and video files to - so it could be on d) drive for example).

                     

                    Thanks for your time and help

                     

                    Shaun

                    • 7. Re: Returning list of files in directory not working.
                      Joe ... Ward Level 4

                      For such things as user-created images and text files, you should use File.documentsDirectory, which points to the user's documents directory (unsurprisingly), and possibly create a subfolder for your app. You could also, as you mention, let the user choose a directory.

                       

                      For temporary files, you can create a temporary directory using the File class.

                       

                      And yes, subfolders within the application directory are also not writable, so you can't simply create the folders in your project.

                       

                      To use a text_files directory within the documents directory, you could do this:

                      var textDir:File = File.documentsDirectory.resolvePath("text_files");

                      if( !textDir.exists ) textDir.createDirectory()

                      var file:File = textDir.resolvePath("myjot.txt");

                      //etc...