VBScript Random file selector

June 11, 2009 at 12:57:50
Specs: Windows XP
I'm making a random wallpaper switcher, and I've hit a snag. I'm using the awesome software Ultramon, and I've ALMOST got what I want.
I have a script that changes the wallpaper on a set interval, the only problem is it's not random. It just uses the next file in the folder.

Here is the script I have so far:

Option Explicit

Const INTERVAL = 20 '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")
	'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

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
					WScript.Sleep INTERVAL * 60 * 1000
				End If
		End If

Can anyone help me put a randomizer in this script?



See More: VBScript Random file selector

Report •

June 12, 2009 at 09:31:02
I don't vbs, but here's how I'd do it in a batch. Maybe you can incorporate the batch or call it and have it set a file name that your vbs can read.

@echo off & setLocal EnableDelayedExpansion

dir /b/a-d c:\files | find /v /n "" > filelist
set /a s=!random!%%440

for /f "tokens=1* delims=[]" %%a in (filelist) do (
if %%a equ !s! echo use %%b

If at first you don't succeed, you're about average.


Report •
Related Solutions

Ask Question