中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

中国DOS联盟论坛
现在时间是 2026-06-14 21:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [求助]求一个定时更换桌面背景的批处理!
楼 主 [求助]求一个定时更换桌面背景的批处理! 发表于 2008-05-25 13:41 ·  中国 四川 成都 电信
新手上路
积分 14
发帖 5
注册 2008-04-13 06:51
UID 115743
性别 男
状态 离线
图片在本地上的,定时可以自己设置
比如说隔三十分钟更换一张桌面背景.
2 发表于 2008-05-26 23:55 ·  中国 重庆 奉节县 电信
中级用户
★★
积分 363
发帖 162
注册 2007-04-22 21:31
UID 86104
性别 男
状态 离线
作个记号,我也想做个批处理的
3 有人发过,楼主注意搜索,稍加修改即可。 发表于 2008-05-27 00:00 ·  中国 重庆 电信
版主
★★★★★
积分 9,023
发帖 5,017
注册 2007-05-31 19:39
UID 89899
性别 男
状态 离线
随机选取图片作为桌面背景的实现

因为自己希望定期从指定目录及其子目录下的JPG文件随机选取一张作为桌面的背景图,而且希望使用居中不拉伸的样式显示,经过多次尝试发现微软提供的BGSWITCH插件未能满足我的要求(每次都会将图片拉伸到满屏),参考论坛上前辈的范例后自己动手写了一个CMD的批处理和一个VBS的脚本,其中VBS的脚本主要是调用WIA的控件将JPG文件转换成BMP文件。

1、setmybg.cmd
@echo off
rem 使用变量延迟扩展的语句
rem setlocal EnableDelayedExpansion

copy nul %~dp0list.txt > nul
for /f "usebackq delims=" %%i in ('dir/a-d/b/s %1') do call set $%%random%%$%%i=$
for /f "tokens=1,2* delims=$=" %%i in ('set $') do echo %%i/%%j >> %~dp0list.txt
for /f "tokens=1,* delims==" %%i in ('set $') do call set %%i=

set /a n=1

rem 使用变量延迟扩展的语句
rem for /f "tokens=1,2* delims=/" %%i in ('sort /R %~dp0list.txt') do if !n! equ 1 ( set bgfile=%%j && set /a n=!n!+1 ) else ( goto end )
for /f "tokens=1,2* delims=/" %%i in ('sort /R %~dp0list.txt') do if %n% equ 1 ( set bgfile=%%j && goto STARTCONVERT )
goto :end

:STARTCONVERT
if not exist %~dp0wallpaper1.bmp goto SETBG
del /f /q %~dp0wallpaper1.bmp

:SETBG
cscript %~dp0convert-bmp.vbs %bgfile% "%~dp0wallpaper1.bmp" > nul
reg add "HKCU\Control Panel\Desktop" /v Wallpaper /d "%~dp0Wallpaper1.bmp" /f > nul
reg add "HKCU\Control Panel\Desktop" /v WallpaperStyle /d "0" /f > nul
reg add "HKCU\Control Panel\Desktop" /v TileWallpaper /d "0" /f > nul
RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters > nul

:end
del /f /q %~dp0list.txt

rem 使用变量延迟扩展的语句
rem endlocal

2、convert-bmp.vbs
Dim oImg '图片文件
Dim oIP '图片处理

Set objArgs = WScript.Arguments
sOrgImg = objArgs(0)
sDesImg = objArgs(1)

Set oImg = CreateObject("WIA.ImageFile")
Set oIP = CreateObject("WIA.ImageProcess")

oImg.LoadFile sOrgImg '载入原始图片

oIP.Filters.Add oIP.FilterInfos("Convert").FilterID
oIP.Filters(1).Properties("FormatID").Value = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}" '这里的格式可以自行设定,见附表
'oIP.Filters(1).Properties("Quality").Value = 5 '图片质量

Set oNewImg = oIP.Apply(oImg) '应用

oNewImg.SaveFile sDesImg '保存转换后的图片


使用的时候将两个文件保存在同一个目录,运行setmybg.cmd并提供保存JPG文件的目录作为参数,例如:
setmybg.cmd d:\photo
如果希望选择特定文件名格式的,可以这样:
setmybg.cmd d:\photo\*0709*JPG

希望能帮到有需要的朋友!
4 发表于 2008-05-27 00:16 ·  中国 重庆 电信
初级用户
积分 45
发帖 19
注册 2007-03-08 02:14
UID 81032
性别 男
状态 离线
我也来一个,同样也是参考了以前的帖子,不过借助了外部工具jpg2bmp.exe来转换图片,需要将jpg2bmp.exe放在同一目录下,还是楼上的方便,可惜我不会vbs

@echo off
::下面设置墙纸的路径
set WallPaper_Path=F:\My Documents\My Pictures\cartoon\Lucky WallPaper\


setlocal ENABLEDELAYEDEXPANSION
del "%APPDATA%\Microsoft\Wallpaper1.bmp"
for /f %%i in ('dir "%WallPaper_Path%" /b^|find /C ".jpg"') do set file_num=%%i
set /a skip_line=%random%%%%file_num%
for /f "skip=%skip_line% tokens=*" %%i in ('dir "%WallPaper_Path%*.jpg" /b') do jpg2bmp "%WallPaper_Path%%%i" "%APPDATA%\Microsoft\Wallpaper1.bmp" && call :apply
goto :eof

:apply
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /d "%APPDATA%\Microsoft\Wallpaper1.bmp" /f
RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters
exit


jpg2bmp下载


另外想问一下楼上的“%~dp0”和"$%%random%%$%%i=$"中的"$"是什么

[ Last edited by gahoo on 2008-5-27 at 01:00 AM ]
5 4楼 发表于 2008-05-27 00:53 ·  中国 重庆 电信
版主
★★★★★
积分 9,023
发帖 5,017
注册 2007-05-31 19:39
UID 89899
性别 男
状态 离线
for /?
The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
6 发表于 2008-05-27 01:03 ·  中国 重庆 电信
初级用户
积分 45
发帖 19
注册 2007-03-08 02:14
UID 81032
性别 男
状态 离线
楼上也是夜猫……
明白了,可是“0”的意义呢
“set $%%random%%$%%i=$"中的"$"是什么
7 发表于 2008-05-27 01:11 ·  中国 四川 成都 电信
中级用户
★★
积分 297
发帖 135
注册 2006-10-21 12:00
UID 67627
性别 男
状态 离线
%~dp0参见call /?

$一个普通字符而已,在左边作为变量名的一部分,在右边作为变量的值

[ Last edited by zqz0012005 on 2008-5-27 at 01:12 AM ]
8 发表于 2008-05-27 09:12 ·  中国 上海 联通
版主
★★★★★
积分 9,023
发帖 5,017
注册 2007-05-31 19:39
UID 89899
性别 男
状态 离线
%0代表批处理文件本身
试试就知道了

@echo off
echo %0
9 已经做好,自动定时更换壁纸批处理版 发表于 2008-05-28 13:22 ·  中国 重庆 联通
中级用户
★★
积分 363
发帖 162
注册 2007-04-22 21:31
UID 86104
性别 男
状态 离线
10 看了 发表于 2008-05-30 01:21 ·  中国 广西 南宁 电信
新手上路
积分 14
发帖 7
注册 2008-05-29 23:47
UID 120044
性别 男
状态 离线
谢谢
论坛跳转: