遍历目录下的HTML文件,得到其Title信息

功能:遍历目录下的htm文件,并得到Title,生成HTML Help WorkShop软件所需要的hhk文件.
          另外,生成HTML Help WorkShop下的HTMLHelp API Information的Alias参数

//*********************************
//INFO:   Get HTML file's title
//Param:   htmlFilePath - html file's fullpath
//return:  html file's title
//**********************************
CString GetTitle(CString htmlFilePath)
{
    CFile htmlFile;
    
if(!htmlFile.Open((LPCTSTR)htmlFilePath,CFile::modeRead))
        cout
<<"Can't Open "<<(LPCTSTR)htmlFilePath;
    DWORD fLen 
= htmlFile.GetLength();
    
char* fileStr = new char[fLen];
    memset(fileStr,
0,0);
    htmlFile.Read(fileStr,fLen);
    CString fFile,retStr;
    fFile 
= fileStr;
    
int nStart=fFile.Find("<title>");
    nStart 
+= 7;
    
int nEnd = fFile.Find("</title>");
    retStr 
= fFile.Mid(nStart);
    retStr 
= retStr.Left(nEnd-nStart);
    
return retStr;
    htmlFile.Close();
}



//*********************************
//INFO:   Fill the "HTML Help WorkShop" HHK information
//Param:   fileTitle - HTML file's title / fileName - HTML file's Name
//return:  html file's title
//**********************************
BOOL WriteHHKFile(CString fileTitle,CString fileName)
{
    CFile hhkFile;
    
if(!hhkFile.Open("D:\\Work Project\\WinHelp\\easyhtml\\HTML\\Ccons34jtmp.txt", CFile::modeNoTruncate|CFile::modeReadWrite ))
    
{
        cout
<<"can not Open txt file"<<endl;
        
return FALSE;
    }

    CString strKey;
    strKey.Format(
"%s%s%s%s%s%s%s%s%s%s%s\0","\n<LI><OBJECT type=\"text/sitemap\">\n",
        
"<param name=\"Keyword\" value=\"",fileTitle,"\">\n",
        
"<param name=\"Name\" value=\"",fileTitle,"\">\n",
        
"<param name=\"Local\" value=\"",fileName,"\">\n</OBJECT>\n");
//    cout<<(LPCTSTR)strKey<<endl;
    hhkFile.SeekToEnd();
    hhkFile.Write(strKey.GetBuffer(strKey.GetLength()),strKey.GetLength());
    hhkFile.Close();


    
return TRUE;
}

//*********************************
//INFO:  write "Microsoft HTML Help WorkShop" API alias information
//Param:   fileName - HTML file's Name
//return:  html file's title
//**********************************
BOOL WriteAliasFile(CString fileName)
{
    CFile aliasFile;
    
if(!aliasFile.Open("D:\\Work Project\\WinHelp\\easyhtml\\HTML\\cons34alias.txt", CFile::modeNoTruncate|CFile::modeReadWrite ))
    
{
        cout
<<"can not Open alias file"<<endl;
        
return FALSE;
    }

    CString strAlias;
    strAlias 
=  fileName.Left(9);
    strAlias 
+= "="+fileName;
    strAlias 
+= "\n";
    
//cout<<(LPCTSTR)strAlias;
    aliasFile.SeekToEnd();
    aliasFile.Write(strAlias.GetBuffer(strAlias.GetLength()),strAlias.GetLength());
    aliasFile.Close();
    
return TRUE;
}



int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    
int nRetCode = 0;

    
// initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    
{
        
// TODO: change error code to suit your needs
        cerr << _T("Fatal Error: MFC initialization failed"<< endl;
        nRetCode 
= 1;
    }

    
else
    
{
        CFileFind finder;
        CString fileName,filePath,fileTitle;
        BOOL bWorking 
= finder.FindFile("D:\\Work Project\\WinHelp\\easyhtml\\HTML\\*.htm");
        
while(bWorking)
        
{
            bWorking 
= finder.FindNextFile();
            
//filePath = finder.GetFilePath();
            fileName = finder.GetFileName();
            
//fileTitle = GetTitle(filePath);
            WriteAliasFile(fileName);
        
//    WriteHHKFile(fileTitle,fileName);
            
//cout<<(LPCTSTR)fileTitle<<endl;
            
//cout<<(LPCTSTR)finder.GetFilePath()<<endl;
        }

        
    }


    
return nRetCode;
}


posted @ 2007-06-05 12:54  shipfi  阅读(1191)  评论(0编辑  收藏  举报