Get a file name from a path

前端 未结 21 1638
面向向阳花
面向向阳花 2020-11-30 23:55

What is the simplest way to get the file name that from a path?

string filename = \"C:\\\\MyDirectory\\\\MyFile.bat\"

In this example, I s

21条回答
  •  情书的邮戳
    2020-12-01 00:18

    m_szFilePath.MakeLower();
    CFileFind finder;
    DWORD buffSize = MAX_PATH;
    char longPath[MAX_PATH];
    DWORD result = GetLongPathName(m_szFilePath, longPath, MAX_PATH );
    
    if( result == 0)
    {
        m_bExists = FALSE;
        return;
    }
    m_szFilePath = CString(longPath);
    m_szFilePath.Replace("/","\\");
    m_szFilePath.Trim();
    //check if it does not ends in \ => remove it
    int length = m_szFilePath.GetLength();
    if( length > 0 && m_szFilePath[length - 1] == '\\' )
    {
        m_szFilePath.Truncate( length - 1 );
    }
    BOOL bWorking = finder.FindFile(this->m_szFilePath);
    if(bWorking){
        bWorking = finder.FindNextFile();
        finder.GetCreationTime(this->m_CreationTime);
        m_szFilePath = finder.GetFilePath();
        m_szFileName = finder.GetFileName();
    
        this->m_szFileExtension = this->GetExtension( m_szFileName );
    
        m_szFileTitle = finder.GetFileTitle();
        m_szFileURL = finder.GetFileURL();
        finder.GetLastAccessTime(this->m_LastAccesTime);
        finder.GetLastWriteTime(this->m_LastWriteTime);
        m_ulFileSize = static_cast(finder.GetLength());
        m_szRootDirectory = finder.GetRoot();
        m_bIsArchive = finder.IsArchived();
        m_bIsCompressed = finder.IsCompressed();
        m_bIsDirectory = finder.IsDirectory();
        m_bIsHidden = finder.IsHidden();
        m_bIsNormal = finder.IsNormal();
        m_bIsReadOnly = finder.IsReadOnly();
        m_bIsSystem = finder.IsSystem();
        m_bIsTemporary = finder.IsTemporary();
        m_bExists = TRUE;
        finder.Close();
    }else{
        m_bExists = FALSE;
    }
    

    The variable m_szFileName contains the fileName.

提交回复
热议问题