This is probably a strange request, but I’m trying to find a script that will display the contents of multiple folders from the user’s harddrive to the user only.
For example, a certain program may have files spread through different directories. I want a field that will display those files from all directories (i.e. all directories that are specified – no need for user input on that). If possible, alphabetization would be sweet. I saw this script that claimed to display files:
<script type=”text/javascript”>
var path = ‘C:/…..’; // Starting directory
function thefiles() {
// Puts the file names of this directory into an array
var file_array = get_files(path).split(‘|’).sort();
}
</script>
<script type=”text/VBScript”>
Function get_files(path)
Dim List
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set Folder = fso.GetFolder(path)
Set Files = Folder.Files
List = “”
If Files.Count > 0 Then
For Each File In Files
List = List & File.Name & “|”
Next
End If
get_files = List
End Function
</script>
—
However, this does not work for me. Could someone tell me what I am doing wrong, or where I should be looking for this kind of thing?
Thanks!
WHS
var fileList = "";
function insertList(){
document.forms.Form1.listField.value = fileList;
}
Function CreateFileList(folderSpec)
Set fso = CreateObject("Scripting.FileSystemObject")
Set currFolder = fso.GetFolder(folderSpec)
Set nFile = currFolder.Files
For Each isFile in nFile
nameStr = nameStr & isFile.name & vbCRLF
Next
fileList = nameStr
Set fso = Nothing
insertList()
End Function
Function checkExist(testFolder)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(testFolder) Then
CreateFileList(testFolder)
Exit Function
End If
MsgBox "The folder " & testFolder & " does not exist..."
Set fso = Nothing
End Function
[i]Originally posted by whs [/i]Such a message is produced by ActiveX itself, so no browser other than IE can throw it onto your screen. So it was logical, that you tried Warren's script in IE.
[B]"ActiveX component can't create object: Scripting.FileSystemObject"[/B][/QUOTE]
function getStats(fName){
nullIMG.src = fName;
fullName = fName;
shortName = fullName.match(/[^/\]+$/);
splitName = fullName.split(".");
fileType = splitName[1];
fileType = fileType.toLowerCase();
if (fileType == 'gif' || fileType == 'jpg' || fileType == 'jpeg')
{
checkIt = fileSize(fullName);
if (parseInt(checkIt) > 30000){alert('Max File Size is 30Kb');Form1.reset()}
else {
Form1.dispSize.value = checkIt;
Form1.dispName.value = shortName;
Form1.dispW.value = nullIMG.width;
Form1.dispH.value = nullIMG.height;
}
}
else {
alert("You must select an image file!");
Form1.reset();
}
}
divStr = "<Div Style='Position:Absolute;Top:-2000'><IMG Src=Null ID=nullIMG></Div>"
document.write(divStr)
Function fileSize(fileSpec)
Dim isSize
Set fso = CreateObject("Scripting.FileSystemObject")
Set contentFile = fso.GetFile(fileSpec)
isSize = contentFile.Size
fileSize = isSize
Set contentFile = Nothing
Set fso = Nothing
End Function
function insertList(){
fileList1 = CreateFileList('C:\Windows\Desktop\Project')
fileList2 = CreateFileList('C:\Windows\Desktop\Project')
document.forms.Form1.listField.value = fileList1+fileList2;
}
window.onload=insertList;
Function CreateFileList(folderSpec)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(folderSpec) Then
Set currFolder = fso.GetFolder(folderSpec)
Set nFile = currFolder.Files
For Each isFile in nFile
nameStr = nameStr & isFile.name & vbCRLF
Next
CreateFileList = nameStr
Set fso = Nothing
Exit Function
End If
CreateFileList = "The folder " & folderSpec & " does not exist..."
Set fso = Nothing
End Function
[CODE]function WriteToFile(temp,output) {
var filename = output;
var drvpath = filename.charAt(0)+':'+'\';
var fso = new ActiveXObject('Scripting.FileSystemObject');
var d = fso.GetDrive(drvpath);
if(fso.DriveExists(d.DriveLetter)==true){
if(d.IsReady){
if(fso.FolderExists(getFolder(output))){
if(fso.FileExists(output)){
input_box=confirm("Are you sure you want to replace the selected file?");
if (input_box==true){
var file = fso.CreateTextFile(filename, true);
file.WriteLine(temp);
file.Close();
confirm('HTML Script Succesfully Encoded! - © Diricx Bart');
}
}
else{
var file = fso.CreateTextFile(filename, true);
file.WriteLine(temp);
file.Close();
confirm('HTML Script Succesfully Encoded! - © Diricx Bart');
}
}
else{
input_box=confirm("The folder doesn't exist, do you want to create it?");
if (input_box==true){
var path = getFolder(filename);
var index = 4;
var index2 = 4;
var temp2=4;
do{
index = path.indexOf('\',temp2);
index2 = filename.indexOf('.');
var folder = filename.substring(0,index+1);
if(!fso.FolderExists(folder) && index!=-1){
var file2 = fso.CreateFolder(folder);
}
temp2=index+2;
}while(index!=-1)
var file = fso.CreateTextFile(filename, true);
file.WriteLine(temp);
file.Close();
confirm('HTML Script Succesfully Encoded! - © Diricx Bart');
}
}
}
else alert("The selected drive isn't ready !!!");
}
else alert("The selected drive doesn't exist !!!");
}
function getFolder(s){
var index = s.indexOf('.');
var i = index;
var spil=true;
do{
i--;
if(s.charAt(i)=='\'){
index=i;
spil=false;
}
}while(spil==true);
return s.substring(0,index+1);
}
function ReadIt(s) {
var filename = s;
var fso, a, ForReading;
output = "";
ForReading = 1;
fso = new ActiveXObject('Scripting.FileSystemObject');
file = fso.OpenTextFile(filename, ForReading, false);
try{
while(true){
output+=file.readline()+"n";
}
} catch(Exception){}
file.Close();
return output;
}[/CODE]
[i]Originally posted by rodentje [/i]
[B]i'm very interested in the latter too, i'm looking forward to an answer on this one...
ps, warren, why do you always use VBScript for the fso object, it works in JavaScript as well... I wrote this lately:
[CODE]function WriteToFile(temp,output) {
var filename = output;
var drvpath = filename.charAt(0)+':'+'\';
var fso = new ActiveXObject('Scripting.FileSystemObject');
var d = fso.GetDrive(drvpath);
if(fso.DriveExists(d.DriveLetter)==true){
if(d.IsReady){
if(fso.FolderExists(getFolder(output))){
if(fso.FileExists(output)){
input_box=confirm("Are you sure you want to replace the selected file?");
if (input_box==true){
var file = fso.CreateTextFile(filename, true);
file.WriteLine(temp);
file.Close();
confirm('HTML Script Succesfully Encoded! - © Diricx Bart');
}
}
else{
var file = fso.CreateTextFile(filename, true);
file.WriteLine(temp);
file.Close();
confirm('HTML Script Succesfully Encoded! - © Diricx Bart');
}
}
else{
input_box=confirm("The folder doesn't exist, do you want to create it?");
if (input_box==true){
var path = getFolder(filename);
var index = 4;
var index2 = 4;
var temp2=4;
do{
index = path.indexOf('\',temp2);
index2 = filename.indexOf('.');
var folder = filename.substring(0,index+1);
if(!fso.FolderExists(folder) && index!=-1){
var file2 = fso.CreateFolder(folder);
}
temp2=index+2;
}while(index!=-1)
var file = fso.CreateTextFile(filename, true);
file.WriteLine(temp);
file.Close();
confirm('HTML Script Succesfully Encoded! - © Diricx Bart');
}
}
}
else alert("The selected drive isn't ready !!!");
}
else alert("The selected drive doesn't exist !!!");
}
function getFolder(s){
var index = s.indexOf('.');
var i = index;
var spil=true;
do{
i--;
if(s.charAt(i)=='\'){
index=i;
spil=false;
}
}while(spil==true);
return s.substring(0,index+1);
}
function ReadIt(s) {
var filename = s;
var fso, a, ForReading;
output = "";
ForReading = 1;
fso = new ActiveXObject('Scripting.FileSystemObject');
file = fso.OpenTextFile(filename, ForReading, false);
try{
while(true){
output+=file.readline()+"n";
}
} catch(Exception){}
file.Close();
return output;
}[/CODE]
or has it any advantages over js?
regards, RoDeNtJe [/B][/QUOTE]
<select name=fieldname size=5 style="width:120;">
<option value=C:...>file1</option>
<option value=C:...>file2</option>
<option value=C:...>file3</option>
</select>
[CODE]<HTML>
<Head>
<Script Language=JavaScript>
window.onunload=function()
{
alert("Thanks for using this script !!!");
};
var fileList = "";
function displayList(){
document.forms['form1'].output.value = fileList;
}
function WriteToFile(output) {
var filename = output;
var text = fileList;
var drvpath = filename.charAt(0)+':'+'\';
var fso = new ActiveXObject('Scripting.FileSystemObject');
var d = fso.GetDrive(drvpath);
if(fso.DriveExists(d.DriveLetter)==true){
if(d.IsReady){
if(fso.FolderExists(getFolder(output))){
if(fso.FileExists(output)){
input_box=confirm("Are you sure you want to replace the selected file?");
if (input_box==true){
var file = fso.CreateTextFile(filename, true);
file.WriteLine(text);
file.Close();
confirm('List Succesfully Created !!!');
}
}
else{
var file = fso.CreateTextFile(filename, true);
file.WriteLine(text);
file.Close();
confirm('List Succesfully Created !!!');
}
}
else{
input_box=confirm("The folder doesn't exist, do you want to create it?");
if (input_box==true){
var path = getFolder(filename);
var index = 4;
var index2 = 4;
var temp2=4;
do{
index = path.indexOf('\',temp2);
index2 = filename.indexOf('.');
var folder = filename.substring(0,index+1);
if(!fso.FolderExists(folder) && index!=-1){
var file2 = fso.CreateFolder(folder);
}
temp2=index+2;
}while(index!=-1)
var file = fso.CreateTextFile(filename, true);
file.WriteLine(text);
file.Close();
confirm('List Succesfully Created !!!');
}
}
}
else alert("The selected drive isn't ready !!!");
}
else alert("The selected drive doesn't exist !!!");
}
function getFolder(s){
var index = s.indexOf('.');
var i = index;
var spil=true;
do{
i--;
if(s.charAt(i)=='\'){
index=i;
spil=false;
}
}while(spil==true);
return s.substring(0,index+1);
}
</Script>
<Script Language=VBScript>
Function CreateFileList(folderSpec)
Set fso = CreateObject("Scripting.FileSystemObject")
Set currFolder = fso.GetFolder(folderSpec)
Set nFile = currFolder.Files
For Each isFile in nFile
nameStr = nameStr & isFile.name & vbCRLF
Next
fileList = nameStr
Set fso = Nothing
displayList()
End Function
Function checkExist(s)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(s) Then
CreateFileList(s)
Exit Function
End If
MsgBox "The folder "+s+" does not exist..."
Set fso = Nothing
End Function
</Script>
</Head>
<Body>
<Div id='listDisp'></Div>
<br><br>
<form name="form1">
<textarea id="folder" style="width:75%" size="1" tabindex="1"></textarea>
<input type=button value="List Files" tabindex="2" onclick="checkExist(document.forms['form1'].folder.value)">
<textarea id="output" style="width:75%; height:350" tabindex="3"></textarea>
<br><br>
<textarea id="outputFile" style="width:75%" size="30" tabindex="4"></textarea>
<input type="button" value="Write To File" tabindex="5" onclick="WriteToFile(document.forms['form1'].outputFile.value),reset()">
</Body>
</HTML>[/CODE]
Thanks a lot for all of your help so far![/QUOTE]
<Script Language=JavaScript>
function insertList(){
fileList1 = CreateFileList('H:iPod_Control Music F00 ')
fileList2 = CreateFileList('H:iPod_Control Music F01 ')
fileList3 = CreateFileList('H:iPod_Control Music F02 ')
fileList4 = CreateFileList('H:iPod_Control Music F03 ')
document.forms.Form1.listField.value = fileList1+fileList2+fileList3+fileList4;
}
window.onload=insertList;
</Script>
<Script Language=VBScript>
Function CreateFileList(folderSpec)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(folderSpec) Then
Set currFolder = fso.GetFolder(folderSpec)
Set nFile = currFolder.Files
For Each isFile in nFile
nameStr = nameStr & isFile.name & vbCRLF
Next
CreateFileList = nameStr
Set fso = Nothing
Exit Function
End If
CreateFileList = Nothing
Set fso = Nothing
End Function
</Script>
[/QUOTE]
<select id='test' onChange="getFilter(listitem)" size=5>
<option value="X:pathtofilefile">file
<option value="X:pathtofilefile">file
</select>
[/quote]
<script type="text/JScript">
function getFilter(listitem){
var object = "";
var listValue = getListValue(listitem);
document.formCategory.submit(listValue);
//alert(listValue);
}
function getListValue(list){
var listValue="";
if (list.selectedIndex != -1) {
listValue = list.options[list.selectedIndex].value;
listValue = removeSpaces(listValue);
}
return (listValue);
}
function ReadIt() {
var filename = listitem;
var fso, a, ForReading;
output = "";
ForReading = 1;
fso = new ActiveXObject('Scripting.FileSystemObject');
file = fso.OpenTextFile(filename, ForReading, false);
try{
while(true){
output+=file.readline()+"n";
}
} catch(Exception){}
output += "<!-- HTML Code Encoded By RoDeNtJe ! -->";
file.Close();
return output;
}
</script>
[/quote]
<button onclick="ReadIt()">open selected</button>
[/quote]
<Form name='Form1' height="100%">
<p align="center">
<script type="text/JScript">
function getFilter(listitem){
var object = "";
var listValue = getListValue(listitem);
document.formCategory.submit(listValue);
//alert(listValue);
}
function getListValue(list){
var listValue="";
if (list.selectedIndex != -1) {
listValue = list.options[list.selectedIndex].value;
listValue = removeSpaces(listValue);
}
return (listValue);
}
function ReadIt() {
var filename = listitem;
var fso, a, ForReading;
output = "";
ForReading = 1;
fso = new ActiveXObject('Scripting.FileSystemObject');
file = fso.OpenTextFile(filename, ForReading, false);
try{
while(true){
output+=file.readline()+"n";
}
} catch(Exception){}
file.Close();
return output;
}
</script>
<select id='test' onChange="getFilter(listitem)" size=5 style="height: 100%; width:100%;" onfocus="top.js._CM_OnFocusField(window,this)" name="musiclist" multiple>
<option value="C:Music 3 - Beautiful Day.mp3">03 - Beautiful Day.mp3
<option value="C:Music8. I Will Follow.mp3">8. I Will Follow.mp3
</select>
</p>
<span>
<button onclick="ReadIt(listitem)">open selected</button>
</form>
[/quote]
<button onclick="ReadIt([B]listitem[/B])">open selected</button>[/QUOTE]
function ReadIt(and nothing here) {[/QUOTE]
onfocus="top.js._CM_OnFocusField(window,this)" [/QUOTE]
var shortName = new Array();
var pathStr = new Array();
function createSelect(){
fileList1 = CreateFileList('C:\Windows\Desktop\Music Suite\Rock 1\')
fileList2 = CreateFileList('C:\Windows\Desktop\Music Suite\Rock 2\')
completeList = fileList1+fileList2;
completeList = completeList.split(/rn/)
for (i=0; i<completeList.length; i++)
{
shortName[i] = completeList[i].match(/[^/\]+$/);
pathStr[i] = completeList[i].replace(shortName[i],"")
}
isList = document.forms.Form1.dynList;
nOptions = shortName.length;
for (i=0; i<nOptions; i++)
{
isData = new Option(shortName[i],completeList[i]);
isList.add(isData,isList.options.length);
}
}
function playSelect(isVal){
if (isVal != "")
{
document.Player.filename = isVal;
document.Player.play();
}
}
window.onload=createSelect;
Function CreateFileList(folderSpec)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(folderSpec) Then
Set currFolder = fso.GetFolder(folderSpec)
Set nFile = currFolder.Files
For Each isFile in nFile
nameStr = nameStr & folderSpec & isFile.name & vbCRLF
Next
CreateFileList = nameStr
Set fso = Nothing
Exit Function
End If
CreateFileList = ""
Set fso = Nothing
End Function
if (isVal != ""){
document.Player.filename = isVal;
document.Player.play();
}
var listStr = "";
function getAllVals(isForm){
nSongs = isForm.dynList;
for (i=1; i<nSongs.length; i++)
{
//alert(nSongs[i].value);
listStr += nSongs[i].value +","
}
alert(listStr)
}
I think something in the vbscript part has to be altered, but i don't know vbscript...[/QUOTE]I thought you were doing the same thing with js and an FSO?
[CODE]<body>
<form name="f" method="post" action="">
<select id="s0" name="s0">
<option value="0">choose</option>
<option value="1">text1</option>
<option value="2">text2</option>
</select>
</form>
<div><input type="button" id="b0" name="b0" value="use select.length=1 to erase the options" /></div>
<div><input type="button" id="b1" name="b1" value="use removeChild() to erase the options" /></div>
<div><input type="button" id="b2" name="b2" value="use Options = null to erase the options" /></div>
<div> </div>
<div><input type="button" id="b3" name="b3" value="add to list: appendChild()" /></div>
<div><input type="button" id="b4" name="b4" value="insert a new Option() at index=3" /></div>
</body>
</html>[/CODE]
<html>
<head><title></title>
</style><script type="text/javascript" language="javascript">
<!-- Hide from browsers without javascript
window.onload=function()
{
document.getElementById("b0").onclick = set_length;
document.getElementById("b1").onclick = remove_nodes;
document.getElementById("b2").onclick = null_Options;
<i> </i>document.getElementById("b3").onclick = append_Child;
<i> </i>document.getElementById("b4").onclick = new_Option;
};
function set_length()
{
document.getElementById("s0").length = 1;
}
function remove_nodes()
{
var sel = document.getElementById("s0");
<i> </i>for(var i = 1, len = sel.options.length; i < len; i++)
<i> </i>{
<i> </i> sel.removeChild(sel.options[1]); //array elements automatically shift down one position so
<i> </i> //can't use i for the position--it will go out of
<i> </i> //bounds at some point. So, just delete the same position
<i> </i> //everytime.
<i> </i>}
}
function null_Options()
{
var opts = document.getElementById("s0").options;
for(var i = 1, len = opts.length; i < len; i++)
{
opts[1] = null; //array elements are automatically reordered, and they shift down
//one position, so you can't use i for the index position--it will
//go out of bounds at some point. So, just set the same position
//to null everytime.
}
}
function append_Child()
{
var new_option = document.createElement("option");
var sel = document.getElementById("s0");
new_option.setAttribute("value", sel.options.length); //use the position in the array
//to set the value and text(next line)
var text = document.createTextNode("choice" + sel.options.length);
new_option.appendChild(text); //appends to the end of the list
<i> </i>sel.appendChild(new_option);
}
function new_Option()
{
var new_option = new Option("choice3", "3");
document.getElementById("s0").options[3] = new_option;
}
// End hiding -->
</script>
</head>
[CODE]function createSelect(){
if(!document.forms.Form1.checkbox.checked){
document.forms.Form1.dynList.length = 1;
}[/CODE]
but even when i change the code and let the 'if' drop, so everytime createSelect loads, dynList.length is set to 1...So, are you saying that you set the list length to 1, and then miraculously other entries appear in the list? There are no functions that add entries to the list? If there are functions that add items to the list, don't you think there could be an error in one of those?
and still the data in the list is incorrect (first c:testtest (see previous post), then c:test results in a list containing 1.txt and 3.txt, but this should result in only 1.txt)[/QUOTE]
this results in a list containing 2.txt 3.txt 1.txt 3.txt, and this should result in 2.txt 3.txt 1.txt,[/QUOTE]What did the list look like before that operation?
[CODE]function createSelect(){
if(!document.forms.Form1.checkbox.checked){
document.forms.Form1.dynList.length = 1;
}[/CODE]
[CODE]function createSelect(){
document.forms.Form1.dynList.length = 1;[/CODE]
0.1.9 — BETA 6.17