CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2002
    Location
    Greece
    Posts
    42

    Long filenames converting to short ones (with ~1)

    Hello.
    I'm making a frontend program that uses emulators to play games.
    I have to use one that doesn't accept long filenames though...
    How can I convert the whole path of a file to the same path written the short way?
    (for example C:\Program files\test.txt --> C:\Progra~1\test.txt)
    I don't want to use the first 6 characters of the filename and then add ~1 because it can be ~2 if you have 2 files with the same 6 first characters. And it doesn't have to do with sorting...

    I think I should first check if there's a ??????~2.??? file. If not, then I know that the filename is ??????~1.???
    What can I do when there are many?
    Visit www.greekroms.net

    Greek translations of roms

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332
    There is the GetShortPathName API for that.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Apr 2002
    Location
    Greece
    Posts
    42
    Thanks WizBang :-)
    Visit www.greekroms.net

    Greek translations of roms

  4. #4
    Join Date
    Jul 2001
    Location
    maharashtra,india
    Posts
    181
    hi wizbang
    can u give me an example to use this function

    i need to get short file name for
    C:\Program Files\Internet Explorer\Connection Wizard\Readme.txt

    what parameter should i pass

    thanx

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332
    You can get all the info for tons of API's at allapi.net. Get both the guide and viewer. They are a MUST HAVE!!
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

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

    From Api-guide

    (Api guide is a free tool you can download at www.allapi.net)
    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
    Related function: getFullPathName:
    Code:
    Private Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
    Private Sub Form_Load()
       'KPD-Team 2000
     'URL: http://www.allapi.net/
     'E-Mail: [email protected]
     Dim Buffer As String, Ret As Long
     'create a buffer
     Buffer = Space(255)
     'copy the current directory to the buffer and append 'myfile.ext'
     Ret = GetFullPathName("myfile.ext", 255, Buffer, "")
     'remove the unnecessary chr$(0)'s
     Buffer = Left(Buffer, Ret)
     'show the result
     MsgBox Buffer
    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