468,110 Members | 1,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Random Wallpaper Script

Rabbit
12,509 Expert Mod 8TB
I created this VBScript to randomize my wallpaper. It also uses subfolders for sequential images. It chooses from one of the files or folders in the specified parent folder at random. If it chooses a file, it waits 10 minutes to change it. If it chooses a folder, it will step through the pictures in order every 5 seconds.

To use it, copy and save the code to a .vbs file and put it in your startup folder if you want it to start automatically.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim WshShell, oFolder, intNumber, strValue, i, oFile, strFolder, intFolders, intFiles, serialIndex, sleepTime, serialFolder
  3.  
  4. strFolder = "C:\Documents and Settings\username\My Documents\My Pictures\Wallpapers\"
  5. Set WshShell = WScript.CreateObject("Wscript.Shell")
  6. Set oFolder = WScript.CreateObject("Scripting.FileSystemObject").GetFolder(strFolder)
  7. serialIndex = 0
  8.  
  9. Do While True
  10.     Do While True
  11.         If serialIndex > 0 Then
  12.             serialIndex = serialIndex + 1
  13.             If serialIndex > serialFolder.Files.Count Then serialIndex = 0
  14.         End If
  15.  
  16.         If serialIndex > 0 Then
  17.             i = 0
  18.             For Each oFile In serialFolder.Files    
  19.                 i = i + 1
  20.                 If i = serialIndex Then Exit For                                                            
  21.             Next
  22.         Else
  23.             intFiles = oFolder.Files.Count
  24.             intFolders = oFolder.Subfolders.Count
  25.             Randomize
  26.             intNumber = Int((intFiles + intFolders) * Rnd) + 1
  27.  
  28.             If intNumber > intFiles Then
  29.                 sleepTime = 5000
  30.                 serialIndex = 1
  31.                 i = intFiles
  32.                 For Each serialFolder In oFolder.Subfolders     
  33.                     i = i + 1
  34.                     If i = intNumber Then Exit For                                                            
  35.                 Next
  36.  
  37.                 i = 0
  38.                 For Each oFile In serialFolder.Files     
  39.                     i = i + 1
  40.                     If i = serialIndex Then Exit For                                                         
  41.                 Next
  42.             Else
  43.                 sleepTime = 600000
  44.                 i = 0
  45.                 For Each oFile In oFolder.Files     
  46.                     i = i + 1
  47.                     If i = intNumber Then Exit For                                                          
  48.                 Next
  49.             End If
  50.         End If
  51.  
  52.         If oFile.Path <> strValue Then
  53.             strValue = oFile.Path
  54.             Exit Do
  55.         End If
  56.     Loop
  57.     Set oFile = Nothing
  58.  
  59.     WshShell.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", strValue
  60.     WshShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters", 1, False
  61.     WScript.Sleep sleepTime
  62. Loop
  63.  
  64. Set WshShell = Nothing
  65. Set oFolder = Nothing
  66.  
Jan 5 '11 #1
0 5509

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Jennie Friesen | last post: by
3 posts views Thread by Voetleuce en f?nsievry | last post: by
2 posts views Thread by arwes | last post: by
15 posts views Thread by Papajo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.