Solved

Enumerating Filesystem Object

Posted on 2004-04-05
13
878 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:robotman757
  • 5
  • 5
  • 3
13 Comments
 
LVL 7

Expert Comment

by:brgivens
ID: 10762046
Is this an ASP question?  (it can't be done in JavaScript)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10762942
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
0
 
LVL 3

Author Comment

by:robotman757
ID: 10764651
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.
0
 
LVL 7

Expert Comment

by:brgivens
ID: 10766985
var a = yourVariable.match(/(.+\/)([^\/]+)$/);

path = a[1];

fileName = a[2];
0
 
LVL 3

Author Comment

by:robotman757
ID: 10767154
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;
          }
 }
0
 
LVL 7

Expert Comment

by:brgivens
ID: 10767295
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 49

Expert Comment

by:DanRollins
ID: 10767811
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
0
 
LVL 3

Author Comment

by:robotman757
ID: 10767939
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!)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10770051
>> 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
0
 
LVL 3

Author Comment

by:robotman757
ID: 10773598
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
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 250 total points
ID: 10781961
Here you go!  This is a fully functional HTA that provides the code you need plus demonstrates how to use it.  Just copy the following and paste it into a file named Tester.HTA. Then double-click that file.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- start of file Tester.HTA
<HTML><BODY>
<input type=button value="reload (if you edit the script)" onclick= "document.location.reload();">
<br>
<input type=button onclick="DoTest();" value="Do Test">
<br>
Results will go here:
<table border=2><TR><TD>
      <div id="stResults"><br></div>
</td></TR></TABLE>

<script>
var goFSO= new ActiveXObject("Scripting.FileSystemObject");

//-------------------------------
// populates Array asResult
//
function GetAllFilesInFolder( sStartingFolder, asResult )
{
      oFldr= goFSO.GetFolder( sStartingFolder );
      var eFile= new Enumerator( oFldr.Files );
      for (; !eFile.atEnd(); eFile.moveNext() ) {
            var s= eFile.item();
            asResult.push( s );
      }
      var eFldr= new Enumerator( oFldr.SubFolders );
      for (; !eFldr.atEnd(); eFldr.moveNext() ) {
            var s= eFldr.item();
            GetAllFilesInFolder( s, asResult );
      }
}

function DoTest()
{
      asAllFiles= new Array();

      GetAllFilesInFolder( "c:\\temp\\", asAllFiles );

      //----- the demo is over.  This is just to show the data in the array
      var s="";
      for ( var j=0; j<asAllFiles.length; j++ ) {
            s+= asAllFiles[j] + "<br>";       
      }
      document.all.stResults.innerHTML= s;
}
</script>
</BODY></HTML>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- end of file Tester.HTA

The function of interest is
       GetAllFilesInFolder( sStartingFolder, asResult )
which is recursive, so it's kind of fun to play with.  It adds each file to the Array, then looks at each folder and *calls itself* for each one. SWEET!   The result is that asResult ends up holding a list of fully-qualified filenames of all files in the specified folder plus all files in each folder that's in that folder, and so forth.

Later, if you need to split out the folder from the filename, just use (for instance)

   var oFile= goFSO.GetFile( asAllFiles[17] );
   var sNameAndExt= oFile.Name;          // eg:   filename.ext
   var sFldrPath=      oFile.ParentFolder; // eg:   c:\temp\mystuff

-- Dan
0
 
LVL 3

Author Comment

by:robotman757
ID: 10782347
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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10782590
Glad to help.  Thanks for the points and the grade :)
-- Dan
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now