Changing Icons with VBScript on Windows XP

Yet another pesky problem caused by going from Office 2003 to Office 2007.¬† We had some Access databases lying around with shortcuts with command lines like “C:Program FilesMicrosoft OfficeOFFICE11msaccess.exe Z:BillyBobDatabase.mdb”.¬† Obviously, these will break after the upgrade, but I noticed during the¬†install the Office 2007¬†installer was actually deleting the icons completely!¬† Direct links to MDB files were not affected, just those that specifically contained C:Program FilesMicrosoft OfficeOFFICE11msaccess.exe in the command line.

The problem lies in the fact that the icon could be placed on the user’s desktop or in all users’ desktop. Some PCs are multi-user, so you could have the icon on several profile¬†desktops.¬† What to do?

I found this VBScript that was originally designed to do a recursive file delete.  I changed it to do a recursive file replace instead:

START_FOLDER = “C:documents and settings”
ECRDATABASE = “ECR Database.lnk”
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
ProcessSubFolders oFSO.GetFolder(START_FOLDER)
Sub ProcessSubFolders(oFolder)
Set cFiles = oFolder.Files
For Each oFile In cFiles
If Right(oFile.Name, Len(ECRDATABASE)) = ECRDATABASE Then
TruncatedFilePath = Left(oFile, Len(oFile) – Len(ECRDATABASE))
oFSO.CopyFile “ECR Database.lnk”, TruncatedFilePath
If oFSO.FolderExists(“C:Program Files (x86)Microsoft OfficeOFFICE11”) Then
oFSO.CopyFile “ECR Database x86.lnk”, TruncatedFilePath
oFSO.DeleteFile TruncatedFilePath & ECRDATABASE
End If
End If
Next
For Each oSubFolder In oFolder.SubFolders
ProcessSubFolders oSubFolder
Next
End Sub

This is a very “expensive” script that will hit about every file in all user’s profiles.¬† That means that if you have 20,000+ files in Internet caches, multiple user profiles and a slow PC, it could take 30+ minutes for the script to finish.¬† Since I was running this script¬†off hours, I didn’t care about I/O or CPU¬†time.

The last part of the script is designed for 64-bit machines.  If C:Program Files (x86) exists, we are on a x64 machine and therefore we copy the x64 version of the icon.

– Soli Deo Gloria