CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 1999
    Posts
    60

    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.

  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Only short filenames must be used so I need to convert long filename to short filename

    use the GetShortPathName API!


  3. #3
    Join Date
    Dec 1999
    Posts
    60

    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.

  4. #4
    Join Date
    May 1999
    Posts
    3,332

    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

  5. #5
    Join Date
    Dec 1999
    Posts
    60

    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.

  6. #6
    Join Date
    May 1999
    Posts
    3,332

    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.


  7. #7
    Join Date
    Feb 2005
    Posts
    6

    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.

  8. #8
    Join Date
    Feb 2005
    Posts
    6

    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

  9. #9
    Join Date
    Feb 2005
    Posts
    6

    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

  10. #10
    Join Date
    Feb 2005
    Posts
    6

    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.

  11. #11
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Only short filenames must be used so I need to convert long filename to short filename

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured