Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

Need to include randomizer in VB Script

$
0
0
Below I have a script used to make a program called UltraMon display a new desktop picture every hour, similar to how window 7 and 8 already do as a theme. What I'd like to do is change the script so that it randomly picks a new wallpaper from the selections instead of the 'next' one on the list each and every time. The script currently works very well at choosing the next wallpaper on the list, but not what I'd like it to do. Here is the script below (I did not write it):

Code:

Option Explicit

Const INTERVAL = 15 'interval between wallpaper changes in minutes
Const UMDESKTOP_EXE = "%ProgramFiles%\UltraMon\UltraMonDesktop.exe"

Dim sh, fso
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

'check if UltraMon 3 or later is installed
Dim umVer
umVer = sh.RegRead("HKLM\Software\Realtime Soft\UltraMon\CurrentVersion")

'get the location of the wallpaper folder(s)
Dim dirWps(1)
If umVer = "" Then
        'UltraMon 2, location of the user and shared wallpaper folders stored in the registry
        dirWps(0) = sh.RegRead("HKCU\Software\Realtime Soft\UltraMon\Wallpaper\Wallpaper Directory")
        dirWps(1) = sh.RegRead("HKLM\Software\Realtime Soft\UltraMon\Wallpaper\All Users Wallpaper Directory")
Else
        'UltraMon 3 or later, wallpaper folder is at a known location
        dirWps(0) = sh.ExpandEnvironmentStrings("%APPDATA%\Realtime Soft\UltraMon\" & umVer & "\Wallpapers")
End If

Dim i
For i = 0 To UBound(dirWps)
        If dirWps(i) <> "" Then
                If Right(dirWps(i), 1) <> "\" Then dirWps(i) = dirWps(i) & "\"
        End If
Next

Do While True
        'enumerate available wallpapers
        Dim fldWp, fileWp, fileWpFullName
        For i = 0 To UBound(dirWps)
                If dirWps(i) <> "" Then
                        Set fldWp = fso.GetFolder(dirWps(i))
                        For Each fileWp In fldWp.Files
                                If Right(fileWp.Name, 10) = ".wallpaper" Then
                                        fileWpFullName = dirWps(i) & fileWp.Name
                                       
                                        'load next wallpaper
                                        Dim cmd : cmd = """" & UMDESKTOP_EXE & """ /load " & fileWpFullName
                                        sh.Run cmd
                                       
                                        'wait
                                        WScript.Sleep INTERVAL * 60 * 1000
                                End If
                        Next
                End If
        Next
Loop

I'm thinking where it says, "If Right(fileWp.Name, 10) = ".wallpaper" Then", that this is where I need to figure out a way to randomize the list. Clearly I need a way to determine how many ".wallpaper" files are in the directory, then choose one at random, then load that chosen wallpaper. I haven't done vba for awhile some I'm a bit rusty, and haven't worked with vbs at all before, but it looks similar to the vb in Excel which I am familiar with. Any thoughts on how I'd do this? It sounds super easy to me, but like I said, I haven't programmed in awhile so, I feel like I don't know how to do this.

Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>