void main()
{
::CoInitialize(NULL);
wchar_t* szPath=L"D:\\okb\\*.*";
LPSTORAGE stg=NULL;
HRESULT hResult=::StgCreateDocfile(L"D:\\tmp.kil",STGM_READWRITE|STGM_CREATE|STGM_SHARE_EXCLUSIVE,0,&stg);
if(S_OK!=hResult)
{
MessageBox(L"创建符合文件失败",0,0);
return;
}
MainProc(szPath,stg);
::CoUninitialize();
}
void MainProc(wchar_t*szPath,LPSTORAGE& stg)
{
WIN32_FIND_DATA data;
HANDLE hFind=::FindFirstFile (szPath,&data);
if(INVALID_HANDLE_VALUE==hFind)
{
MessageBox(L"查找文件失败",0,0);
}
do
{
//只查找txt文件和目录文件
if(wcscmp(data.cFileName,L".")==0)
{
continue;
}else if(wcscmp(data.cFileName,L"..")==0)
{
continue;
}else if(data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
IStorage*pSubstg=NULL;
stg->CreateStorage(data.cFileName,STGM_READWRITE|STGM_CREATE|STGM_SHARE_EXCLUSIVE,0,0,&pSubstg);
wchar_t Path[1024]={0};
wcscpy(Path,szPath);
wcscpy(&Path[wcslen(szPath)-3],data.cFileName);
wcscpy(Path+wcslen(Path),L"\\*.*");
MainProc(Path,pSubstg);
}else
{
//首先假定全是txt文档
IStream* psubStm=NULL;
stg->CreateStream(data.cFileName,STGM_READWRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,0,0,&psubStm);
//构造文件名称
wchar_t Path[1024]={0};
wcscpy(Path,szPath);
wcscpy(&Path[wcslen(szPath)-3],data.cFileName);
char *name=NULL;
DWORD n=WideCharToMultiByte(CP_OEMCP,NULL,Path,-1,NULL,0,NULL,FALSE);
name=new char[n];
WideCharToMultiByte(CP_OEMCP,NULL,Path,-1,name,n,NULL,FALSE);
ifstream input;
input.open(name);
int filesize=data.nFileSizeLow;
char* buf=new char[filesize+1];
memset(buf,0,filesize+1);
input.read(buf,filesize);
psubStm->Write(buf,filesize,NULL);
delete [] name;
delete [] buf;
buf=NULL;
name=NULL;
psubStm->Release();
}
}while(FindNextFile(hFind,&data));
stg->Release();
}