Tags

, ,


I use VBScript a lot to automate installation of software via SCCM on remote machines. Sometimes I have to deal with removing icons that may or may not be sitting on individual users’ Desktops. Or maybe something has to be removed from their %AppData%. I do like to be thorough and leave the PC estate consistent irrespective of what might have come before.

As a result, my install VBScripts sometimes have to parse all local userprofiles and check each in turn. I was re-thinking this today now that we have lots of Windows 7 machines but still some XP.

For example, say I need to remove something from the AppData folder of every user account on a machine:
I could query the filesystem to find the profilesroot path (eg C:\Users) but better still is to use the official registry key.
Then as I look at each subfolder (ie profile), what should I append to that folder path to get the AppData folder for that user? I could do an OS version check and hard-code the path suffix “AppData\Roaming” or “Application Data” but there must be a better way that is not making assumptions abut the future. (Although, I am of course assuming that these profiles are never going to be roaming!)

Here’s what I came up with:

Const ssfPROFILE = &H28
Const ssfLOCALAPPDATA = &H1C
Const ssfAPPDATA = &H1A
Const ssfDESKTOPDIRECTORY = &H10
Const ssfSTARTMENU = &H0B
Const ssfSTARTUP = &H07

Set objShellApp = CreateObject("Shell.Application")

strProfile = objShellApp.NameSpace(ssfPROFILE).Self.Path
strLocalAppDataPathSuffix = Replace(objShellApp.NameSpace(ssfLOCALAPPDATA).Self.Path, strProfile, "")
strAppDataPathSuffix = Replace(objShellApp.NameSpace(ssfAPPDATA).Self.Path, strProfile, "")
strDesktopPathSuffix = Replace(objShellApp.NameSpace(ssfDESKTOPDIRECTORY).Self.Path, strProfile, "")
strStartMenuPathSuffix = Replace(objShellApp.NameSpace(ssfSTARTMENU).Self.Path, strProfile, "")
strStartupPathSuffix = Replace(objShellApp.NameSpace(ssfSTARTUP).Self.Path, strProfile, "")

Now as I parse each profile, I can just append the relevant suffix as calculated above.

Advertisements