Using DOS command to Auto-Pick today’s wallpaper

I wish this image converter tool can support command-line mode, so I could make my little tiny wallpaper changer bat file more powerful.

::== randomSetWallpaper.bat
@echo off
setLocal EnableDelayedExpansion
set ImageDir=”I:\My Documents\My Pictures\wallpaper\”
set ImageList=!ImageDir!”*.bmp”
:: This might be different on each machine.
set ImageOfToday=”C:\Documents and Settings\TEMP\Local Settings\Application Data\Microsoft\Wallpaper1.bmp”
for /f %%A in (‘dir /b !ImageList!^|find /v /c “\”‘) do set C=%%A
::set R=!random:~-3!
:: %RANDOM% – expands to a random decimal number between 0 and 32767.
:: Here we only need 3 digitals, the tolal number of files won’t be greater than 999.
set R=!random:~-100!
if !R! gtr !C! goto :loop
if !R! equ 0 goto :loop
for /f “tokens=*” %%A in (‘dir /b !ImageList!’) do (
set /a N+=1
rem Step by step to find the marked point. !R! ( !N! is adcting as an indicator.)
rem The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated.
if !N! equ !R! copy !ImageDir!%%A !ImageOfToday! && goto :eof
rem an extra doulbe quote seems OK here.
goto :eof

It works, but, what a pity I had to convert all the images to bmp format in advance!

Another drawback is, this NEW wallpaper won’t be in effect until next time login, because I don’t know how to refresh the desktop settings.

The purpose of ‘setLocal EnableDelayedExpansion‘:

if you want the variable to be expanded at execution time rather than at parse time? For that, you use “delayed expansion”, which is enabled by the /V command line option or by using the SETLOCAL ENABLEDELAYEDEXPANSION command in a batch file.

-from The old new thing.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s