Results 1 to 4 of 4

Thread: Need Help Here! Something to do with GetShortPath API

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Location
    Singapore
    Posts
    8

    Exclamation

    Hey guys,
    I got a small problem here. You see, I got an alternate to make shortpathnames w/o using the GetShortPath API. The code is as followed:

    Code Snippet:
    -------------

    'I wont type out the sub stuff etc.
    'Code below:

    'Long Name: My Document

    Dim Temp as String
    'make sure there's no space in between

    Temp = Join("My Document", " ")

    'take the first 6 characters

    Temp = Left$(Temp, 6)

    'plant the "~1" stuff

    Temp = Temp & "~1"

    -------------------

    Sometimes we might have 'My Document' and 'My Document Folder', thus we will have 'mydocu~1' and 'mydocu~2'. How do we solve this ~1 and ~2 etc.. problem? And which is '~1', which is '~2'?

    Hope you guys understand what I am typing coz I myself don't understand!
    PiKaPrO
    =======
    PiKa ProGraMMeR © 2000
    [email protected] No SPAMMERS!
    MSVB6.0 PRO SP3 Detected.

  2. #2
    Guest
    I dont understand why you dont use the API

    Use the DIR function to check if it is the same dir as you are looking for, here is an example.

    With api and without


    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
    
    Public Function GetShortPath(ByVal sLongPath As String) As String
        Dim sBuffer As String
        Dim lRet As Long
        sBuffer = Space$(255)
        lRet = GetShortPathName(sLongPath, sBuffer, Len(sBuffer))
        If lRet <> 0 Then
            GetShortPath = Left$(sBuffer, lRet)
        End If
    End Function
    
    Public Function GetShortPathWOApi(ByVal sLongPath As String)
        Dim sTemp As String
        Dim lStart As Long
        Dim lTiCounter As Long
        Dim bAddedSlash As Boolean
        
        lStart = 1
        If Not Right$(sLongPath, Len(sLongPath) - 1) = "\" Then     'Add \ to end
            sLongPath = sLongPath & "\"
            bAddedSlash = True
        End If
        
        Do Until InStr(lStart, sLongPath, "\") = 0      'Do until there are no more slashes
            sTemp = Mid(sLongPath, lStart, InStr(lStart, sLongPath, "\") - lStart)  'Get the dir
            
            If Len(sTemp) > 8 Or (Len(sTemp) <> Len(Replace(sTemp, " ", ""))) Then      'If it is bigger then 8chars
                                                                                        'Or path contains one or more spaces
                sTemp = Replace(sTemp, " ", "")
                sTemp = Left$(sTemp, 6)
                lTiCounter = 1      'Do until the short name of the dir is the same as the long name
                Do Until Dir(Mid(sLongPath, 1, InStr(lStart - 1, sLongPath, "\")) & sTemp & "~" & lTiCounter, vbDirectory) = _
                            Dir(Mid(sLongPath, 1, InStr(lStart, sLongPath, "\") - 1), vbDirectory)
                    lTiCounter = lTiCounter + 1
                Loop
                sTemp = sTemp & "~" & lTiCounter    'Found the long name
            End If
            GetShortPathWOApi = GetShortPathWOApi & sTemp & "\"
            lStart = InStr(lStart, sLongPath, "\") + 1
        Loop
        
        If bAddedSlash Then
            'Remove slash
            GetShortPathWOApi = Left$(GetShortPathWOApi, Len(GetShortPathWOApi) - 1)
        End If
        
        If Dir$(GetShortPathWOApi, vbDirectory) = "" Then
            'Doesn't exists
            GetShortPathWOApi = ""
        End If
    End Function
    
    Private Sub Form_Load()
        MsgBox GetShortPath("C:\My Music")
        MsgBox GetShortPathWOApi("C:\My Music")
    End Sub

    PS Without the api you cant detetect all path's, some paths are giving really odd short names. I made 14 paths called "12345678 (1-14)" and "C:\test\12345678 11" has a shortpath of "C:\test\12A4A6~1"

    [Edited by Azzmodan on 06-05-2000 at 11:59 AM]

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Talking Look!

    I just come out something like this & hope it make sense to you.

    Code:
    Private Sub Form_Load()
    Dim longName$
    Dim shortName$
    Dim fCnt&
    Dim itm$
    Dim fArray() As Integer
    
    longName = "My Document"
    shortName = Mid(LCase(Replace(longName, Chr(32), "")), 1, 6)
    itm = Dir(App.Path & "\" & shortName & "*.txt", vbNormal)
    fCnt = 0
    Do Until itm = vbNullString
        DoEvents
        ReDim Preserve fArray(fCnt)
        fArray(fCnt) = CInt(Mid(itm, 8, 1))
        itm = Dir()
        fCnt = fCnt + 1
    Loop
    
    'Get the largest value for ~#
    itm = 1
    For fCnt = 0 To UBound(fArray()) - 1
        If fArray(fCnt) < fArray(fCnt + 1) Then
            If CInt(itm) < fArray(fCnt + 1) Then itm = fArray(fCnt + 1)
        End If
    Next
    
    Debug.Print "Final Shortname for " & longName & " = " & shortName & "~" & itm + 1&; ".txt"
    End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Location
    Singapore
    Posts
    8
    Originally posted by Azzmodan

    Code:
    'PS Without the api you cant detetect all path's, some paths are giving really odd short names. I made 14 paths called "12345678 (1-14)" and "C:\test\12345678 11" has a shortpath of "C:\test\12A4A6~1"


    [Edited by Azzmodan on 06-05-2000 at 11:59 AM]
    After looking at this, I assume the GetShorthPath API is better than w/o API huh. Thanks for looking at this post and helping out guys!
    PiKaPrO
    =======
    PiKa ProGraMMeR © 2000
    [email protected] No SPAMMERS!
    MSVB6.0 PRO SP3 Detected.

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