Results 1 to 11 of 11

Thread: [RESOLVED] Open a folder in %AppData%

  1. #1

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Resolved [RESOLVED] Open a folder in %AppData%

    Hi,

    From Start, Run if I type:
    explorer.exe %AppData%\FolderName

    FolderName opens in Windows Explorer, but...
    Code:
    Shell "explorer.exe %AppData%\FolderName", 1
    Returns an error message from Windows Explorer:
    The path '%AppData%\FolderName' does not exist or is not a directory.

    but...
    Code:
    Shell "explorer.exe c:\", 1
    opens the root directory in explorer.

    Can you tell why it won't recognize %AppData%?

  2. #2
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Open a folder in %AppData%

    Because %AppData% means nothing in VB. You should use App.Path instead.

    Shell "explorer.exe " & App.Path & "\FolderName", 1

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Open a folder in %AppData%

    That is probably because under some OS' the AppData translates to another subdirectory (Roming, LocalLow, Local for ex.) below AppData.
    So it may not be giving you the desired path you are expecting and can not open the folder since the path is invalid.

    What OS?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Open a folder in %AppData%

    Thanks for the reply.

    app.path points to the folder where my program is being run from, not the systems application data path under Documents and Settings, which is where %AppData% points too.

  5. #5

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: Open a folder in %AppData%

    RobDog888,

    I've tested %AppData% in both XP and Vista Home Premium and it points where I want it too, I just need to know the VB equivalent.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Open a folder in %AppData%

    %AppData% is an environmental variable from the OS. Its valid in VB.

    What OS?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Open a folder in %AppData%

    In XP it points to
    C:\Documents and Settings\UserName\Application Data

    In Vista it points to
    C:\Users\UserName\AppData\Roaming

    Different paths.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Open a folder in %AppData%

    Call me genius (I figured this out simply by trying typing a lot of things):
    Code:
    Option Explicit
    
    Private Const SW_SHOWMAXIMIZED = 3
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Sub Form_Load()
        ShellExecute 0, "explore", """shell:AppData""", vbNullString, vbNullString, SW_SHOWMAXIMIZED
    End Sub
    If you take the extra quotes out, it'll show you Move and Copy browse dialogs, so you must have the extra quotes around it. """shell:AppData\Program Name""" works as well.


    Edit!
    A (complete?) list of what you can use in Vista - remember to also check if it works in XP.
    Last edited by Merri; Jul 2nd, 2008 at 11:23 AM.

  9. #9

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Talking Re: Open a folder in %AppData%

    Merri,

    From now on I will call you a genius, works just the way I wanted.

    Thank you.

  10. #10
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] Open a folder in %AppData%

    SHGetFolderPath API can return any XP-supported special folder (and its Vista counterpart), including both the user's Application Data as well as the shared Application Data folders. Here's a generic function you can use to identify them, along with an enumeration of all the supported special folders for ease of use:
    Code:
    Public Enum FolderEnum
        feApp = 0  ' \Program Files\Project (more reliable than App.Path)
        feCDBurnArea = 59 ' \Docs & Settings\User\Local Settings\Application Data\Microsoft\CD Burning
        feCommonAppData = 35 ' \Docs & Settings\All Users\Application Data
        feCommonDesktop = 25 ' \Docs & Settings\All Users\Desktop
        feCommonDocs = 46 ' \Docs & Settings\All Users\Documents
        feCommonPics = 54 ' \Docs & Settings\All Users\Documents\Pictures
        feCommonMusic = 53 ' \Docs & Settings\All Users\Documents\Music
        feCommonStartMenu = 22 ' \Docs & Settings\All Users\Start Menu
        feCommonStartMenuPrograms = 23 ' \Docs & Settings\All Users\Start Menu\Programs
        feProgramFiles = 38 ' \Program Files
        feProgramFilesCommon = 43 ' \Program Files\Common Files
        'feRecycleBin = 10 ' ???
        feUser = 40 ' \Docs & Settings\User
        feUserAppData = 26 ' \Docs & Settings\User\Application Data
        feUserCache = 32 ' \Docs & Settings\User\Local Settings\Temporary Internet Files
        feUserCookies = 33 ' \Docs & Settings\User\Cookies
        feUserDesktop = 16 ' \Docs & Settings\User\Desktop
        feUserDocs = 5 ' \Docs & Settings\User\My Documents
        feUserFavorites = 6 ' \Docs & Settings\User\Favorites
        feUserHistory = 34 ' \Docs & Settings\User\Local Settings\History
        feUserMusic = 13 ' \Docs & Settings\User\My Documents\My Music
        feUserPics = 39 ' \Docs & Settings\User\My Documents\My Pictures
        feUserRecent = 8 ' \Docs & Settings\User\Recent
        feUserSendTo = 9 ' \Docs & Settings\User\SendTo
        feUserStartMenu = 11 ' \Docs & Settings\User\Start Menu
        feUserStartMenuPrograms = 2 ' \Docs & Settings\User\Start Menu\Programs
        feUserStartup = 7 ' \Docs & Settings\User\Start Menu\Programs\Startup
        feWindows = 36 ' \WINNT
        feWindowsSystem = 37 ' C:\WINNT\System32
    End Enum
    
    Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
    Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
    Private Declare Function SHGetFolderPath Lib "shfolder" Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal pszPath As String) As Long
    
    Public Function SpecialFolder(pfe As FolderEnum) As String
        Const MAX_PATH = 260
        Dim strPath As String
        Dim strBuffer As String
        Dim lngHandle As Long
        Dim lngLen As Long
        
        strBuffer = Space$(MAX_PATH)
        If pfe = feApp Then
            lngHandle = GetModuleHandle(App.EXEName)
            lngLen = GetModuleFileName(lngHandle, strBuffer, MAX_PATH)
            strPath = Left$(strBuffer, lngLen)
            strPath = Left$(strPath, InStrRev(strPath, "\") - 1)
            If InStr(strPath, "Microsoft Visual Studio") > 0 Then strPath = App.Path
        Else
            If SHGetFolderPath(0, pfe, 0, 0, strBuffer) = 0 Then strPath = Left$(strBuffer, InStr(strBuffer, vbNullChar) - 1)
        End If
        If Right$(strPath, 1) = "\" Then strPath = Left$(strPath, Len(strPath) - 1)
        SpecialFolder = strPath
    End Function
    Sample usage:

    ?SpecialFolder(feUserAppData)
    C:\Documents and Settings\Ellis\Application Data

    The weirdness in there dealing with the Application Path (feApp) is to more reliably handle when the exe is on a network share.
    Last edited by Ellis Dee; Jul 2nd, 2008 at 02:35 PM.

  11. #11

    Thread Starter
    Addicted Member Gymbo's Avatar
    Join Date
    Jan 2006
    Location
    The Oregon Coast
    Posts
    213

    Re: [RESOLVED] Open a folder in %AppData%

    Thanks Ellis,

    This enumeration is a keeper. Just proves that there's moree than one way to skin a cat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width