[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

In a *.vbs file detrimine the special folder for 'Program files'

Posted on 2004-11-21
10
Medium Priority
?
4,083 Views
Last Modified: 2007-12-19
I found some code for this but 'Private Declare Function SHGetSpecialFolderLocation Lib "shell32"'  generates an error.  I don't use visual basic script files that often, and cannot seem to declear much of anything like 'Dim Apple as String', however Dim Apple works.  Below is the code.  There is alot of extra special folder, but  
GetSpecialFolderLocation(38) ' This should be 'Program files'









Private Const CSIDL_DESKTOP = &H0                 '{desktop}
Private Const CSIDL_INTERNET = &H1                'Internet Explorer (icon on desktop)
Private Const CSIDL_PROGRAMS = &H2                'Start Menu\Programs
Private Const CSIDL_CONTROLS = &H3                'My Computer\Control Panel
Private Const CSIDL_PRINTERS = &H4                'My Computer\Printers
Private Const CSIDL_PERSONAL = &H5                'My Documents
Private Const CSIDL_FAVORITES = &H6               '{user}\Favourites
Private Const CSIDL_STARTUP = &H7                 'Start Menu\Programs\Startup
Private Const CSIDL_RECENT = &H8                  '{user}\Recent
Private Const CSIDL_SENDTO = &H9                  '{user}\SendTo
Private Const CSIDL_BITBUCKET = &HA               '{desktop}\Recycle Bin
Private Const CSIDL_STARTMENU = &HB               '{user}\Start Menu
Private Const CSIDL_DESKTOPDIRECTORY = &H10       '{user}\Desktop
Private Const CSIDL_DRIVES = &H11                 'My Computer
Private Const CSIDL_NETWORK = &H12                'Network Neighbourhood
Private Const CSIDL_NETHOOD = &H13                '{user}\nethood
Private Const CSIDL_FONTS = &H14                  'windows\fonts
Private Const CSIDL_TEMPLATES = &H15
Private Const CSIDL_COMMON_STARTMENU = &H16       'All Users\Start Menu
Private Const CSIDL_COMMON_PROGRAMS = &H17        'All Users\Programs
Private Const CSIDL_COMMON_STARTUP = &H18         'All Users\Startup
Private Const CSIDL_COMMON_DESKTOPDIRECTORY = &H19 'All Users\Desktop
Private Const CSIDL_APPDATA = &H1A                '{user}\Application Data
Private Const CSIDL_PRINTHOOD = &H1B              '{user}\PrintHood
Private Const CSIDL_LOCAL_APPDATA = &H1C          '{user}\Local Settings _
                                                 '\Application Data (non roaming)
Private Const CSIDL_ALTSTARTUP = &H1D             'non localized startup
Private Const CSIDL_COMMON_ALTSTARTUP = &H1E      'non localized common startup
Private Const CSIDL_COMMON_FAVORITES = &H1F
Private Const CSIDL_INTERNET_CACHE = &H20
Private Const CSIDL_COOKIES = &H21
Private Const CSIDL_HISTORY = &H22
Private Const CSIDL_COMMON_APPDATA = &H23          'All Users\Application Data
Private Const CSIDL_WINDOWS = &H24                 'GetWindowsDirectory()
Private Const CSIDL_SYSTEM = &H25                  'GetSystemDirectory()
Private Const CSIDL_PROGRAM_FILES = &H26           'C:\Program Files
Private Const CSIDL_MYPICTURES = &H27              'C:\Program Files\My Pictures
Private Const CSIDL_PROFILE = &H28                 'USERPROFILE
Private Const CSIDL_SYSTEMX86 = &H29               'x86 system directory on RISC
Private Const CSIDL_PROGRAM_FILESX86 = &H2A        'x86 C:\Program Files on RISC
Private Const CSIDL_PROGRAM_FILES_COMMON = &H2B    'C:\Program Files\Common
Private Const CSIDL_PROGRAM_FILES_COMMONX86 = &H2C 'x86 Program Files\Common on RISC
Private Const CSIDL_COMMON_TEMPLATES = &H2D        'All Users\Templates
Private Const CSIDL_COMMON_DOCUMENTS = &H2E        'All Users\Documents
Private Const CSIDL_COMMON_ADMINTOOLS = &H2F       'All Users\Start Menu\Programs _
                                                  '\Administrative Tools
Private Const CSIDL_ADMINTOOLS = &H30              '{user}\Start Menu\Programs _
                                                  '\Administrative Tools
Private Const CSIDL_FLAG_CREATE = &H8000&          'combine with CSIDL_ value to force
                                                  'create on SHGetSpecialFolderLocation()
Private Const CSIDL_FLAG_DONT_VERIFY = &H4000      'combine with CSIDL_ value to force
                                                  'create on SHGetSpecialFolderLocation()
Private Const CSIDL_FLAG_MASK = &HFF00             'mask for all possible flag values
Private Const SHGFP_TYPE_CURRENT = &H0             'current value for user, verify it exists
Private Const SHGFP_TYPE_DEFAULT = &H1

Private Const MAX_PATH = 260
Private Const S_OK = 0

'Converts an item identifier list to a file system path.
Private Declare Function SHGetPathFromIDList Lib "shell32" _
   Alias "SHGetPathFromIDListA" _
  (ByVal pidl As Long, _
   ByVal pszPath As String) As Long

Private Declare Function SHGetSpecialFolderLocation Lib "shell32" _
   (ByVal hwndOwner As Long, _
    ByVal nFolder As Long, _
    pidl As Long) As Long

Private Declare Sub CoTaskMemFree Lib "ole32" _
   (ByVal pv As Long)


msgbox GetSpecialFolderLocation(38)



Private Function GetSpecialFolderLocation(CSIDL As Long) As String

   Dim sPath As String
   Dim pidl As Long
   
  'fill the idl structure with the specified folder item
   If SHGetSpecialFolderLocation(Me.hWnd, CSIDL, pidl) = S_OK Then
     
     'if the pidl is returned, initialize
     'and get the path from the id list
      sPath = Space$(MAX_PATH)
     
      If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then

        'return the path
         GetSpecialFolderLocation = Left(sPath, InStr(sPath, Chr$(0)) - 1)
         
      End If
   
     'free the pidl
      Call CoTaskMemFree(pidl)

    End If
   
End Function


'Thanks

'   -JP
0
Comment
Question by:sendtojp
  • 5
  • 4
10 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 2000 total points
ID: 12639708

Set oShell = CreateObject("WScript.Shell")
Set oProcEnv = oShell.Environment("PROCESS")

sProgramFiles = oProcEnv("ProgramFiles")

If sProgramFiles = "" Then
  sProgramFiles = oShell.RegRead _
     ("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir")
End If

WScript.Echo sProgramFiles
0
 
LVL 1

Author Comment

by:sendtojp
ID: 12639835
Thanks for Solving my problem :) 500pts. of A


Is there a list of these

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir.


Also how do you force the shortcut to open Maxiumized here is some code for another 500pts.


Set Shortcut = WSHShell.CreateShortcut(DesktopPath & "\MyProgram.mde" & ".lnk")
Shortcut.TargetPath = target

StartupPath = sProgramFiles & "\MyDir\"
Shortcut.WorkingDirectory = StartupPath
0
 
LVL 2

Expert Comment

by:JuergenHartl
ID: 12639849
Set nw = CreateObject("Shell.Application")
Set fldr = nw.NameSpace(38)
MsgBox fldr.Self.path
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sendtojp
ID: 12639860
JuergenHartl,

   Is this a shorter version
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 12639962
You can look in your registry under "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion" to see what is available.Some of the other values that retrieve directory locations are:

CommonFilesDir
ConfigPath
DevicePath
MediaPath
OtherDevicePath
ProgramFilesDir
ProgramFilesPath
SystemRoot
WallPaperDir
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 12639985
Have you tried

Shortcut.WindowStyle = 3
0
 
LVL 1

Author Comment

by:sendtojp
ID: 12640164
0
 
LVL 1

Author Comment

by:sendtojp
ID: 12643085
binge, please reconsider not deleting the other as they added two other answers such as giving a list of other special folders (below).  The first question asked for just the 'Program files' and the code to process it.

Thanks,

     -JP

I did not ask the two other questions because the Questions were asked and answered here. Someone searching would search for simliar things.
 


You can look in your registry under "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion" to see what is available.Some of the other values that retrieve directory locations are:

CommonFilesDir
ConfigPath
DevicePath
MediaPath
OtherDevicePath
ProgramFilesDir
ProgramFilesPath
SystemRoot
WallPaperDir
0
 
LVL 1

Author Comment

by:sendtojp
ID: 12643095
Or I can ask them as seperate questions.???
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question