将其放入 vbs 文件并在 cmd 中运行,参数为文件夹所在的文件夹
EditDesktopIni(Wscript.Arguments(0))
Sub EditDesktopIni(foldpath)
Dim fso, inifile, icondata, file, fold, subfold, item
Const ForReading = 1, ForWriting = 2
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder(foldpath)
Set subfold = fold.SubFolders
For Each item In subfold
If (fso.FileExists(foldpath + "\" + item.Name + "\desktop.ini")) Then 'If desktop.ini exists, delete it
fso.DeleteFile foldpath + "\" + item.Name + "\desktop.ini", True
End If
Set file = fso.OpenTextFile(foldpath + "\" + item.Name + "\desktop.ini", ForWriting, True, TristateUseDefault)
file.WriteLine "[.ShellClassInfo]"
file.WriteLine "IconResource=" & foldpath & "\" & item.name & ".ico" & ",0"
file.WriteLine "[ViewState]"
file.WriteLine "Mode="
file.WriteLine "Vid="
file.WriteLine "FolderType=Generic"
file.WriteLine ""
fso.GetFile(foldpath + "\" + item.Name + "\desktop.ini").Attributes = 6
Next
End Sub
笔记:
- 我从 Windows 7 com 的测试文件夹中获得了 desktop.ini 文件中的文本。我不确定它是否适用于其他版本的 Windows。
- 我已经测试了代码,但图标仅在一段时间后更新并进行了一些刷新
编辑:这个可以立即工作(并且代码更好):
EditDesktopIni(Wscript.Arguments(0))
Sub EditDesktopIni(foldpath)
Dim fso, inifile, icondata, file, fold, subfold, item, subfoldpath
Const ForReading = 1, ForWriting = 2
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder(foldpath)
Set subfold = fold.SubFolders
For Each item In subfold
inifile = foldpath + "\" + item.Name + "\desktop.ini"
subfoldpath = foldpath & "\" & item.name
If (fso.FileExists(inifile)) Then 'If desktop.ini exists, delete it
fso.DeleteFile inifile, True
End If
Set file = fso.OpenTextFile(inifile, ForWriting, True, TristateUseDefault)
file.WriteLine "[.ShellClassInfo]"
file.WriteLine "IconResource=" & subfoldpath & ".ico" & ",0"
file.WriteLine "[ViewState]"
file.WriteLine "Mode="
file.WriteLine "Vid="
file.WriteLine "FolderType=Generic"
file.WriteLine ""
fso.GetFile(inifile).Attributes = 6
item.Attributes = 0
item.Attributes = 4 'Set the folder to system to refresh the icon
Next
End Sub