String Handling: Truncating Long Path Names

  • Thread starter Thread starter Ron
  • Start date Start date
R

Ron

I *just* know there's a function to do this, but I can't
find it:

I want to take the following string:

c:\winnt\23342432sss\2343243ccc\32432432423eeee\2432424cc\t
tt\xxx\tttt\xxx\explorer.exe-ph33334

into something like:

c:\winnt\...\explorer.exe-ph33334

Is there a function to do this?

And a related question:

Is there a 64 char limit to the NotifyIcon text? If so,
can I override it?

Thanks.

RON
 
* "Ron said:
I *just* know there's a function to do this, but I can't
find it:

I want to take the following string:

c:\winnt\23342432sss\2343243ccc\32432432423eeee\2432424cc\t
tt\xxx\tttt\xxx\explorer.exe-ph33334

into something like:

c:\winnt\...\explorer.exe-ph33334

Is there a function to do this?

\\\
Private Declare Function GetWindowDC Lib "user32.dll" ( _
ByVal hwnd As IntPtr _
) As IntPtr

Private Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal hdc As IntPtr _
) As IntPtr

Private Declare Function PathCompactPath Lib "shlwapi.dll" Alias "PathCompactPathA" ( _
ByVal hDC As IntPtr, _
ByVal lpszPath As String, _
ByVal dx As Int32 _
) As Int32

Private Sub Test()
Dim hDC As IntPtr = GetWindowDC(Me.Handle)
Dim strFilePath As String = _
"C:\Program Files\Some Company\Some Product\bla\bla\File.txt"
PathCompactPath(hDC, strFilePath, 250)
MsgBox(strFilePath)
ReleaseDC(Me.Handle, hDC)
End Sub
///
 
I want to take the following string:
c:\winnt\23342432sss\2343243ccc\32432432423eeee\2432424cc\t
tt\xxx\tttt\xxx\explorer.exe-ph33334

into something like:

c:\winnt\...\explorer.exe-ph33334

Is there a function to do this?

You need to look for the position of the second and the last "\", create
substrings and concatenate them. Use InStr/InStrRev or
String.IndexOf/String.LastIndexOf. Watch out for the exceptions (ie. file in
root directory).
And a related question:

Is there a 64 char limit to the NotifyIcon text? If so,
can I override it?

ToolTip text must be less than 64 characters long.
http://msdn.microsoft.com/library/d...ystemwindowsformsnotifyiconclasstexttopic.asp

Always look in MSDN for answers to your questions.

sincerely,
--
Sebastian Zaklada
Skilled Software
http://www.skilledsoftware.com
************************************
SQL Source Control 2003 - for
SQL Server Source Safe integration
and custom databases documentation
 
Ron,
In addition to Herfried's suggestion of PathCompactPath, there is
PathCompactPathEx that accepts a length instead of a HDC.

Something like:
Declare Auto Function PathCompactPathEx Lib "shlwapi.dll" (ByVal pszOut
As String, ByVal pszSrc As String, ByVal cchMax As Integer, ByVal dwFlags As
Integer) As Boolean

Public Shared Sub Main()
Dim stringIn As String
Dim stringOut As String
stringIn =
"c:\winnt\23342432sss\2343243ccc\32432432423eeee\2432424cc\ttt\xxx\tttt\xxx\
explorer.exe-ph33334"
stringOut = New String(" "c, 34)
PathCompactPathEx(stringOut, stringIn, stringOut.Length, 0)
stringOut = stringOut.Trim(ChrW(0)) ' remove trailing null char
Debug.WriteLine(stringIn, "String in")
Debug.WriteLine(stringOut, "String out")
End Sub

Hope this helps
Jay
 
Back
Top