标题: [文本处理] 如何通过批处理修改桌面url快捷方式的图标? [打印本页]
作者: 如你是我 时间: 2024-4-15 16:45 标题: 如何通过批处理修改桌面url快捷方式的图标?
比如我桌面有个百度的网页快捷方式
我想通过命令修改这个快捷方式的图标为%USERPROFILE%\Pictures\baidu.ico而不是默认的%SystemRoot%\System32\SHELL32.dll
怎么通过命令修改呢,而不是删了再建,因为网页快捷方式是从共享拷出来的,可能随时会换成别的网址
作者: czjt1234 时间: 2024-4-15 19:11
- url文件是个文本文件,可以用记事本打开并编辑
-
- 文件中,与快捷键和图标相关的行:
- IconFile 表示图标文件
- IconIndex 如果图标文含有多个图标,IconIndex表示图标的序号,第一个图标的序号为0
复制代码
好象可以直接按文本文件处理
作者: 如你是我 时间: 2024-4-16 09:52
回复 2# czjt1234
用echo会添加iconfile 但是原来的iconfile还在 就没有替换成
作者: czjt1234 时间: 2024-4-17 10:45
- rem 另存为 ANSI 编码 bat
- ' & cls & cscript.exe /nologo /e:vbscript "%~f0" & pause & exit
-
- file = "百度.url"
- icon = "%USERPROFILE%\Pictures\baidu.ico"
-
- Call write_ini(file, , "InternetShortcut", "IconFile", icon)
- Call write_ini(file, , "InternetShortcut", "IconIndex", "0")
- Set oFSO = CreateObject("Scripting.FileSystemObject")
- Set oWshShell = CreateObject("WScript.Shell")
- Set oWshUrlShortcut = oWshShell.CreateShortcut(file)
- s = oWshUrlShortcut.TargetPath
- oWshUrlShortcut.TargetPath = s
- oWshUrlShortcut.Save()
-
- Function write_ini(ByVal file, ByVal charset, ByVal section, ByVal key, ByRef value)
- Dim oFSO, oStream, oRegExp, oMatches, sINI, s, i, m, b, t, LineSeparator
- Set oFSO = CreateObject("Scripting.FileSystemObject")
- Set oStream = CreateObject("ADODB.Stream")
- oStream.Type = 2 'adTypeText
- oStream.Mode = 3 'adModeReadWrite
- Set oRegExp = New RegExp
- oRegExp.Global = True
- oRegExp.MultiLine = True
- If VarType(charset) = vbError Then charset = "GB2312" '缺省参数
- On Error Resume Next
-
- If (VarType(file) <> vbString) Or _
- (VarType(section) <> vbString) Or _
- (VarType(key) <> vbString And VarType(key) <> vbNull) Or _
- (VarType(value) <> vbString And VarType(value) <> vbNull) Then _
- write_ini = "至少有一个参数的类型错误" : Exit Function
- If Left(section, 1) <> "[" Then section = "[" & section
- If Right(section, 1) <> "]" Then section = section & "]" 'section参数带不带[]都可以
- Err.Clear()
- oStream.Charset = charset
- If Err.Number <> 0 Then write_ini = Err.Description : Exit Function
- If Not oFSO.FileExists(file) Then '文件不存在
- oStream.Open()
- oStream.WriteText section
- Err.Clear()
- oStream.SaveToFile file '新建文件
- If Err.Number <> 0 Then write_ini = "新建文件失败" : Exit Function
- oStream.Close()
- End If
- oStream.Open()
- Err.Clear()
- oStream.LoadFromFile file
- If Err.Number <> 0 Then write_ini = "读取文件失败" : Exit Function
- sINI = oStream.ReadText() '读取文件内容
- oStream.Close()
-
- LineSeparator = vbNewLine '兼容各种分行符
- If InStr(sINI, vbLf) > 0 Then LineSeparator = vbLf
- If InStr(sINI, vbCr) > 0 Then LineSeparator = vbCr
- If InStr(sINI, vbCrLf) > 0 Then LineSeparator = vbCrLf
- sINI = sINI & LineSeparator '添加分行符,防止最后一行不匹配正则表达式
- oRegExp.Pattern = "^(.+)\r"
- If LineSeparator = vbLf Then oRegExp.Pattern = "^(.+)\n"
-
- m = -1 '用于标志小节名是否存在
- Set oMatches = oRegExp.Execute(sINI) '兼容小节名含有特殊字符的情况,兼容其它行含小节名的情况
- For i = 0 To oMatches.Count - 1
- If LCase(oMatches.Item(i).SubMatches.Item(0)) = LCase(section) Then m = i : Exit For
- Next
- If m = -1 Then sINI = sINI & section & LineSeparator '小节名不存在,在文末添加小节名
-
- '目标小节已存在,开始处理键名键值
- b = False 'False非目标小节,True是目标小节
- s = 0 '0未处理目标小节,1已处理目标小节未处理目标键名,2已处理目标小节和目标键名
- t = "" '新的sINI文本
- Set oMatches = oRegExp.Execute(sINI)
- For i = 0 To oMatches.Count - 1
- m = oMatches.Item(i).SubMatches.Item(0)
- If LCase(m) = LCase(section) Then '目标小节
- b = True
- s = 1
- If VarType(key) <> vbNull Then t = t & m & LineSeparator
- ElseIf Left(m, 1) = "[" And Right(m, 1) = "]" Then '其它小节
- b = False
- If s = 1 Then
- If VarType(value) <> vbNull And VarType(key) <> vbNull Then
- t = t & key & "=" & value & LineSeparator & m & LineSeparator
- Else
- t = t & m & LineSeparator
- End If
- s = 2
- Else
- t = t & m & LineSeparator
- End If
- Else '键名键值行、注释行
- If b = False Then '非目标小节
- t = t & m & LineSeparator
- Else '目标小节
- If VarType(key) = vbNull Then '删除目标小节
- s = 2
- ElseIf LCase(Split(m ,"=")(0)) = LCase(key) Then '目标键名
- If VarType(value) = vbNull Then '删除目标键名
- Else
- t = t & key & "=" & value & LineSeparator '修改键值
- End If
- s = 2
- Else '非目标键名
- t = t & m & LineSeparator
- End If
- End If
- End If
- Next
- If s = 1 And VarType(value) <> vbNull And VarType(key) <> vbNull Then '新建目标键名
- t = t & key & "=" & value & LineSeparator
- End If
-
- t = RePlace(t, LineSeparator & "[", LineSeparator & LineSeparator & "[")
- oStream.Open()
- oStream.WriteText t
- Err.Clear()
- oStream.SaveToFile file, 2 'adSaveCreateOverWrite 覆盖写入文件
- If Err.Number <> 0 Then write_ini = "写入文件失败" : Exit Function
- oStream.Close()
- write_ini = "写入成功" & Len(t) & "字符" & LineSeparator & t
- End Function
复制代码
作者: aloha20200628 时间: 2024-4-17 19:25
本帖最后由 aloha20200628 于 2024-4-17 19:43 编辑
回复 1# 如你是我
用批处理脚本修改url快捷方式图标不同于lnk快捷方式图标,后者能即时更新,而前者似乎须重启资源管理器方可生效。楼主可试试以下批处理脚本...- @echo off &setlocal
- set/p "sc=从桌面中拖入一个url快捷方式:" &if not defined sc exit/b
- set "sc=%sc:"=%"
- if /i "%sc:~-4%" neq ".url" exit/b
- set/p "ip=拖入一个新的图标文件:" &if not defined ip exit/b
- set "ip=%ip:"=%"
- echo,[InternetShortcut]>"_sc.tmp"
- (for /f "usebackq skip=1 tokens=1* delims==" %%a in ("%sc%") do (
- if /i "%%a"=="iconfile" (echo,%%a="%ip%") else if /i "%%a"=="iconindex" (echo,%%a=0) else echo,%%a=%%b
- ))>>"_sc.tmp"
- (move /y "_sc.tmp" "%sc%"
- taskkill /f /im explorer.exe & start explorer.exe)>nul 2>nul
- exit/b
复制代码
备注》重启资源管理器会重排桌面所有快捷方式图标,若当前未采用“自动排列图标”布局,则在运行本脚本时须预先取消勾选“自动排列图标”(桌面空白处右键菜单>查看>自动排列图标)
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |