Populating dropdown box with filenames

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cainnech
    New Member
    • Nov 2007
    • 132

    #1

    Populating dropdown box with filenames

    Hi all,

    I have this code
    Code:
    <html>
    <head>
    <script type="text/javascript"> 
    var path = "C:\\Test\\Directory" 
    
    function ShowFolderFileList() 
    { 
    var fso, f, fc, s, temp; 
    fso = new ActiveXObject("Scripting.FileSystemObject"); 
    f = fso.GetFolder(path); 
    fc = new Enumerator(f.files); 
    s = "";
    temp = "";
    for (; !fc.atEnd(); fc.moveNext()) 
      { 
      temp = fc.item();
      document.getElementById('filelist').options[document.getElementById('filelist').options.length] = new Option (temp, temp); // First value is the TEXT of the option, the second is the VALUE of the option.
      } 
    }
    
    </script>  
    
    </head>
    <body>
    <input type="button" onclick="ShowFolderFileList()" value="TEST">
    <select id="filelist">
    
    </select>
    
    </body>
    </html>
    Now this code works perfectly but it shows the entire pathname of the file.
    So what I tried to do is to remove the path and just keep the filename. So I added into the loop just below temp = fc.item();

    Code:
      s = temp.replace(/C:\\Test\\directory\\/g,"")
    And when I do this I get this error: This property or method is not supported by this object.

    So the problem is that I'm referring to temp which is actually an object. And the replace function only works on strings. So my problem is that I don't know how to change it.

    When I alert(temp) right after temp = fc.item(); I do get the complete path. So that's what's so weird to me.

    Anyone with some thoughts?
    Thanks,
    Kenneth
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    I'm not sure what the exact type of item() would be, but perhaps you could try using the toString() method.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      you could either get the property of temp, that holds the string or use the temp.toString() method to convert the object to a string.

      Comment

      • Cainnech
        New Member
        • Nov 2007
        • 132

        #4
        The .toString() metod doesn't work. But after a while doing "research" on the net and experimenting I found out that the item() has a series of properties depending on what kind of object you are referring to.

        In that view I found that what I was trying to do was not necessary because this is already a property of the item.

        So the new code has become:

        Code:
        <html>
        <head>
        <script type="text/javascript"> 
        var path = "C:\\test\\directory" 
        
        function ShowFolderFileList() 
        { 
        		var fso, f, fc, s, temp; 
        		fso = new ActiveXObject("Scripting.FileSystemObject"); 
        		f = fso.GetFolder(path); 
        		fc = new Enumerator(f.files); 
        		s = "";
        		temp = "";
        		for (; !fc.atEnd(); fc.moveNext()) 
        { 
        temp = fc.item();
        s = temp.name;
        document.getElementById('filelist').options[document.getElementById('filelist').options.length] = new Option (s, s);
        }; 
        }
        
        </script>  
        
        </head>
        <body>
        <input type="button" onclick="ShowFolderFileList()" value="TEST">
        <select id="filelist">
        
        </select>
        
        </body>
        </html>
        So the s = temp.name; shows me immediately the name of the file instead of the complete path.

        Thanks for putting me in the right direction.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Glad you managed to solve it. Here's a link to the Name property and a reference for completeness.

          Comment

          Working...