ファイルのコピーや移動を行う場合に特殊フォルダーのパスが必要になることがよくありますが、今回はVBScriptで特殊フォルダーのパスを取得する方法をまとめてみました。
ShellオブジェクトのNameSpaceメソッドを使う方法
ShellオブジェクトのNameSpaceメソッドを使うとFolderオブジェクトが取得できるので、そこから特殊フォルダーのパスを得ることができます。
Option Explicit 'ShellSpecialFolderConstants 'http://msdn.microsoft.com/en-us/library/windows/desktop/bb774096.aspx Private Const ssfDESKTOP = 0 Private Const ssfPROGRAMS = 2 Private Const ssfCONTROLS = 3 Private Const ssfPRINTERS = 4 Private Const ssfPERSONAL = 5 Private Const ssfFAVORITES = 6 Private Const ssfSTARTUP = 7 Private Const ssfRECENT = 8 Private Const ssfSENDTO = 9 Private Const ssfBITBUCKET = 10 Private Const ssfSTARTMENU = 11 Private Const ssfDESKTOPDIRECTORY = 16 Private Const ssfDRIVES = 17 Private Const ssfNETWORK = 18 Private Const ssfNETHOOD = 19 Private Const ssfFONTS = 20 Private Const ssfTEMPLATES = 21 Private Const ssfCOMMONSTARTMENU = 22 Private Const ssfCOMMONPROGRAMS = 23 Private Const ssfCOMMONSTARTUP = 24 Private Const ssfCOMMONDESKTOPDIR = 25 Private Const ssfAPPDATA = 26 Private Const ssfPRINTHOOD = 27 Private Const ssfLOCALAPPDATA = 28 Private Const ssfALTSTARTUP = 29 Private Const ssfCOMMONALTSTARTUP = 30 Private Const ssfCOMMONFAVORITES = 31 Private Const ssfINTERNETCACHE = 32 Private Const ssfCOOKIES = 33 Private Const ssfHISTORY = 34 Private Const ssfCOMMONAPPDATA = 35 Private Const ssfWINDOWS = 36 Private Const ssfSYSTEM = 37 Private Const ssfPROGRAMFILES = 38 Private Const ssfMYPICTURES = 39 Private Const ssfPROFILE = 40 Private Const ssfSYSTEMx86 = 41 'Private Const ssfPROGRAMFILESx86 = 48 '48だと管理ツール Private Const ssfPROGRAMFILESx86 = 42 'Program Filesフォルダーのパス表示 MsgBox CreateObject("Shell.Application").Namespace(ssfPROGRAMFILES).Self.Path
NameSpaceメソッドの説明ページにある通り、ShellSpecialFolderConstantsの値を引数にすることができますが、上記コード中にも書いてある通り、ssfPROGRAMFILESx86(0x30(48))を指定すると、得られるフォルダーはProgram Files (X86)ではなく管理ツールフォルダーになってしまうので、上記コードではssfPROGRAMFILESx86の値を「0x2A(42)」にしてあります(Error in MSDN documentation for ssfPROGRAMFILESx86 参照)。
また、ShellSpecialFolderConstantsのページを見ると、
The values in this enumeration are equivalent to their corresponding CSIDL or KNOWNFOLDERID values, used in C++ applications. They supersede the use of environment variables for this purpose. Note that not all CSIDL or KNOWNFOLDERID values have an equivalent value in ShellSpecialFolderConstants.
とあるので、フォルダーのパスを得るのにCSIDL値を用いることも(すべてのCSIDL値が使えるわけでは無いにしても)できるようです。
Option Explicit 'CSIDL値(ShlObj.hより) Private Const CSIDL_DESKTOP = 0 '<desktop> Private Const CSIDL_INTERNET = 1 'Internet Explorer (icon on desktop) Private Const CSIDL_PROGRAMS = 2 'Start Menu\Programs Private Const CSIDL_CONTROLS = 3 'My Computer\Control Panel Private Const CSIDL_PRINTERS = 4 'My Computer\Printers Private Const CSIDL_PERSONAL = 5 'My Documents Private Const CSIDL_FAVORITES = 6 '<user name>\Favorites Private Const CSIDL_STARTUP = 7 'Start Menu\Programs\Startup Private Const CSIDL_RECENT = 8 '<user name>\Recent Private Const CSIDL_SENDTO = 9 '<user name>\SendTo Private Const CSIDL_BITBUCKET = 10 '<desktop>\Recycle Bin Private Const CSIDL_STARTMENU = 11 '<user name>\Start Menu Private Const CSIDL_MYMUSIC = 13 'My Music folder Private Const CSIDL_MYVIDEO = 14 'My Videos folder Private Const CSIDL_DESKTOPDIRECTORY = 16 '<user name>\Desktop Private Const CSIDL_DRIVES = 17 'My Computer Private Const CSIDL_NETWORK = 18 'Network Neighborhood (My Network Places) Private Const CSIDL_NETHOOD = 19 '<user name>\nethood Private Const CSIDL_FONTS = 20 'windows\fonts Private Const CSIDL_TEMPLATES = 21 Private Const CSIDL_COMMON_STARTMENU = 22 'All Users\Start Menu Private Const CSIDL_COMMON_PROGRAMS = 23 'All Users\Start Menu\Programs Private Const CSIDL_COMMON_STARTUP = 24 'All Users\Startup Private Const CSIDL_COMMON_DESKTOPDIRECTORY = 25 'All Users\Desktop Private Const CSIDL_APPDATA = 26 '<user name>\Application Data Private Const CSIDL_PRINTHOOD = 27 '<user name>\PrintHood Private Const CSIDL_LOCAL_APPDATA = 28 '<user name>\Local Settings\Applicaiton Data (non roaming) Private Const CSIDL_ALTSTARTUP = 29 'non localized startup Private Const CSIDL_COMMON_ALTSTARTUP = 30 'non localized common startup Private Const CSIDL_COMMON_FAVORITES = 31 Private Const CSIDL_INTERNET_CACHE = 32 Private Const CSIDL_COOKIES = 33 Private Const CSIDL_HISTORY = 34 Private Const CSIDL_COMMON_APPDATA = 35 'All Users\Application Data Private Const CSIDL_WINDOWS = 36 'GetWindowsDirectory() Private Const CSIDL_SYSTEM = 37 'GetSystemDirectory() Private Const CSIDL_PROGRAM_FILES = 38 'C:\Program Files Private Const CSIDL_MYPICTURES = 39 'C:\Program Files\My Pictures Private Const CSIDL_PROFILE = 40 'USERPROFILE Private Const CSIDL_SYSTEMX86 = 41 'x86 system directory on RISC Private Const CSIDL_PROGRAM_FILESX86 = 42 'x86 C:\Program Files on RISC Private Const CSIDL_PROGRAM_FILES_COMMON = 43 'C:\Program Files\Common Private Const CSIDL_PROGRAM_FILES_COMMONX86 = 44 'x86 Program Files\Common on RISC Private Const CSIDL_COMMON_TEMPLATES = 45 'All Users\Templates Private Const CSIDL_COMMON_DOCUMENTS = 46 'All Users\Documents Private Const CSIDL_COMMON_ADMINTOOLS = 47 'All Users\Start Menu\Programs\Administrative Tools Private Const CSIDL_ADMINTOOLS = 48 '<user name>\Start Menu\Programs\Administrative Tools Private Const CSIDL_CONNECTIONS = 49 'Network and Dial-up Connections Private Const CSIDL_COMMON_MUSIC = 53 'All Users\My Music Private Const CSIDL_COMMON_PICTURES = 54 'All Users\My Pictures Private Const CSIDL_COMMON_VIDEO = 55 'All Users\My Video Private Const CSIDL_RESOURCES = 56 'Resource Direcotry Private Const CSIDL_RESOURCES_LOCALIZED = 57 'Localized Resource Direcotry Private Const CSIDL_COMMON_OEM_LINKS = 58 'Links to All Users OEM specific apps Private Const CSIDL_CDBURN_AREA = 59 'USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning Private Const CSIDL_COMPUTERSNEARME = 61 'Cookiesフォルダーのパス表示 MsgBox CreateObject("Shell.Application").Namespace(CSIDL_COOKIES).Self.Path
WshShellオブジェクトのSpecialFoldersプロパティを使う方法
WshShellオブジェクトのSpecialFoldersプロパティから特殊フォルダーのコレクションであるWshSpecialFoldersオブジェクトを取得できるので、そこから特殊フォルダーのパスを得ることができます。
Option Explicit Dim s 'WshSpecialFolders オブジェクト 'http://msdn.microsoft.com/ja-jp/library/cc364437.aspx With CreateObject("WScript.Shell") s = "AllUsersDesktop:" & .SpecialFolders("AllUsersDesktop") & vbCrLf s = s & "AllUsersPrograms:" & .SpecialFolders("AllUsersPrograms") & vbCrLf s = s & "AllUsersStartMenu:" & .SpecialFolders("AllUsersStartMenu") & vbCrLf s = s & "AllUsersStartup:" & .SpecialFolders("AllUsersStartup") & vbCrLf s = s & "Desktop:" & .SpecialFolders("Desktop") & vbCrLf s = s & "Favorites:" & .SpecialFolders("Favorites") & vbCrLf s = s & "Fonts:" & .SpecialFolders("Fonts") & vbCrLf s = s & "MyDocuments:" & .SpecialFolders("MyDocuments") & vbCrLf s = s & "NetHood:" & .SpecialFolders("NetHood") & vbCrLf s = s & "PrintHood:" & .SpecialFolders("PrintHood") & vbCrLf s = s & "Programs:" & .SpecialFolders("Programs") & vbCrLf s = s & "Recent:" & .SpecialFolders("Recent") & vbCrLf s = s & "SendTo:" & .SpecialFolders("SendTo") & vbCrLf s = s & "StartMenu:" & .SpecialFolders("StartMenu") & vbCrLf s = s & "Startup:" & .SpecialFolders("Startup") & vbCrLf s = s & "Templates:" & .SpecialFolders("Templates") End With MsgBox s
WshShellオブジェクトのExpandEnvironmentStringsメソッドを使う方法
WshShellオブジェクトのExpandEnvironmentStringsメソッドを使って、環境変数(KNOWNFOLDERID (Windows) 参照)から特殊フォルダーのパスを取得することができます。
Option Explicit 'Program Filesフォルダーのパス表示 MsgBox CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")
FileSystemObjectオブジェクトのGetSpecialFolderメソッドを使う方法
FileSystemObjectオブジェクトのGetSpecialFolderメソッドを使うとFolderオブジェクトが取得できるので、そこから特殊フォルダーのパスを得ることができます。
Option Explicit Private Const WindowsFolder = 0 Private Const SystemFolder = 1 Private Const TemporaryFolder = 2 MsgBox CreateObject("Scripting.FileSystemObject").GetSpecialFolder(TemporaryFolder).Path
この記事へのコメントはありません。