By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

Populating dropdown box with filenames

100+
P: 132
Hi all,

I have this code
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript"> 
  4. var path = "C:\\Test\\Directory" 
  5.  
  6. function ShowFolderFileList() 
  7. var fso, f, fc, s, temp; 
  8. fso = new ActiveXObject("Scripting.FileSystemObject"); 
  9. f = fso.GetFolder(path); 
  10. fc = new Enumerator(f.files); 
  11. s = "";
  12. temp = "";
  13. for (; !fc.atEnd(); fc.moveNext()) 
  14.   { 
  15.   temp = fc.item();
  16.   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.
  17.   } 
  18. }
  19.  
  20. </script>  
  21.  
  22. </head>
  23. <body>
  24. <input type="button" onclick="ShowFolderFileList()" value="TEST">
  25. <select id="filelist">
  26.  
  27. </select>
  28.  
  29. </body>
  30. </html>
  31.  
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();

Expand|Select|Wrap|Line Numbers
  1.   s = temp.replace(/C:\\Test\\directory\\/g,"")
  2.  
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
Mar 6 '09 #1
Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
I'm not sure what the exact type of item() would be, but perhaps you could try using the toString() method.
Mar 6 '09 #2

Dormilich
Expert Mod 5K+
P: 8,639
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.
Mar 6 '09 #3

100+
P: 132
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:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript"> 
  4. var path = "C:\\test\\directory" 
  5.  
  6. function ShowFolderFileList() 
  7.         var fso, f, fc, s, temp; 
  8.         fso = new ActiveXObject("Scripting.FileSystemObject"); 
  9.         f = fso.GetFolder(path); 
  10.         fc = new Enumerator(f.files); 
  11.         s = "";
  12.         temp = "";
  13.         for (; !fc.atEnd(); fc.moveNext()) 
  14. temp = fc.item();
  15. s = temp.name;
  16. document.getElementById('filelist').options[document.getElementById('filelist').options.length] = new Option (s, s);
  17. }; 
  18. }
  19.  
  20. </script>  
  21.  
  22. </head>
  23. <body>
  24. <input type="button" onclick="ShowFolderFileList()" value="TEST">
  25. <select id="filelist">
  26.  
  27. </select>
  28.  
  29. </body>
  30. </html>
  31.  
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.
Mar 6 '09 #4

acoder
Expert Mod 15k+
P: 16,027
Glad you managed to solve it. Here's a link to the Name property and a reference for completeness.
Mar 6 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.