Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Placing filenames into an Array help

Status
Not open for further replies.

girlinterrupted24

Programmer
Nov 16, 2005
35
ES
Hello there,

I don't know if I am on the right track at all... or whether what I want to do is possible... I basically would like to extract all filenames of files found in a folder, and to place these file names into an array... the orinal non dynamic version which works is this :

var theImageList = new Array(
// Name, URL
["Library image 1", "/images/library/1.jpg"],
["Library image 2", "/images/library/2.jpg"],
["Library image 2", "/images/library/3.jpg"]
);

This is what I have tried to do to make it dynamically extract all files found in folder, so these update automatically if files are added/deleted... but no luck as it doesn't work...

var fso = new ActiveXObject( 'Scripting.FileSystemObject' ),
e, f, i = [];
if( fso.FolderExists( 'c:\inetpub\ ) ) {
f = fso.GetFolder( 'c:\inetpub\ );
var tinyMCEImageList = new Array(
// Name, URL
e = new Enumerator(f.files);
for( ; !e.atEnd(); e.moveNext() ) {
if( ( i = e.item() ) ) { ["Library image " + i, i]; }
}

);
}

I would be ever so grateful for any help anyone could offer on getting this to work....Thank you
 
It is in a Browser pop up... hmm what alternative method could I maybe use... if I were to list the file names in the database wich i connect to via a dsn in ASP, or if I write a .txt document on the fly containing these, could I then extract the array items from this?

If so could you maybe show me an example of how I would do this... sorry to be annoying... Im a little lost when it comes to more complex things in javascript... I am so used to relying on ASP for everything I can...

Thank you
 
Look up how to use Windows Scripting Host to perform tasks like this. You can use JScript or VBScript, your choice, and not have to worry about browser security issues. All the files need to be accessible locally, including the database. The code snippet you provided doesn't really give any clues to indicate what you're really doing.

Lee
 
This is how thing's got done independent of the favorite "allow-disallow".
[tt]
var fso = new ActiveXObject( 'Scripting.FileSystemObject' );
if ( fso.FolderExists( 'c:[red]\\[/red]inetpub[red]\\[/red]wwwroot[red]\\[/red[images[red]\\[/red]library' ) ) {
var f=fso.getFolder('c:[red]\\[/red]inetpub[red]\\[/red]wwwroot[red]\\[/red[images[red]\\[/red]library');
var tinyMCEImageList=new Array();
var e=new Enumerator(f.files);
var i=1; [green]//your i is not what you think is[/green]
for (;!e.atEnd();e.moveNext()) {
tinyMCEImageList.push (["Library image " + i, e.item()]);
i++;
}
}
[/tt]
 
Personally, I'd go with this:

Code:
var folderToCheck = 'c:\\inetpub\\[URL unfurl="true"]wwwroot\\images\\library';[/URL]
var fso = new ActiveXObject('Scripting.FileSystemObject');
if (fso.FolderExists(folderToCheck)) {
	var f = fso.GetFolder(folderToCheck);    
	var tinyMCEImageList = [];
	var e = new Enumerator(f.files);
	for(var i=1; !e.atEnd(); e.moveNext(), i++) {
		tinyMCEImageList[tinyMCEImageList.length] = ['Library image ' + i, e.item()];
	}
}

It avoids the use of "push", and so would work on older IE browsers, too.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hey guys,

Thank you so much for your help... I popped both versions of the code in but it doesn't seem to work...

The list just does not show at all...

What it is is basically a drop down list of images available to insert into a content management system.

The original code is set to fixed images, which is how it is at the moment, and so these options are what appear in the drop down list of available images :

var tinyMCEImageList = new Array(
// Name, URL
["Library image 1", "/images/library/1.jpg"],
["Library image 2", "/images/library/2.jpg"],
["Library image 2", "/images/library/3.jpg"]
);

What I want to do is make this dynamic, as the user can upload new images, delete images from the Library at any time, so with this being the case I really need this list to be dynamic...

My other thoughts if this wasnt possible, would be to write via ASP a .txt file that contains the filenames and js can access, or can I access an ASP variable which would contain the library image names (these would always be numbers, ie 1.jpg, 4.jpg etc.)... I am a little lost as to what would be possible or the best way...

Thanks so much for your help...
 
Aah - try changing this from my last post:

Code:
['Library image ' + i, e.item()];

to this:

Code:
['Library image ' + i, '/images/library/' + e.item()];

It may well have been the wrong path that was causing the images not to work.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I will free myself from any statement of working or not working because I know it works as expected and as designed. The only problem is op either mixed every concept up. To get the peace, "can't do" might be the quickest escape.
 
Hi Dan,

Thanks again for all your help with this, although it still doesnt seem to work :( I think I just always have bad luck with javascript, I wish I knew more!

This is the code as is now (along with the commented out bit that does work, but is fixed!)

/*var tinyMCEImageList = new Array(
// Name, URL
["Library image 1", "/images/library/1.jpg"],
["Library image 2", "/images/library/2.jpg"],
["Library image 2", "/images/library/3.jpg"]
);

*/

var folderToCheck = 'c:\\inetpub\\var fso = new ActiveXObject('Scripting.FileSystemObject');
if (fso.FolderExists(folderToCheck)) {
var f = fso.GetFolder(folderToCheck);
var tinyMCEImageList = [];
var e = new Enumerator(f.files);
for(var i=1; !e.atEnd(); e.moveNext(), i++) {
tinyMCEImageList[tinyMCEImageList.length] = ['Library image ' + i, '/images/library/' + e.item()];
}
}

So you can see then where this is pulled in, in case it may help, it is here :

</tr>
<!-- Image list -->
<script language="javascript">
if (typeof(tinyMCEImageList) != "undefined" && tinyMCEImageList.length > 0) {
var html = "";

html += '<tr><td><label for="image_list">{$lang_image_list}</label></td>';
html += '<td><select id="image_list" name="image_list" style="width: 200px" onchange="this.form.src.value=this.options[this.selectedIndex].value;resetImageData();getImageData();">';
html += '<option value="">---</option>';

for (var i=0; i<tinyMCEImageList.length; i++)
html += '<option value="' + tinyMCEImageList[1] + '">' + tinyMCEImageList[0] + '</option>';

html += '</select></td></tr>';

document.write(html);
}
</script>
<!-- /Image list -->
<tr>

Thank you ever so much again for helping
 
Hey guys,

Sorry to be a pain with this... but does anyone know how I can get this to work... I am totally lost with it, any suggestions would be so so much appreciated

thanks as always
Jayne
 
Hey Dan,

Thanks ever so much for replying. The list of files will be on a server, not on the clients PC.

At the moment they are currently on my windows testing server which is c:\inetpub\ then the address in the my browser windows is
This is basically where I am trying to get it working firstly, then on the domains, but sadly at the moment it just doesnt work...

is there anything i need to change/can try?

Thank you again for helping me
Jayne
 
Then you need to do this with server-side scripting. If you want to use ASP, then you can use Javascript/JScript for the language if you want.

Lee
 
I must confess I am a little confused as to what you mean... I ideally would prefer to do all this in ASP as that is the language which I use all the time... but sadly I need this drop down box to work in an existing Javascript driven application which I cannot change to ASP... so I somehow do need this to be done in Javascript... unless I am totally missing the point and maybe you are suggesting that I need to do something in ASP, then this will feed it to the Javascript code as I have it?

Sorry to appear so totally dumb... my JS skills are really not the greatest when it comes to achieving more complex things... so any further help / suggestions you would be so greatly appreciated...

Thanks, Jayne
 
If you want to have a client-side JjavaScript have a list of files sitting on the server, you have to deliver them to the page - you cannot use client-side JavaScript to read them, as it has no access to the server's drive.

So you have no choice but to use server-side scripting of some sort - ASP (VBScript), ASP (JScript), PHP, JSP, etc.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Not tested but should work !

I have broken out the javascript aspect of this so should you have no file then TinyMce wouldnt fall over.

var theImageList = new Array(
// Name, URL


<!-- All that needs outputing is ["Library image 1", "/images/library/1.jpg"], -->

<%
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder(Server.MapPath("."))
Set fileList = objFiles.Files
For Each i in fileList
response.write ("[""" & i.name & """, ""Myloctaion/""" & i.name & """],")
next
%>
<!-- Leave empty one at the end (saves messing with the comma and falling over if there isnt any) -->
["", ""]
);



Regards
Das
If you have trouble with it email me

Look at Innovate Text Editor - Far superior
 
Just remember two things JS is client side so not possible to access files from server so had to be done in ASP, PHP ect

All your trying to do is output javascript code using ASP
if you run the example_image_list.asp file directly you will be able to see what is outputted as the js file and compare with what you are looking for.

das
 
Here is the code I use to read filenames of all GIF and JPG images into an array. I modified it to use your multi-dimensioned array and set "Library Image x" as the name as shown in your sample above.
Note: In your code what you show as the Name is not the name of the file, the name of the files are 1.jpg, 2.jpg, 3.jpg, etc and are included in the array under the URL section. It seems a bit confusing. I would expect it to be more like:
Name URL
1.jpg /images/library/

If your array code as shown above works though then my code here should work for you as well.
I test if it is the first entry and if not append the comma to separate each array entry as appropriate.

Code:
<%

  Dim fso
  Dim rootFolder
  Dim files
  Set fso = Server.CreateObject("Scripting.FileSystemObject")
  Set rootFolder = fso.GetFolder(Server.MapPath("/YOURFOLDERPATH/images/library/"))
  Set files = rootFolder.Files
 
  ' Write the javascript header
  response.write "<SCR" & "IPT LANGUAGE=""JavaScript"">" & vbCrlf
  ' then declare the array
  response.write "var theImageList = new Array();" & vbCrlf
  outstr="theImageList = ["
  Dim cnt
  cnt = 0
  For Each file in files
    If file.Type = "GIF Image" OR file.type = "JPG Image" Then
      ' Store filename in Client Side Javascript array theImageList
      If cnt <> 0 then outstr = outstr & ","
      outstr = outstr & "['Library image " & cnt+1 & "', '/images/library/" & file.Name & "']"
      cnt=cnt+1
    End If
  Next

  outstr = outstr & "];" & vbCrLf
  response.write outstr
  ' write the script trailer...
  Response.Write "</SCR" & "IPT>" & vbCrlf
  Set files = Nothing
  Set rootFolder = Nothing
  Set fso = Nothing
%>

Paranoid? ME?? Who wants to know????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top