3

我正在寻找创建一个批处理 windows 脚本文件来定义文件夹的图标位置,在文件夹 desktop.ini

基本上,我的媒体驱动器中有 400 多个视频。每个都有一个与文件夹同名的图标文件,例如:

E:\Movies\Movie (2010) --> 文件夹名称 E:\Movies\Movie (2010).ico --> 图标文件名

最初我右键单击每个文件夹并自定义图标,但最近我更改了 PC 并将 HDD 连接到新 PC 中,所有文件夹图标都变为默认值。如果有可能的解决方案,我将不胜感激。

PS我最后一次做任何类型的编码是MS-DOS批处理文件的时代:)

4

1 回答 1

3

将其放入 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

笔记:

  1. 我从 Windows 7 com 的测试文件夹中获得了 desktop.ini 文件中的文本。我不确定它是否适用于其他版本的 Windows。
  2. 我已经测试了代码,但图标仅在一段时间后更新并进行了一些刷新

编辑:这个可以立即工作(并且代码更好):

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
于 2015-06-12T14:34:50.333 回答