|
-
Jun 23rd, 2007, 11:22 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jun 23rd, 2007, 11:28 PM
#2
Thread Starter
Hyperactive Member
Re: Format File Path
Oh, I believe the "uncommon paths" are called environment variables.
-
Jun 23rd, 2007, 11:53 PM
#3
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
-
Jun 24th, 2007, 12:10 AM
#4
Thread Starter
Hyperactive Member
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
-
Jun 24th, 2007, 12:48 AM
#5
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.
-
Jun 24th, 2007, 02:32 AM
#6
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
-
Jun 24th, 2007, 02:37 AM
#7
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).
-
Jun 24th, 2007, 02:53 AM
#8
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
-
Jun 24th, 2007, 03:15 AM
#9
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.
-
Jun 24th, 2007, 01:13 PM
#10
Re: Format File Path
schoolbusdriver, that's excellent to know.
-
Jun 24th, 2007, 10:23 PM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|