This article describes how to use sample Windows Script Host (WSH) code to programmatically create a DHTML slide show in a folder that contains images.
NOTE: This article uses custom DHTML that may not be available in all browsers.
Step 1: Create the WSH Sample
NOTE: Because there are several versions of Microsoft Windows, the following steps may be different on your computer. If they are, see your product documentation to complete these steps.
- Click Start, point to Programs, point to Accessories, and then click Notepad.
- Type or paste the following WSH code into Notepad:
Option Explicit
' slideshow delay in milliseconds
Const lngDelay = 2000
' valid file extensions for slides
Const strValid = ".jpg.jpeg.gif.png"
' output file name
Const strFileName = "slideshow.htm"
' ******************** DO NOT MODIFY BELOW THIS LINE ********************
' Define variables.
Dim objFSO
Dim objOutput
Dim strImage
Dim strExt
Dim strFirst
Dim lngCount
Dim objFolder
Dim objFile
' Create the output file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile(strFileName)
' Output the page header and beginning of slideshow script.
objOutput.WriteLine "<html>" & vbCrLf & "<head>"
objOutput.WriteLine "<script language=""javascript"">" & vbCrLf & "<!--"
objOutput.WriteLine "function PreloadImage(tmpSImage)"
objOutput.WriteLine "{"
objOutput.WriteLine vbTab & "var tmpOImage=new Image();"
objOutput.WriteLine vbTab & "tmpOImage.src=tmpSImage;"
objOutput.WriteLine vbTab & "return tmpOImage;"
objOutput.WriteLine "}"
objOutput.WriteLine "var strImage = new Array("
' Get a folder object from the current to parse for files.
Set objFolder = objFSO.GetFolder(".")
' Loop through the files in the folder.
For Each objFile In objFolder.Files
' Does the file have an extension?
If InStr(objFile.Name,".") Then
strImage = objFile.Name
' Get the file name extension.
strExt = LCase(Mid(strImage,InStrRev(strImage,".")))
' Is the extension a valid extension?
If InStr(strValid,strExt) Then
' Output the code to load the image.
objOutput.Write "'" & strImage & "',"
If Len(strFirst) = 0 Then strFirst = strImage
lngCount = lngCount + 1
End If
End If
Next
objOutput.WriteLine "''" & vbCrLf & ");"
' Output the rest of the slideshow script.
With objOutput
.WriteLine "var objImage = new Array(strImage.length-2);"
.WriteLine "var lngCount = strImage.length - 1;"
.WriteLine "function LoadImages()"
.WriteLine "{"
.WriteLine vbTab & "if (lngCount >= 0)"
.WriteLine vbTab & "{"
.WriteLine vbTab & vbTab & "document.all['theSPN'].innerHTML=lngCount;"
.WriteLine vbTab & vbTab & "objImage[lngCount] = PreloadImage(strImage[lngCount]);"
.WriteLine vbTab & vbTab & "lngCount--;"
.WriteLine vbTab & vbTab & "setTimeout('LoadImages()', 50);"
.WriteLine vbTab & "}"
.WriteLine vbTab & "else"
.WriteLine vbTab & "{"
.WriteLine vbTab & vbTab & "lngCount = 0;"
.WriteLine vbTab & vbTab & "document.all['theDIV'].innerHTML='<img src=""" & strFirst & """ id=""theIMG"">';"
.WriteLine vbTab & vbTab & "setTimeout('SwapImage()'," & lngDelay & ");"
.WriteLine vbTab & "}"
.WriteLine "}"
.WriteLine "function SwapImage()"
.WriteLine "{"
.WriteLine vbTab & "++lngCount;"
.WriteLine vbTab & "if (lngCount<strImage.length-1)"
.WriteLine vbTab & "{"
.WriteLine vbTab & vbTab & "document.all['theIMG'].src=objImage[lngCount].src;"
.WriteLine vbTab & vbTab & "setTimeout('SwapImage()'," & lngDelay & ");"
.WriteLine vbTab & "}"
.WriteLine "}"
.WriteLine "//-->" & vbCrLf & "</script>"
.WriteLine "</head>" & vbCrLf & "<body onLoad=""setTimeout('LoadImages()'," & lngDelay * 2 & ");"">"
.WriteLine "<div align=""center"" id=""theDIV""><h2>Please wait...<br>"
.WriteLine "Loading " & lngCount & " images... (<span id=""theSPN"">0</span>)</h2></div>"
.WriteLine "</body>" & vbCrLf & "</html>"
End With
' Close the output file.
objOutput.Close
- Save the file to your desktop as Slideshow.vbs.
Step 2: Create the Slide Show
- Copy the Slideshow.vbs script to a folder that contains images (for example, to your My Documents\My Pictures folder).
- Double-click the script to run it.
When the script is finished running, you will have a file named Slideshow.htm, which contains the DHTML script for the slide show.