Log In
Sign Up and Get Started Blogging!
JoeUser is completely free to use! By Signing Up on JoeUser, you can create your own blog and participate on the blogs of others!
Project: DesktopX
Scripts, tutorials, and general Desktop X help
PICTURE SLIDESHOW Part 3: Dealing with Subfolders
Intermediate
Published on February 15, 2007 By
sViz
In
DesktopX Tutorials
UPDATE 11/26/2007:
See
The WinCustomize Wiki Tutorial
for an updated, efficient slideshow tutorial. Part 1-
Link
Part 2-
Link
If creating a slideshow with folders was twice as hard as with files, then a slideshow with subfolders definitely proved to be twice as hard as with folders. It got pretty tricky in the brainstorming, trial and error process. What do you do with a subfolder, what do you do if the sub folder has subfolders, how do you make sure to get the files in the main folder, the sub folder, and the sub-subfolder, what if the subfolder is empty but the sub-subfolder has pictures, what if the subfolder has pictures but the sub-subfolder is empty? Contingencies! Contingencies! Contingencies! *deep breaths* It all worked out though. Again, I tried to make this a streamlined as possible. (Although, I’m sure that advanced coders will probably get migraines from all the redundancies in my scripts
) Ah, well. Make sure your object’s width and height is set (e.g. 200 x 200).
I’ll be breaking up the script here so I can explain it better.
Let’s take a look at the script, shall we?
Dim files, picscount, grpofpics, numofpics
Dim foldercheck, fs, f, f1, fc, sf, subfld, fldpics, subfld2
'Called when the script is executed
Sub Object_OnScriptEnter
picscount = 0
End Sub
Nothing new here except some extra variables I will be using.
Sub Object_OnDropFiles(files)
'Stop timer, clear variables
object.KillTimer 1
grpofpics = ""
subfld2 = ""
subfld = ""
fldpics = ""
subcount = 0
'Files have extensions (.bmp), a folder does not
'We search the string to see if it contains a period
foldercheck = Instr(1, files, ".")
Here, we clear out the variables before they are defined later on. Once again, we find out whether the user drops a file or a folder.
'If user drops a folder, get files within folder
If foldercheck = 0 Then
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(files)
Set fc = f.Files
Set sf = f.SubFolders
Here we get the folder and its files and subfolders
'In the main folder....
For Each f1 In fc
'Check file extensions for valid images
checkext = Instr(f1.name, ".")
If checkext > 0 Then
checkext = Split(f1.name,".")
extension = LCase(checkext(1))
End If
'Create a variable, listing only valid image files in folder
If extension = "bmp" Or extension = "png" _
Or extension = "ico" Or extension = "jpg" _
Or extension = "tga" Then
fldpics= fldpics & f & "\" & f1.name & "|"
End If
Next
In this version, the first procedure is to get the files in the main folder and create a string (list) of those filenames.
'Check if folder contains subfolders
For Each f1 In sf
subcount = subcount + 1
Next
'If folder contains subfolder get subfolder name,
'call function to get files from subfolder
If subcount >0 Then
For Each f1 In sf
subfld = f & "\" & f1.name
searchSubfolders
Next
Next we count up all the subfolders in the main folder. If there are any, we get the fullpath to and the name of the subfolder. Last, we call the function (searchSubfolders) that will retrieve the files from that subfolder. (That’s to come later)
'If there are subfolders within subfolder
If subfld2 <> "" Then
subfld2 = Left(subfld2, Len(subfld2)- 1)
subfldgrp = Split(subfld2, "|")
'Search sub-subfolders for images
For Each elem In subfldgrp
subfld = elem
searchSubfolders
Next
End If
If grpofpics <> "" Then grpofpics = Left(grpofpics, Len(grpofpics)-1)
End If
Although this piece of code is placed after the code that searches for subfolders in the main folder it is actually performed after the function that searches for and retrieves image files from the subfolder. From the information gathered in that function we determine whether or not there are sub-subfolders. If there are, we perform an image search in each of those sub-subfolders by calling the same function we’ve setup to search subfolders. (That’s to be explained later) Finally, if the variable ‘grpofpics’ contains image files (i.e. if it’s not blank or empty) then we do that procedure which removes the extra pipline like I explained in Part 2.
'Create variable listing both files from main folder
'and files from subfolder
If grpofpics = "" And fldpics <> "" Then fldpics = Left(fldpics, Len(fldpics)-1)
allpics = fldpics & grpofpics
'If there are images found, create array and count images
If allpics <> "" Then
grpofpics = Split(allpics, "|")
numofpics = UBound(grpofpics)
End If
Here, I start with another contingency: What if the main folder has images but the subfolder is empty? Take a look at these two examples.
If the subfolder has images:
The list of files in the main folder looks like this: fldpics = mainimage1.bmp|mainimage2.bmp|
The list of files in the subfolder looks like this: grpofpics = subimage1.bmp|subimage2.bmp
Put them together: allpics = mainimage1.bmp|mainimage2.bmp|subimage1.bmp|subimage2.bmp
What if the subfolder has no images:
The list of files in the main folder looks like this: fldpics = mainimage1.bmp|mainimage2.bmp|
The list of files in the subfolder looks like this: grpofpics =
Put them together: allpics = mainimage1.bmp|mainimage2.bmp|
See that? There’s an extra pipeline in the 2nd example when you put them together. It’s a case sensitive issue. In the first example it’s fine, in the 2nd example it’s not fine. Case sensitive issues call for If statements. So we tell DX to lop of that pipeline character only if there are no images in the subfolders. Sure, it looks pretty simple but I was going crazy trying to figure out this problem.
'If user drops files
ElseIf foldercheck > 0 Then
grpofpics= Split(files, "|")
For Each elem In grpofpics
'Check file extensions for valid images
checkext = Split(elem,".")
extension = LCase(checkext(1))
'Create a variable, listing only valid image files in folder
If extension = "bmp" Or extension = "png" _
Or extension = "ico" Or extension = "jpg" _
Or extension = "tga" Then
validpics= validpics & elem & "|"
End If
Next
'If there are images found, create array and count images
If validpics <> "" Then
validpics = Left(validpics, Len(validpics)-1)
grpofpics = Split(validpics, "|")
numofpics = UBound(grpofpics)
Else
grpofpics = ""
End If
End If
'If grpofpics contains images, set first picture on drop and add to picscount
If IsArray(grpofpics) = True Then
Object.Picture = grpofpics(0)
picscount = 1
Else
msgbox "No images found"
End If
'If there is more than one image start timer on drop
If numofpics > 0 Then object.SetTimer 1, 2000
End Sub
Everything here is the same. We define what to do if the user drops files, then we check if ‘grpofpics’ is an array and set the object picture. That concludes the OnDropFiles section. Now for the function that searches the subfolders for image files.
'Function to search subfolder for images
Sub searchSubfolders
Set f2 = fs.GetFolder(subfld)
Set fc2 = f2.Files
Set sf2 = f2.SubFolders
For Each f1 In fc2
'Check file extensions for valid images
checkext = Instr(f1.name, ".")
If checkext > 0 Then
checkext = Split(f1.name,".")
extension = LCase(checkext(1))
End If
'Create a variable, listing only valid image files in folder
If extension = "bmp" Or extension = "png" _
Or extension = "ico" Or extension = "jpg" _
Or extension = "tga" Then
grpofpics= grpofpics & f2 & "\" & f1.name & "|"
End If
Next
'Get names of subfolder subfolders, if any
For Each f1 In sf2
subfld2 = subfld2 & subfld & "\" & f1.name & "|"
Next
End Sub
In this piece of code we pretty much do what we’ve been doing. We get the name of the folder (in this case the name of the subfolder), get the files in the subfolder (checking them for validity), and get the names of the subfolders within the subfolder if there are any. As you can see there’s a 2 on the end of most of the variables (f2, fc2, sf2); that’s so we don’t confuse the information from the subfolder with the information from the main folder.
Sub Object_Ontimer1
'If count is higher than number of pics then reset count
If picscount > numofpics Then picscount = 0
'Set picture
Object.Picture = grpofpics(picscount)
'Add to count
picscount = picscount + 1
End Sub
All that’s left is the timer and nothing’s changed here. After all that hard work it’s time to sit back and enjoy the show!
That’s it. Thanks for reading!
Notes:
1. If you drop a large folder with many subfolders onto the object it will take a couple of seconds to search through all of them.
2. I ran into a problem with image link files (mypic.bmp.lnk). As you can see they have double extensions. If anyone has suggestions on how to rectify this please let me know.
Synergy:
DesktopX Wiki-
Link
DX Tutorials-
Link
My Tutorials-
Link
Assorted Tutorials-
Link
Me-
Link
Popular Articles in this Category
DesktopX 3: Creating a widget
Popular Articles from sViz
Happy GDPR Day...
Comments
1
ZubaZ
on Feb 15, 2007
Holy COW!
This is awesome!
Thankyou, thankyou, thankyou!!
Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:
Richer content, access to many features that are disabled for guests like commenting on the forums.
Access to a great community, with a massive database of many, many areas of interest.
Access to contests & subscription offers like exclusive emails.
It's simple, and FREE!
Sign Up Now!
Meta
Views
» 6084
Comments
»
1
Category
»
DesktopX Tutorials
Comment
Recent Article Comments
LightStar Design Windowblind...
Let's see your political mem...
Let's start a New Jammin Thr...
A day in the Life of Odditie...
Safe and free software downl...
Veterans Day
A new and more functional PC...
Post your joy
AI Art Thread: 2022
WD Black Internal and Extern...
Sponsored Links