起動ごとに壁紙をランダムに変更するためのバッチスクリプト。

警告

注意

  • 壁紙画像に使われるのはbmp ファイルのみです。
  • ディレクトリも含めて、指定ディレクトリ配下のbmp ファイルが全て対象となります。

スクリプト

@echo off
setlocal ENABLEDELAYEDEXPANSION
REM ====================
REM 起動ごとに壁紙をランダムに変更するためのバッチスクリプト。
REM 壁紙画像に使われるのはbmp ファイルのみ。
REM --------------------
REM @author 金魚屋・J・龍角
REM @copyright 金魚屋見聞録
REM @url http://www.kingyoya.org/
REM @date 2008-07-05
REM @version 1.0.0
REM ====================
REM はじめに
REM --------------------
REM このスクリプトはレジストリを直接書き替えます。
REM このスクリプトを利用することによって起こったあらゆることについて、作者はあらゆる責任も負いません。
REM このスクリプトの利用に特に制限はありません。
REM WindowsXP で動作確認しています。
REM マイドキュメントの位置やプロファイルの設定を変更している場合は利用できません。
REM ====================
REM 使い方
REM --------------------
REM 1. スタートアップなどに、スクリプトへのショートカットを定義する
REM 2. 必要であればショートカットのリンク先で、画像を検索するディレクトリを引数指定する。
REM ====================
REM 画像検索ディレクトリ
REM --------------------
REM 1. 引数が指定されている場合
REM その引数を画像のディレクトリパスとする
REM 2. 引数が指定されていない場合
REM ログインユーザの\My Documents\My Pictures ディレクトリ
REM ====================
REM 検索される画像
REM --------------------
REM 画像検索ディレクトリ配下にある、bmp ファイルを再帰的に検索する。
REM ====================
REM 
REM ====================
REM 定義
REM --------------------
SET regkey=HKEY_CURRENT_USER\Control Panel\Desktop
SET imagedir=%USERPROFILE%\My Documents\My Pictures
REM ====================

SET arg=%~1
if "%arg%" NEQ "" SET imagedir=!arg!

REM --------------------
REM ディレクトリ検証
REM --------------------
if not exist "%imagedir%\*" goto errordir
echo 画像ディレクトリ:%imagedir%

REM --------------------
REM 画像ファイル数を取得
REM --------------------
set count=0
for /R "%imagedir%" %%f in ("*.bmp") do (
	set /A count=count+1
)
if !count! LEQ 0 goto noimages
echo 画像ファイル数:%count%

REM --------------------
REM 乱数生成
REM --------------------
set number=%time: =0%
set number=%number::=%
set number=%number:~4,2%
set /A number=%RANDOM% %% %count%

if !number! GEQ !count! goto errorcount1
if !number! LSS 0 goto errorcount2
echo 乱数:%number%

REM --------------------
REM 対象画像を決定
REM --------------------
set /A count=0
set target=

for /R "%imagedir%" %%f in ("*.bmp") do (
	if !count! == %number% (
		set target=%%f
		goto setwallpaper
	)
	set /A count=count+1
)
goto errortarget

REM --------------------
REM 壁紙を設定
REM --------------------
:setwallpaper
if "%target%" == "" goto errortarget
echo 壁紙:%target%
REG ADD "%regkey%" /v Wallpaper /t REG_SZ /d "%target%" /f

goto end

REM --------------------
REM 終了処理
REM --------------------
:errordir
echo 画像ディレクトリが見つかりません。

goto errorend

:noimages
echo 画像が見つかりません

goto errorend

:errorcount1
echo カウントエラー1

goto errorend

:errorcount2
echo カウントエラー2

goto errorend

:errortarget
echo 壁紙の取得に失敗

goto errorend

:errorend
echo 壁紙の変更に失敗しました。(%imagedir%)
pause

:end

endlocal