Wscript to batch edit desktop.ini for multiple folders

1.8k views Asked by At

I'm looking to create a batch windows script file to define the icon location of a folder, in the folders desktop.ini

Basically, I have over 400 videos in my media drive. Each has a icon file in it with the same name as the folder for example:

E:\Movies\Movie (2010) --> Folder Name E:\Movies\Movie (2010).ico --> Icon File Name

Initially I went about right clicking each folder and customizing the icon, however recently I changed PC's and attached the HDD in the new PC and all the folders icons went to default. If there is a possible solution, I would really be grateful.

PS the last time I did any sort of coding was the era of MS-DOS batch files :)

1

There are 1 answers

2
loadingnow On

Put this in a vbs file and run in the cmd with the argument being the folder the folders are in

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

Notes:

  1. I got the text in desktop.ini file from a test folder in a Windows 7 com. I am not sure if it will work on other versions of windows.
  2. I have tested the code but the icons only update after some time and some refreshes

EDIT: This one works instantly (and the code is much nicer):

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