Link to home
Start Free TrialLog in
Avatar of robotman757
robotman757

asked on

Enumerating Filesystem Object

Hello,

    I have written a web application that allows a user to log files that they have transferred. The main issue is that it is restricted to choosing files from one particular folder because they have to justify their transfers and log the security aspects of the files. There is a group that needs the ability to pick a folder and then get all the files from this folder and the subsequent subfolders into a single list while still including the path for each folder. I tried to find an easier way to do this, but have ran into a snag. Any help or pointers to help would be appreciated.

Thanks
Dan
Avatar of brgivens
brgivens

Is this an ASP question?  (it can't be done in JavaScript)
Avatar of DanRollins
It *can* be done using the FileSystemObject ActiveX object.

robotman757,
It seems pretty straight forward to enumerate the files and directories (and their files, etc..) and generate a list.  With what part are you having trouble?

-- Dan
Avatar of robotman757

ASKER

I am able to get one folder and get all the files in that folder and then pass the files into a listbox control. I pass the folder name through a single variable. What I want to do is start at this folder and get all files in it and then go to a subfolder and get any files and subfolders. I need to keep the full path to the file, and it has to store the path and filename in 2 seperate fields in the underlying Access database. I can't seem to find a way to get this to work.
var a = yourVariable.match(/(.+\/)([^\/]+)$/);

path = a[1];

fileName = a[2];
Here is the code that I currently use to get a single folder with the files. It is ran when a user presses a button.

function getfiles()
{

     var pShell = new ActiveXObject("Shell.Application");
     var pFolder = new Object;                              
         pFolder = pShell.BrowseForFolder(0, "Choose a folder", 0);      
         if (pFolder == null)
         {
          alert("You must choose a folder.");
          document.location.href="usrmadd2.asp?UserID=<% =uid %>";
         }      
     var pFolderItem = new Object;            
         pFolderItem = pFolder.Items().Item();
                                                   
         document.Form1.SelFldr.innerText = pFolderItem.Path;
         
         //Ensure there are no options in ListBox if ran again
         if (i!=0){
         for (var z=document.Form1.ListBox1.options.length-1; z>=0; z--)
            {
             document.Form1.ListBox1.options[z] = null;
            }
         numpass = 0;}
         
     var fso = new ActiveXObject("Scripting.FileSystemObject");
     var sFolder = new Object;
         if(fso.folderExists(pFolderItem.Path))
         {
          sFolder=fso.GetFolder(pFolderItem.Path);
          i = 0;
          fldrfiles=new Array();
          var sFile = new Enumerator(sFolder.Files);
           for (; !sFile.atEnd(); sFile.moveNext())
           {
            ss = sFile.item();
            tmpfile = new String(ss.name);
            fldrfiles[fldrfiles.length] = new Option(tmpfile, i);
            i++;
           }
            //alert(fldrfiles[2].text + fldrfiles[2].value);
            for (var y = 0; y < fldrfiles.length; y++)
            {
             var options = new Object;
              document.Form1.ListBox1.options[y] = new Option (fldrfiles[y].text , fldrfiles[y].value);
            }
            document.Form1.ListBox1.length=y;
          }
 }
var a,i,path=[],fileName=[];
for (i=0;i<document.Form1.ListBox1.options.length;i++){
  a = document.Form1.ListBox1.options[i].text.match(/(.+\/)([^\/]+)$/);
  path = a[1];
  fileName = a[2];
}

It might be   a = document.Form1.ListBox1.options[i].value, I'm not sure.
I don't understadn th part about the listbox and I believe the part about stroing data into an Access database is a separate issue.

The simplest solution involves calling a short routine that generates the fully-qualified pathname of every file that is in this directory or any child directory; that is

Directory name (String) =
   c:\dir1

Filename list (Array) =
   c:\dir1\file1.txt
   c:\dir1\file2.txt
   c:\dir1\file3.txt
   c:\dir1\file4.txt
   c:\dir1\dir2\fileA.txt
   c:\dir1\dir2\fileB.txt
   c:\dir1\dir2\dir3\fileC.txt
   c:\dir1\dir2\dir3\fileD.txt
   c:\dir1\dir2\dir3\dir4\fileE.txt
   c:\dir1\dir2\dir3\dir4\fileF.txt
   c:\dir1\dir4\fileG.txt
   c:\dir1\dir4\fileH.txt
   c:\dir1\dir5\fileI.txt

I can write code that will do that for you.  Will that help?  Will you be able to figure out how to look at each item and store it in a desired way into your database?

-- Dan
The whole program uses the list of files and for each file has to store the name of the file along with about 18 other pieces of information including the path. What I did was create a button to get the folder and then list all the files so that a user could then select the files they need which then moved them to another listbox. From that page they go to another area where the user decides what the security level is and where the file is going to be saved to. Then it has to go to a dynamically created page that does more stuff. This all works fine if it is getting files from one folder. The code I posted does that well. If you can write code that can go into each subfolder and get all that info, that would be great. I will find a way to pass the information to the next few pages.

Dan (My name too!)
>> each file has to store the name of the file along with about 18 other pieces of information...
>> If you can write code that can go into each subfolder and get all that info, that would be great.

What are the 18 pieces of info that you need?

If you have fully-qualified path-and-filename, then you can obtain a File object using FSO and it will tell you just about anything you need to know about a file.

    DateCreated Property
    DateLastAccessed Property
    DateLastModified Property
    Drive Property
    Name Property
    ParentFolder Property
    Path Property
    ShortName Property
    ShortPath Property
    Size Property
    Type Property

function GetDateCreated( filespec ) {
        var fso, f, s;
        fso = new ActiveXObject( "Scripting.FileSystemObject" );
        f = fso.GetFile( filespec );
        s = f.DateCreated;
        return( s );
}

So I ask you again:  
If I provide code to create an array of all filespecs in a folder and in the folders below that... (etc), will you be able to accomplish what you need to do?

-- Dan
I only need to get the path and file name. The other information is chosen on the other pages by the user. The program sends the list of files to a page that allows the user to select what the security level and need to know category is, and there are a few other choices as well. I have the program also store information about the user and date and time of the transfers. So the main thing I need is a way to get all the files and their paths starting at a certain folder and going through each folder and subfolder. The best option I believe would be to store this info into an array, and then I can send it where I need to.

Dan
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Dan for your help. I was so close but so far away. I went off on a wild tangent, and forgot about the simple "keep it simple stupid" mentality. I am going to modify the code so that I can get the paths into a hidden list box control. There will be a few more changes to make it all work, but that is not too hard. Thanks again.

Dan
Glad to help.  Thanks for the points and the grade :)
-- Dan