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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get vbscript array

Status
Not open for further replies.

gibbo171

Programmer
Dec 13, 2007
33
GB
Hello All

I have the following vbscript

<script type="text/vbscript">


Function GetFileList()

folder = "C:\Documents and Settings\HP_Owner\Desktop\Slideshow\pics\"

set fso = CreateObject("Scripting.fileSystemObject")
set fold = fso.getFolder(folder)
for each file in fold.files
msgbox file.name
next
set fold = nothing: set fso = nothing
End Function

</script>

this gets the filename of all files in a certain directory for me and works fine but what i would like to do is add each file to an array and instead of using options in the code below use the array instead, is this possible and if so can someone give me an example please


<select name="slide" onChange="change();">
<option value="./pics/pic01.jpg" selected>Cart
<option value="./pics/pic02.jpg">AAT
<option value="./pics/pic03.jpg">Boat
</select>

thanks

gibbo
 
Building the array is easy. From there you can loop through the array to do whatever else you need to witht he list.

Code:
GetFileList "C:\Documents and Settings\HP_Owner\Desktop\Slideshow\pics\" 

Function GetFileList(folder)

intcount = 0
Dim fileArray()
 
    Set fso = CreateObject("Scripting.fileSystemObject") 
    Set fold = fso.getFolder(folder) 
    For Each file in fold.files 
    	ReDim Preserve fileArray(intcount)
    	fileArray(intcount)=file.Name
        WScript.Echo fileArray(intCount)
        intCount = intCount + 1 
    Next 
    Set fold = nothing: set fso = nothing 
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark

THat was Really Helpful, I ammended it slightly as im testing on a local machine as follows

Function GetFileList()

folder = "C:\Documents and Settings\HP_Owner\Desktop\Slideshow\pics\"

intcount = 0
Dim fileArray()

set fso = CreateObject("Scripting.fileSystemObject")
set fold = fso.getFolder(folder)

for each file in fold.files
ReDim Preserve fileArray(intcount)
fileArray(intcount)=file.Name
msgbox fileArray(intCount)
intCount = intCount + 1

next
msgbox intcount
set fold = nothing: set fso = nothing

End Function

Can you assist with the second part of my problem where i need my array to link into here

<select name="slide" onChange="change();">
<option value="./pics/pic01.jpg" selected>Cart
<option value="./pics/pic02.jpg">AAT
<option value="./pics/pic03.jpg">Boat
</select>


I dont need selection boxes though, but i am trying to get this slide show working, it will be saved on a small local network ( Not a web server) and i just want it to point at the folder from my array as apposed to having to hard code the page each time I want to change the pictures

thanks again

Gibbo


<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
var rotate_delay = 5000; // delay in milliseconds (5000 = 5 secs)
current = 0;
function next() {
if (document.slideform.slide[current+1]) {
document.images.show.src = document.slideform.slide[current+1].value;
document.slideform.slide.selectedIndex = ++current;
}
else first();
}
function previous() {
if (current-1 >= 0) {
document.images.show.src = document.slideform.slide[current-1].value;
document.slideform.slide.selectedIndex = --current;
}
else last();
}
function first() {
current = 0;
document.images.show.src = document.slideform.slide[0].value;
document.slideform.slide.selectedIndex = 0;
}
function last() {
current = document.slideform.slide.length-1;
document.images.show.src = document.slideform.slide[current].value;
document.slideform.slide.selectedIndex = current;
}
function ap(text) {
document.slideform.slidebutton.value = (text == "Stop") ? "Start" : "Stop";
rotate();
}
function change() {
current = document.slideform.slide.selectedIndex;
document.images.show.src = document.slideform.slide[current].value;
}
function rotate() {
if (document.slideform.slidebutton.value == "Stop") {
current = (current == document.slideform.slide.length-1) ? 0 : current+1;
document.images.show.src = document.slideform.slide[current].value;
document.slideform.slide.selectedIndex = current;
window.setTimeout("rotate()", rotate_delay);
}
}
// End -->
</script>
</HEAD>
<BODY>

<center>
<form name=slideform>
<table cellspacing=1 cellpadding=4 bgcolor="#000000">
<tr>
<td align=center bgcolor="white">
<b>Image Slideshow</b>
</td>
</tr>
<tr>
<td align=center bgcolor="white" width=400 height=300>
<img src="./pics/pic01.jpg" name="show">
</td>
</tr>
<tr>
<td align=center bgcolor="#C0C0C0">
<select name="slide" onChange="change();">
<option value="./pics/pic01.jpg" selected>Cart
<option value="./pics/pic02.jpg">AAT
<option value="./pics/pic03.jpg">Boat
</select>
</td>
</tr>
<tr>
<td align=center bgcolor="#C0C0C0">
<input type=button onClick="first();" value="|<<" title="Beginning">
<input type=button onClick="previous();" value="<<" title="Previous">
<input type=button name="slidebutton" onClick="ap(this.value);" value="Start" title="AutoPlay">
<input type=button onClick="next();" value=">>" title="Next">
<input type=button onClick="last();" value=">>|" title="End">
</td>
</tr>
</table>
</form>
</center>

 
Don't put the folder path in the function, pass it to the function so you can re-use the function.

Code:
GetFileList "C:\Documents and Settings\HP_Owner\Desktop\Slideshow\pics\" 

Function GetFileList(folder)

intcount = 0
Dim fileArray()
 
    Set fso = CreateObject("Scripting.fileSystemObject") 
    Set fold = fso.getFolder(folder) 
    Response.Write "<select name='slide' onChange='change();'>"
    For Each file in fold.files 
        ReDim Preserve fileArray(intcount)
        fileArray(intcount)=file.Name
        Response.Write "<option value=" fileArray(intCount)& ">" & Left(fileArray(intCount),Len(fileArray(intCount)-2))
        intCount = intCount + 1 
    Next 
    Response.Write "</select>"

    Set fold = nothing: set fso = nothing 
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark,

this code causes an error for me,

thanks

Gibbo
 
Im sure this is my lack of knowledge heres the code I have in full

thanks

gibbo

<script type="text/vbscript">

GetFileList "C:\Documents and Settings\HP_Owner\Desktop\Slideshow\pics\"

Function GetFileList(folder)

intcount = 0
Dim fileArray()

Set fso = CreateObject("Scripting.fileSystemObject")
Set fold = fso.getFolder(folder)
Response.Write "<select name='slide' onChange='change();'>"
For Each file in fold.files
ReDim Preserve fileArray(intcount)
fileArray(intcount)=file.Name
Response.Write "<option value=" fileArray(intCount)& ">" & Left(fileArray

(intCount),Len(fileArray(intCount)-2))
intCount = intCount + 1
Next
Response.Write "</select>"

Set fold = nothing: set fso = nothing
End Function


</script>


<input type="button" value="Click me!"
onclick="GetFileList()" >

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
var rotate_delay = 5000; // delay in milliseconds (5000 = 5 secs)
current = 0;
function next() {
if (document.slideform.slide[current+1]) {
document.images.show.src = document.slideform.slide[current+1].value;
document.slideform.slide.selectedIndex = ++current;
}
else first();
}
function previous() {
if (current-1 >= 0) {
document.images.show.src = document.slideform.slide[current-1].value;
document.slideform.slide.selectedIndex = --current;
}
else last();
}
function first() {
current = 0;
document.images.show.src = document.slideform.slide[0].value;
document.slideform.slide.selectedIndex = 0;
}
function last() {
current = document.slideform.slide.length-1;
document.images.show.src = document.slideform.slide[current].value;
document.slideform.slide.selectedIndex = current;
}
function ap(text) {
document.slideform.slidebutton.value = (text == "Stop") ? "Start" : "Stop";
rotate();
}
function change() {
current = document.slideform.slide.selectedIndex;
document.images.show.src = document.slideform.slide[current].value;
}
function rotate() {
if (document.slideform.slidebutton.value == "Stop") {
current = (current == document.slideform.slide.length-1) ? 0 : current+1;
document.images.show.src = document.slideform.slide[current].value;
document.slideform.slide.selectedIndex = current;
window.setTimeout("rotate()", rotate_delay);
}
}
// End -->
</script>
</HEAD>
<BODY>

<center>
<form name=slideform>
<table cellspacing=1 cellpadding=4 bgcolor="#000000">
<tr>
<td align=center bgcolor="white">
<b>Image Slideshow</b>
</td>
</tr>
<tr>
<td align=center bgcolor="white" width=400 height=300>
<img src="./pics/pic01.jpg" name="show">
</td>
</tr>
<tr>
<td align=center bgcolor="#C0C0C0">
<select name="slide" onChange="change();">

</select>
</td>
</tr>
<tr>
<td align=center bgcolor="#C0C0C0">
<input type=button onClick="first();" value="|<<" title="Beginning">
<input type=button onClick="previous();" value="<<" title="Previous">
<input type=button name="slidebutton" onClick="ap(this.value);" value="Start"

title="AutoPlay">
<input type=button onClick="next();" value=">>" title="Next">
<input type=button onClick="last();" value=">>|" title="End">
</td>
</tr>
</table>
</form>
</center>

 
I have not tested all of your code but there is a line wrap problem.

Put all of the following on one line:

Response.Write "<option value=" fileArray(intCount)& ">" & Left(fileArray(intCount),Len(fileArray(intCount)-[red]4[/red]))

Please note my correction to my code in red. To drop the period and 3 character extension from the file name.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark,

no longer get the error but nothing happens, am i actually calling this function from anywhere?

Looking at this im thinking i may not be but im not sure how to either as never programmed anything browser based before

thanks

Gibbo
 
The other question i have is will Response.Write work on my local machine, this will not be running on a web server, just a small LAN

thanks

Gibbo
 
Response.write requires that the code be in an ASP page. So long as you are hosting on a server or your local machine with IIS it should work.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark,

I wasnt planning on hosting this on a server so I may have a problem,

basically all I need to make is a really simple IE based image slide show that can get the images from a directory on my computer, any ideas or examples you are aware of?

thanks

Gibbo
 
Mark

I have found this that would do what i want using Javascript if I can get the array to populate from VBScript, can your code be adapted to work with this

thanks

Gibbo

<form><input type="button" value="<- Back" onclick="clearInterval(slideshow); clickphoto(-1);">&nbsp;<input type="button" value="Resume Slideshow" onclick="slideshow=setInterval('swapimage()', 1000);">&nbsp;<input type="button" value="Next ->" onclick="clearInterval(slideshow); clickphoto(1);"></form>
<div id="caption" style="font-family:arial; font-size:13;"></div><br>
<img name="photo">

<script language="javascript">

function clickphoto(direction)
{
pi += direction;
pi %= photo.length;
if (pi ==-1) pi = photo.length - 1;
displayphoto(pi);

}

function swapimage()
{
pi++;
pi %= photo.length;
displayphoto(pi);
}

function displayphoto(photonum)
{
document.images['photo'].src=photo[photonum].name;
caption.innerHTML=(photonum + 1) + ' of ' + photo.length + '<br>&nbsp;';
if (photo[photonum].caption.length > 0)
{
caption.innerHTML+='<b>' + photo[photonum].caption + '</b>';
}
caption.innerHTML+='&nbsp;';
}

function Photo(name, caption)
{
this.name=name;
this.caption=caption;
return this;
}

var photo=new Array(), pi=0;

photo[pi++]=new Photo('./pics/pic01.jpg'
,'Caption1');
photo[pi++]=new Photo('./pics/pic02.jpg','Caption2');
photo[pi++]=new Photo('./pics/pic03.jpg','Caption3');

pi=0;

var caption=document.getElementById('caption');
displayphoto(pi);

var images=new Array();

for (var ii=0;ii<photo.length;ii++)
{
images[ii]=new Image();
images[ii].src=photo[ii].name;
}

var slideshow=setInterval('swapimage()', 10000);

</script>
 
Sorry I don't do Java. I suggest you post in the Java forum and get a sample do do the whole thing in Java.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark

Will do and thanks for spending so much time helping me

Gibbo
 
Mark

Will update this thread when i ve got this figured out but I have figured out how to get a variable form VBScript to Javascript as follows and thought you may be interested

Gibbo

<HTML>
<Head>

<Script Language=VBScript>

Dim VarTest


folder = "C:\Documents and Settings\HP_Owner\Desktop\Slideshow\pics\"

intcount = 0
Dim fileArray()

set fso = CreateObject("Scripting.fileSystemObject")
set fold = fso.getFolder(folder)

for each file in fold.files
ReDim Preserve fileArray(intcount)
fileArray(intcount)=file.Name
intCount = intCount + 1

next
VarTest = intcount

set fold = nothing: set fso = nothing

</Script>

<Script Language=JavaScript>


function callalert()

{
alert(VarTest);
}

</Script>


<Body>
<input type=button value="Get Alert" onclick="callalert()">
</Body>
</HTML>
 
Give this a try:

Code:
On Error Resume Next
Dim objFSO, oFO, oFolder, oFile, picDir, IE
Set objFSO = CreateObject("Scripting.FileSystemObject")
picDir = "C:\Users\markmac\Pictures"

Set oFolder = objFSO.GetFolder(picDir)
Set IE = CreateObject("InternetExplorer.Application")
IE.toolbar = false : IE.menubar = false : IE.statusbar = false : IE.Resizable = False

For Each oFile In oFolder.Files
	If Right(oFile.Name,3) = "jpg" Or Right(oFile.Name,3) = "gif" Then
		IE.Navigate2 "file://" & Replace(oFile.ParentFolder,"\","/")&"/" & oFile.Name
		IE.Visible = True
		WScript.Sleep 1000
	End If
Next

Set IE = Nothing
Set objFSO = Nothing

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Mark

Thanks again,

thats brilliant

It shows me the last image in the directory but doesnt rotate through the previous 2,

also how would i add some buttons,

thanks

gibbo
 
mark

re my last, works great actually scrolling through the images but still need to sort some buttons if you have any ideas

thanks

gibbo
 
Rather than buttons, why not just play with the Wscript.sleep to show the current image longer?

Otherwise, I suggest you find a copy of the book ASP.Net Web Matrix Starter Kit.

While Web Matrix has been depricated, the code within the book fully applies to you and it creates a web based slideshow with back and next buttons just like you are looking for.

You can get the book for as little as $.46 used on Amazon.




I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top