Option Explicit

Const UMDESKTOP_EXE = "C:\Program Files\UltraMon\UltraMonDesktop.exe"

Dim sh, fso
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

'get the location of the user and shared wallpaper folders
Dim dirWps(1)
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")

Dim i
For i = 0 To UBound(dirWps)
	If Right(dirWps(i), 1) <> "\" Then dirWps(i) = dirWps(i) & "\"
Next

'get name of current wallpaper
Dim curWp
curWp = sh.RegRead("HKCU\Control Panel\Desktop\Wallpaper")
i = InStrRev(curWp, ".")
curWp = Left(curWp, i) & "wallpaper"

'enumerate available wallpapers
Dim fldWp, fileWp, nextOne, nextWp, firstWp, fileWpFullName
For i = 0 To UBound(dirWps)
	Set fldWp = fso.GetFolder(dirWps(i))
	For Each fileWp In fldWp.Files
		If Right(fileWp.Name, 10) = ".wallpaper" Then
			fileWpFullName = dirWps(i) & fileWp.Name
			
			If firstWp = "" Then firstWp = fileWpFullName
			
			If nextOne = True Then
				nextWp = fileWpFullName
				Exit For
			ElseIf fileWpFullName = curWp Then
				nextOne = True
			End If
		End If
	Next
	
	If nextWp <> "" Then Exit For
Next

If nextWp = "" Then nextWp = firstWp

'load next wallpaper
If nextWp <> "" Then
	Dim cmd : cmd = """" & UMDESKTOP_EXE & """ /load " & nextWp
	sh.Run cmd
End If