Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wallpapr.bat

(view source code of wallpapr.bat as plain text)

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" (
  3. 	ECHO.
  4. 	ECHO This batch file will only work on Windows 2000 and later.
  5. 	GOTO Syntax
  6. )
  7. VER | FIND "Windows NT" >NUL
  8. IF NOT ERRORLEVEL 1 (
  9. 	ECHO.
  10. 	ECHO This batch file will only work on Windows 2000 and later.
  11. 	GOTO Syntax
  12. )
  13. FINDSTR.EXE /? >NUL 2>&1
  14. IF ERRORLEVEL 1 (
  15. 	ECHO.
  16. 	ECHO FINDSTR.EXE not found
  17. 	GOTO Syntax
  18. )
  19. IF NOT "%~2"=="" GOTO Syntax
  20. ECHO "%~1" | FIND "?" >NUL
  21. IF NOT ERRORLEVEL 1 GOTO Syntax
  22. ECHO "%~1" | FIND "/" >NUL
  23. IF NOT ERRORLEVEL 1 GOTO Syntax
  24.  
  25. SETLOCAL
  26. :: Define direcory where bitmaps are located
  27. SET WorkingDir=%~1
  28. IF NOT DEFINED WorkingDir SET WorkingDir=%windir%
  29.  
  30. :: Count number of read-only bitmap files
  31. FOR /F "tokens=1 delims=:" %%A IN ('ATTRIB.EXE "%WorkingDir%.\*.bmp" 2^>NUL ^| FINDSTR.EXE /R /B /C:".....R" ^| FINDSTR.EXE /N "R"') DO SET NumPics=%%A
  32. IF NOT DEFINED NumPics (
  33. 	ECHO.
  34. 	ECHO No read-only bitmap files found in %WorkingDir%
  35. 	GOTO Syntax
  36. )
  37.  
  38. :: Calculate random to number of bitmaps factor
  39. SET /A Factor = 32768 / %NumPics%
  40.  
  41. :: Make variable Random really random by using a more or less random "seed"
  42. FOR /F "tokens=* delims=" %%A IN ('VER ^| TIME ^| FINDSTR.EXE /R /C:"[0-9]"') DO SET TimeStr=%%A
  43. SET TimeStr=%TimeStr::= %
  44. SET TimeStr=%TimeStr:.= %
  45. SET TimeStr=%TimeStr:,= %
  46. FOR %%A IN (%TimeStr%) DO SET Seed=%%A
  47. SET /A Seed = %Seed% + 2
  48. FOR /L %%A IN (0,1,%Seed%) DO CALL SET Dummy=%%Random%%
  49.  
  50. :Again
  51. :: Select a random bitmap from the "array"
  52. SET /A PicsIndex = %Random% / %Factor% + 1
  53. :: Force a new selection on array out of bound errors
  54. IF %PicsIndex% GTR %NumPics% GOTO Again
  55. IF %PicsIndex% LSS         1 GOTO Again
  56.  
  57. :: Make the randomly selected bitmap file the current user's wallpaper
  58. FOR /F "tokens=1* delims=:" %%A IN ('ATTRIB.EXE %WorkingDir%.\*.bmp ^| FINDSTR.EXE /R /B /C:".....R" ^| FINDSTR.EXE /N "R"') DO (
  59. 	IF %PicsIndex% EQU %%A CALL :MakeWallpaper "%%~B"
  60. )
  61. ENDLOCAL
  62. GOTO:EOF
  63.  
  64.  
  65. :MakeWallpaper
  66. :: Extract bitmap file name from line
  67. SET PicsChoice=%~1
  68. SET PicsChoice=%PicsChoice:~11%
  69. :: Display wallpaper file name
  70. ECHO New wallpaper bitmap: %PicsChoice%
  71. :: Create, merge and then delete temporary REG file
  72. :: to change the Wallpaper setting in the registry
  73. SET PicsChoice=%PicsChoice:\=/%
  74. SET PicsChoice=%PicsChoice:/=\\%
  75. >  "%Temp%.\wallpaper.dat" ECHO REGEDIT4
  76. >> "%Temp%.\wallpaper.dat" ECHO.
  77. >> "%Temp%.\wallpaper.dat" ECHO [HKEY_CURRENT_USER\Control Panel\Desktop]
  78. >> "%Temp%.\wallpaper.dat" ECHO "Wallpaper"="%PicsChoice%"
  79. >> "%Temp%.\wallpaper.dat" ECHO.
  80. START /WAIT REGEDIT.EXE /S "%Temp%.\wallpaper.dat"
  81. DEL "%Temp%.\wallpaper.dat"
  82. :: Activate the changes; thanks for Pete Smith for this RUNDLL32 command
  83. RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True
  84. GOTO:EOF
  85.  
  86.  
  87. :Syntax
  88. ECHO.
  89. ECHO Wallpapr.bat,  Version 1.00 for Windows 2000 and later
  90. ECHO Randomly change Windows' background bitmap
  91. ECHO.
  92. ECHO Usage:  WALLPAPR  [ working_dir ]
  93. ECHO.
  94. ECHO Where:  "working_dir" is the directory where the bitmap files
  95. ECHO                       are located (default: %Windir%)
  96. ECHO.
  97. ECHO Note:   Each time this batch file is run, a random selection
  98. ECHO         will be made from a list of READ-ONLY bitmap files
  99. ECHO         in the working directory.
  100. ECHO.
  101. ECHO Based on a RUNDLL32 command by Pete Smith
  102. ECHO Written by Rob van der Woude
  103. ECHO http://www.robvanderwoude.com
  104.  

page last uploaded: 2017-07-06, 12:37