Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Transforming the path of a file from short format to long

Szilard69Szilard69 Member Posts: 9
I have the path of a file and it is in short format (for example: C:Progra~1... ) I would like to transform it to its long format (C:Program Files...) I'm using Win NT 4.0 and Visual C++ 6.0 so the GetLongPath() function doesn't work (it only works for Win2000 and above)
If you have any idea about how to solve this problem please send me a reply.
Thanx

Szilard Huber

Comments

  • Szilard69Szilard69 Member Posts: 9
    CString shortNameToLongName(CString shortName)
    {
    WIN32_FIND_DATA fd;
    HANDLE hSearch;
    DWORD size;

    // get current directory so we can restore later
    size = GetCurrentDirectory(0, NULL);
    LPTSTR currDir = new _TCHAR[size + 1];
    GetCurrentDirectory(size, currDir);

    if (shortName.Find(_T('\')) < 0)
    {
    // not a full path - just return it
    return shortName;
    }

    // Is it UNC style (//server/share/...) or normal (drive:/...)
    if (shortName.Left(2) == _T("\\"))
    {
    // UNC so we do CD //server/share
    CString s = shortName.Right(shortName.GetLength() - 2);
    int pos = s.Find(_T('\'));
    ASSERT(pos);
    CString server = shortName.Left(pos + 2);
    s = s.Right(s.GetLength() - pos - 1);
    pos = s.Find(_T('\'));
    ASSERT(pos);
    CString share = s.Left(pos);
    ASSERT(SetCurrentDirectory(server + _T("\") + share));
    shortName = s.Right(s.GetLength() - pos - 1);
    } else
    {
    // Normal so we do CD "drive:"
    int pos = shortName.Find(_T('\'));
    CString drive = shortName.Left(pos + 1);
    ASSERT(SetCurrentDirectory(drive));
    shortName = shortName.Right(shortName.GetLength() - pos - 1);
    }

    // Get long name for each for each directory in path
    int pos = shortName.Find(_T('\'));
    while (pos >= 0)
    {
    CString s = shortName.Left(pos);
    hSearch = FindFirstFile( shortName.Left(pos), &fd );
    ASSERT(hSearch != INVALID_HANDLE_VALUE);
    FindClose(hSearch);
    ASSERT(SetCurrentDirectory(fd.cFileName));
    shortName = shortName.Right(shortName.GetLength() - pos - 1);
    pos = shortName.Find(_T('\'));
    }

    // Whats left in shortName is filename - convert it long
    hSearch = FindFirstFile( shortName, &fd );
    ASSERT(hSearch != INVALID_HANDLE_VALUE);

    // Now append it to current
    // directory.
    size = GetCurrentDirectory(0, NULL);
    LPTSTR dir = new _TCHAR[size + 1];
    GetCurrentDirectory(size, dir);
    CString result = CString(dir) + _T("\") + fd.cFileName;

    // go back to initial directory
    ASSERT(SetCurrentDirectory(currDir));
    delete currDir;
    delete dir;

    return result;
    }
  • Justin BibJustin Bib USAMember Posts: 0

    ____ < http://forcoder.org /> free video tutorials and ebooks about < Go, Assembly, C++, Java, Scratch, PHP, Swift, Visual Basic .NET, Visual Basic, R, MATLAB, Perl, C#, PL/SQL, Objective-C, JavaScript, Delphi, C, Python, Ruby ABAP, Prolog, FoxPro, Transact-SQL, Erlang, VBScript, Alice, Lisp, Apex, Dart, Kotlin, Hack, Clojure, LabVIEW, Rust, Scala, Bash, COBOL, SAS, Lua, Fortran, F#, D, Ada, Logo, Awk, Scheme, Crystal, Julia, ML /> ___________

Sign In or Register to comment.