|
-
February 15th, 2000, 02:02 AM
#1
Only short filenames must be used so I need to convert long filename to short filename
Please reply immediately cause my task is
on the verge of being lost.
I just want to know; how can we convert long
filenames to short filenames.
A problem stays like a challenge till
I find the solution to it.
-
February 15th, 2000, 02:18 AM
#2
Re: Only short filenames must be used so I need to convert long filename to short filename
use the GetShortPathName API!
-
February 15th, 2000, 02:33 AM
#3
Re: Only short filenames must be used so I need to convert long filename to short filename
I am using it from VB.
Can I get an example please!
A problem stays like a challenge till
I find the solution to it.
-
February 15th, 2000, 02:39 AM
#4
Re: Only short filenames must be used so I need to convert long filename to short filename
Code:
Option Explicit
Private Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" (ByVal lpszLongPath as String _
, ByVal lpszShortPath as String, ByVal cchBuffer as Long) as Long
Private Sub Command1_Click()
Dim l as Long
Dim strLong as string * 255
Dim strshort as string * 255
strLong = "c:\winnt\niagnt32_install.log"
l = GetShortPathName(strLong, strshort, len(strLong))
MsgBox strshort
End Sub
tested with NT 4 and VB 6
Last edited by Cimperiali; February 24th, 2005 at 05:47 AM.
Reason: Tags code and color
-
February 15th, 2000, 02:47 AM
#5
Re: Only short filenames must be used so I need to convert long filename to short filename
I am very very thankful to you.
You have saved my day.
If there was anything more thanks
I could do, I would be obliged to do it.
If I have any other problems, can I contact
you?
if yes then; if you don't have problem with
that; then
please mail me at: [email protected]
A problem stays like a challenge till
I find the solution to it.
-
February 15th, 2000, 02:56 AM
#6
Re: Only short filenames must be used so I need to convert long filename to short filename
>If I have any other problems, can I contact
you?
the easiest way to contact "me" is by posting your question to this forum.
That way you have access to the knowledge of all other forum participants.
-
February 22nd, 2005, 12:31 PM
#7
Re: Only short filenames must be used so I need to convert long filename to short filename
Hi, when using that function it fills the rest of the buffer with NULL characters (maybe the \0 character from c++ not sure).
Is there a VB function to remove the rest of that stuff from the buffer, so I can have just the short path as my string.
-
February 22nd, 2005, 12:38 PM
#8
Re: Only short filenames must be used so I need to convert long filename to short filename
Ok I found a way around this using the following function:
Code:
Private Function VbNullTrim(strTextToTrim As String) As String
Dim lngPosition As Long ' Position of the NULL character
' Find the position of the first vbNullChar
lngPosition = InStr(strTextToTrim, vbNullChar)
' Trim the vbNullChars off the string
If lngPosition > 0 Then
VbNullTrim = Left$(strTextToTrim, lngPosition - 1)
Else
VbNullTrim = strTextToTrim
End If
End Function
Does anyone know of a built-in function to do this?
Last edited by Cimperiali; February 24th, 2005 at 05:47 AM.
Reason: adding code tags
-
February 22nd, 2005, 12:53 PM
#9
Re: Only short filenames must be used so I need to convert long filename to short filename
I have run into another problem with this code.
I read the documentation at:
http://msdn.microsoft.com/library/de...rtpathname.asp
And found out that you shouldn't use len(strLong) as the size of the buffer. For example "c:\a b" converts as "c:\ab~1" which is longer that long filename's length.
The full short path can get quite longer than the long path sometimes (ironic in a way), so it is highly recommended you check to make the return value is not greater than the buffer size you are using
-
February 22nd, 2005, 01:00 PM
#10
Re: Only short filenames must be used so I need to convert long filename to short filename
One more note, after I remove the vbNullChar from that buffer I have to Trim() the buffer and assign it to another String.
-
February 24th, 2005, 05:54 AM
#11
Re: Only short filenames must be used so I need to convert long filename to short filename
 Originally Posted by Billkamm
[...]you shouldn't use len(strLong) as the size of the buffer
Dim strLong as string * 255
means it is a fixed len string
you can fill it with a single char, but it will always be 255 in len
Thus
strLong="a"
msgbox len(strLong) 'you see 255!
Api guide shows this example:
Code:
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Public Function GetShortPath(strFileName As String) As String
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim lngRes As Long, strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the short pathname
lngRes = GetShortPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetShortPath = Left$(strPath, lngRes)
End Function
Private Sub Form_Load()
MsgBox GetShortPath("c:\Program Files\")
End Sub
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
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
|