Results 1 to 11 of 11

Thread: [RESOLVED] Format File Path

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Resolved [RESOLVED] Format File Path

    Is there a way to replace uncommon file paths such as:
    %ProgramFiles%\Internet Explorer\iexplore.exe
    with
    C:\Program Files\Internet Explorer\iexplore.exe
    ?

    I know I can do something like:
    filepath=Replace("%ProgramFiles%\Internet Explorer\iexplore.exe","%ProgramFiles%","C:\Program Files")
    but I know there's many others like %windir and %commonprogramfiles%. I know I'm forgetting some so is there a method to use that will also replace the ones I forgot with their normal paths? Thanks

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: Format File Path

    Oh, I believe the "uncommon paths" are called environment variables.

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

    Re: Format File Path

    Yep, it's an API call, and you need to use it on any registry value of type REG_EXPAND_SZ. Send the string to this function:
    Code:
    Private Declare Function ExpandEnvironmentStrings Lib "Kernel32" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nSize As Long) As Long
    
    Public Function ExpandString(pstrEnviron As String) As String
        Const Buffer = 1023
        Dim strExpanded As String
        Dim lngLen As Long
        
        lngLen = Len(pstrEnviron) + Buffer
        strExpanded = Space$(lngLen)
        lngLen = ExpandEnvironmentStrings(pstrEnviron, strExpanded, lngLen)
        ExpandString = Left$(strExpanded, lngLen - 1)
    End Function

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: Format File Path

    Thank you! I just have a couple of questions. Are there any drawbacks to the function and why is the buffer 1023 instead of 1024? Thanks

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

    Re: Format File Path

    Not sure what you mean by drawbacks; this is the recommended way. (For all intents and purposes, this is the only way.)

    The buffer can be whatever you're comfortable with. I've seen implementations where Buffer was like 400. It doesn't really matter; 200 would probably be sufficient. As for why it's 1023 specifically, I originally had it at 1000, then upped it to 1023 to make it more computer-y. It should probably be 1024 instead.

  6. #6
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Format File Path

    In addition to Ellis Dees suggessions, GetEnvironmentVariable API alos will help to obtain the values of Environment Variables.

    Code:
    Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
    Function GetEnvironmentVar(sName As String) As String
        GetEnvironmentVar = String(255, 0)
        GetEnvironmentVariable sName, GetEnvironmentVar, Len(GetEnvironmentVar)
        If InStr(1, GetEnvironmentVar, Chr$(0)) > 0 Then GetEnvironmentVar = Left$(GetEnvironmentVar, InStr(1, GetEnvironmentVar, Chr$(0)) - 1)
        GetEnvironmentVar = sName + ": " + GetEnvironmentVar
    End Function
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Me.AutoRedraw = True
        Me.Print GetEnvironmentVar("USERNAME")
        Me.Print GetEnvironmentVar("USERDOMAIN")
        Me.Print GetEnvironmentVar("PROCESSOR_IDENTIFIER")
        Me.Print GetEnvironmentVar("NUMBER_OF_PROCESSORS")
        Me.Print GetEnvironmentVar("OS")
    End Sub

  7. #7
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Format File Path

    Where are you getting that environment string from ? ie - if it's from the registry, you can retrieve the expanded string directly (without post-processing).

  8. #8
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Format File Path

    schoolbus driver,

    Code:
    Me.Print GetEnvironmentVar("USERNAME")
        Me.Print GetEnvironmentVar("USERDOMAIN")
        Me.Print GetEnvironmentVar("PROCESSOR_IDENTIFIER")
        Me.Print GetEnvironmentVar("NUMBER_OF_PROCESSORS")
        Me.Print GetEnvironmentVar("OS")
    Thease are built in to the os can be found in the Env. Variable list

  9. #9
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Format File Path

    @ Fazi, I know . The reason I asked if the origin was the registry is that the SHRegGetPath API will automatically expand the environment string (REG_EXPAND_SZ) to a full path (it's a 1 liner). SHQueryValueEx does the same (with a bit more code).
    Last edited by schoolbusdriver; Jun 24th, 2007 at 03:19 AM.

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

    Re: Format File Path

    schoolbusdriver, that's excellent to know.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: [RESOLVED] Format File Path

    Okay! Thank you everyone!
    Last edited by abazabam; Jun 25th, 2007 at 10:32 PM.

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