开发者

Windows batch script to switch desktop background

Is there a way to check what a user currently has as their background and then changing it depending on what it is? For example: I want a white background during day time and a black background for night time. Running the script would check the current background, if it is white it will switch to the black background, and if it is black i开发者_StackOverflow中文版t will switch to the white.

I'm a little unfamiliar with Windows batch script and I'm seeking some tips and advice on how I can accomplish the task above. Here is what I've been able to find so far:

@echo off
call :quiet>nul 2>&1
goto :EOF

:quiet


:: Configure Wallpaper 
REG ADD "HKCU\Control Panel\Desktop" /V Wallpaper /T REG_SZ /F /D "%SystemRoot%\energybliss.bmp"
REG ADD "HKCU\Control Panel\Desktop" /V WallpaperStyle /T REG_SZ /F /D 0
REG ADD "HKCU\Control Panel\Desktop" /V TileWallpaper /T REG_SZ /F /D 2


:: Configure the screen saver.
:: REG ADD "HKCU\Control Panel\Desktop" /V SCRNSAVE.EXE /T REG_SZ /F /D "%SystemRoot%\System32\scrnsave.scr"
:: REG ADD "HKCU\Control Panel\Desktop" /V ScreenSaveActive /T REG_SZ /F /D 1


:: Set the time out to 900 seconds (15 minutes).
:: REG ADD "HKCU\Control Panel\Desktop" /V ScreenSaveTimeOut /T REG_SZ /F /D 900


:: Set the On resume, password protect box 
:: REG ADD "HKCU\Control Panel\Desktop" /V ScreenSaverIsSecure /T REG_SZ /F /D 1


:: Remove the user's ability to see the Screen Saver, background, and appearance tabs of Display Properties. 
::REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System" /V NoDispScrSavPage /T REG_DWORD /F /D 1
::REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System" /V NoDispBackgroundPage /T REG_DWORD /F /D 1
::REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System" /V NoDispAppearancePage /T REG_DWORD /F /D 1

:: Make the changes effective immediately
%SystemRoot%\System32\RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters


You can use code like that:

@echo off

:: '>nul 2>&1' was moved to other place
call :quiet
exit /b

:quiet
    :: Put there wallpaper name (with extension, bigger that 8 symbols)
    set "Wallpaper.Night.BadWrited=Wallpaper1.bmp"

    :: It is a dirty hack and example of bad code
    for /F "tokens=*" %%a in ('reg query "HKCU\Control Panel\Desktop" /v Wallpaper') do     set "Wallpaper.Current.BadWrited=%%a"

    :: Take last 8 symbols of wallpaper name. Change number of symbols to your own  minimal
    set "Wallpaper.Current.BadWrited=%Wallpaper.Current.BadWrited:~-8%"
    set "Wallpaper.Night.BadWrited=%Wallpaper.Night.BadWrited:~-8%"

    if "%Wallpaper.Current.BadWrited%"=="%Wallpaper.Night.BadWrited%" (
        call :MakeDayWallpaper>nul 2>&1
    ) else (
        call :MakeNightWallpaper>nul 2>&1
    )
exit /b

:MakeDayWallpaper
    echo Day wallpaper setted
    :: Put your code here
exit /b

:MakeNightWallpaper
    echo Night wallpaper setted 
    :: Put your code here
exit /b

But i recommend to use the system scheduler. You can acces it from control panel, 'Scheduled Tasks' or something. You can make 2 files named 'makeday.bat' and 'makenight.bat'. Scheduler will run them every day at needed time

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜