FileManager

来源:互联网 发布:樱井知香2010年番号 编辑:程序博客网 时间:2024/06/06 19:32

"FileManager.h"


#include <list>


#define FILE_PATH_LEN (1024)
#define FILE_LINE_LEN (1024)


#define PARAMETER_ERROR (0)
#define ENFORCE_SUCCESSED (1)
#define ENFORCE_FAILED (2)


enum OPERATION_TYPE

MATCH_FILE=1,
DELITE_FILE,
DELETE_BY_EXTENSION,
DELETE_BY_PARTNAME,
FIND_FILE,
REPLACE_FILE,
INSERT_FRONT,
INSERT_BACK
};


class CFileHelperDlg;




class CFileManager
{
public:
CFileManager(char* pcMathLine, char* m_pcOperateLine);
~CFileManager(void);


private:


char* m_pcMatchLine;
char* m_pcOperateLine;


public:


int insertFileBack(char* pcFilePath);
int insertFileFront(char* pcFilePath);
int replaceFile(char* pcFilePath);
int deleteFile(char* pcFilePath);
int findFile(char* pcFilePath,  std::list<int> &pnLine);
int matchFile(char* pcFilePath, std::list<int> &pnLine);
int TraverseFolder(char* pcFilePath, int flag, CFileHelperDlg* dlg);
};


/*********************************************************************************************


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "FileManager.h"
#include "Resource.h"
#include "FileHelperDlg.h"


using namespace std;


CFileManager::CFileManager(char* pcMathLine, char* pcOperateLine)
{
m_pcMatchLine = pcMathLine;
m_pcOperateLine = pcOperateLine;
}


CFileManager::~CFileManager(void)
{


}


int CFileManager::insertFileBack(char* pcFilePath)
{
if(0 == strcmp(pcFilePath, "") || 0 == pcFilePath)
{
return PARAMETER_ERROR;
}


FILE *pFile = 0;
CString strLine;
BOOL bIsInserted=FALSE;
CStdioFile file;
file.Open(pcFilePath, CFile::modeRead);


if(0 == file.m_pStream )
{
return PARAMETER_ERROR;
}


char *pcNewPath = (char*)malloc(FILE_PATH_LEN);
if(0 == pcNewPath)
{
return PARAMETER_ERROR;
}
memset(pcNewPath, 0, FILE_PATH_LEN);
sprintf_s(pcNewPath, FILE_PATH_LEN, "%s_new", pcFilePath);


fopen_s(&pFile, pcNewPath, "w");
if(0 == pFile)
{
free(pcNewPath);
return PARAMETER_ERROR;
}


while(file.ReadString(strLine))
{
char *pcLine = strLine.GetBuffer(0);
int nRes = strcmp(pcLine, m_pcMatchLine);

fprintf(pFile, "%s\n", pcLine);


if(0 == nRes)
{
fprintf(pFile, "%s\n", m_pcOperateLine);
bIsInserted=TRUE;
}
}


file.Close();
fclose(pFile);


//rename and delete 
BOOL bRes = DeleteFile(pcFilePath);
if(bRes)
{
rename(pcNewPath, pcFilePath);
}

free(pcNewPath);


if(bIsInserted)
{
return ENFORCE_SUCCESSED;
}
else
{
return ENFORCE_FAILED;
}
}




int CFileManager::insertFileFront(char* pcFilePath)
{
if(0 == strcmp(pcFilePath, "") || 0 == pcFilePath)
{
return PARAMETER_ERROR;
}


FILE *pFile = 0;
CString strLine;
BOOL bIsInserted=FALSE;
CStdioFile file;
file.Open(pcFilePath, CFile::modeRead);


if(0 == file.m_pStream)
{
return PARAMETER_ERROR;
}


char *pcNewPath = (char*)malloc(FILE_PATH_LEN);
if(0 == pcNewPath)
{
return PARAMETER_ERROR;
}
memset(pcNewPath, 0, FILE_PATH_LEN);
sprintf_s(pcNewPath, FILE_PATH_LEN, "%s_new", pcFilePath);


fopen_s(&pFile, pcNewPath, "w");
if(0 == pFile)
{
free(pcNewPath);
return PARAMETER_ERROR;
}


while(file.ReadString(strLine))
{
char *pcLine = strLine.GetBuffer(0);
int nRes = strcmp(pcLine, m_pcMatchLine);

if(nRes == 0)
{
fprintf(pFile, "%s\n", m_pcOperateLine);
bIsInserted=TRUE;
}


fprintf(pFile, "%s\n", pcLine);
}


file.Close();
fclose(pFile);


//rename and delete 
BOOL bRes = DeleteFile(pcFilePath);
if(bRes)
{
rename(pcNewPath, pcFilePath);
}

free(pcNewPath);
if(bIsInserted)
{
return ENFORCE_SUCCESSED;
}
else
{
return ENFORCE_FAILED;
}
}




int CFileManager::replaceFile(char* pcFilePath)
{
if(0 == strcmp(pcFilePath, "") || 0 == pcFilePath)
{
return PARAMETER_ERROR;
}


FILE *pFile = 0;
CString strLine;
BOOL bIsReplaceed=FALSE;
CStdioFile file;
file.Open(pcFilePath, CFile::modeRead);


if(0 == file.m_pStream )
{
return PARAMETER_ERROR;
}


char *pcNewPath = (char*)malloc(FILE_PATH_LEN);


if(0 == pcNewPath)
{
return PARAMETER_ERROR;
}


memset(pcNewPath, 0, FILE_PATH_LEN);
sprintf_s(pcNewPath, FILE_PATH_LEN, "%s_new", pcFilePath);
fopen_s(&pFile, pcNewPath, "w");


if(0 == pFile)
{
free(pcNewPath);
return PARAMETER_ERROR;
}


while(file.ReadString(strLine))
{
char *pcLine = strLine.GetBuffer(0);
int nRes = strcmp(pcLine, m_pcMatchLine);


if(0 == nRes)
{
fprintf(pFile, "%s\n", m_pcOperateLine);
bIsReplaceed=TRUE;
}
else
{
fprintf(pFile, "%s\n", pcLine);
}
}


file.Close();
fclose(pFile);


//rename and delete 
BOOL bRes = DeleteFile(pcFilePath);
if(bRes)
{
rename(pcNewPath, pcFilePath);
}

free(pcNewPath);


if(bIsReplaceed)
{
return ENFORCE_SUCCESSED;
}
else
{
return ENFORCE_FAILED;
}

}


int CFileManager::deleteFile(char* pcFilePath)
{

if(0 == pcFilePath || 0 == strcmp(pcFilePath, ""))
{
return PARAMETER_ERROR;
}
FILE *pFile = 0;
CString strLine;
BOOL IsDeleted=FALSE;
CStdioFile file;
file.Open(pcFilePath, CFile::modeRead);


if(0 == file.m_pStream )
{
return PARAMETER_ERROR;
}


char *pcNewPath = (char*)malloc(FILE_PATH_LEN);
if(0 == pcNewPath)
{
return PARAMETER_ERROR;
}
memset(pcNewPath, 0, FILE_PATH_LEN);
sprintf_s(pcNewPath, FILE_PATH_LEN, "%s_new", pcFilePath);


fopen_s(&pFile, pcNewPath, "w");
if(0 == pFile)
{
free(pcNewPath);
return PARAMETER_ERROR;
}


while(file.ReadString(strLine))
{
char *pcLine = strLine.GetBuffer(0);
int nRes = strcmp(pcLine, m_pcMatchLine);


if(0 == nRes)
{
IsDeleted=TRUE;
continue;
}

fprintf(pFile, "%s\n", pcLine);
}


file.Close();
fclose(pFile);


//rename and delete 
BOOL bRes = DeleteFile(pcFilePath);
if(bRes)
{
rename(pcNewPath, pcFilePath);
}

free(pcNewPath);


if(IsDeleted)
{
return ENFORCE_SUCCESSED;
}
else
{
return ENFORCE_FAILED;
}
}


int CFileManager::findFile(char* pcFilePath, list<int> &pnLine)
{
if(0 == pcFilePath || 0 == strcmp(pcFilePath, ""))
{
return PARAMETER_ERROR;
}


CString strLine=_T("");
UINT nLine = 0;
BOOL bIsMatch=FALSE;//new




CStdioFile file;
file.Open(pcFilePath, CFile::modeRead);


if(0 == file.m_pStream )
{
return PARAMETER_ERROR;
}


while(file.ReadString(strLine))
{
nLine++;
char *pcLine = strLine.GetBuffer(0);


if(strstr(pcLine, m_pcMatchLine))
{
pnLine.push_back(nLine);
bIsMatch=TRUE;
}
strLine=_T("");
}

file.Close();
if(bIsMatch)//new
{
return ENFORCE_SUCCESSED;
}
else
{
return ENFORCE_FAILED;
}

}




int CFileManager::matchFile(char* pcFilePath, list<int> &pnLine)
{
if(0 == pcFilePath || 0 == strcmp(pcFilePath, ""))
{
return PARAMETER_ERROR;
}

CString strLine;
UINT nLine = 0;
BOOL bIsMatch=FALSE;//new


CStdioFile file;
file.Open(pcFilePath, CFile::modeRead);


if( 0 ==file.m_pStream )
{
return PARAMETER_ERROR;
}




while(file.ReadString(strLine))
{
nLine++;
char *pcLine = strLine.GetBuffer(0);


if(strcmp(pcLine, m_pcMatchLine) == 0)
{
pnLine.push_back(nLine);
bIsMatch=TRUE;
}
strLine=_T("");
}
file.Close();


if(bIsMatch)//new
{
return ENFORCE_SUCCESSED;
}
else
{
return ENFORCE_FAILED;
}

}


int CFileManager::TraverseFolder(char* pcPath, int flag, CFileHelperDlg* dlg)
{
if(0 == dlg)
{
return PARAMETER_ERROR;
}


switch(flag)
{
case FIND_FILE:
case MATCH_FILE:
case DELITE_FILE:
case DELETE_BY_PARTNAME:
case DELETE_BY_EXTENSION:
if(0 == pcPath || 0 == strcmp(pcPath, "") || 0 == strcmp(m_pcMatchLine, ""))
{
return ENFORCE_FAILED;
}
break;
case INSERT_BACK:
case REPLACE_FILE:
case INSERT_FRONT:
if(0 == pcPath || 0 == strcmp(pcPath, "") || 0 == strcmp(m_pcMatchLine, "") || 0 == strcmp(m_pcOperateLine, ""))
{
return ENFORCE_FAILED;
}
break;
default:
break;
}


WIN32_FIND_DATA FindData;  
HANDLE hError = 0;  
int FileCount = 0;  
char FilePathName[FILE_PATH_LEN] = {0};  
int nExtLen = strlen(m_pcMatchLine);
// 构造路径  
char FullPathName[FILE_PATH_LEN] = {0};  
strcpy_s(FilePathName, pcPath);  
strcat_s(FilePathName, "\\*.*");  
hError = FindFirstFile(FilePathName, &FindData);  
if (INVALID_HANDLE_VALUE == hError )  
{   
return PARAMETER_ERROR;  
}  


while(::FindNextFile(hError, &FindData))  
{   
// 过虑.和..   
if (strcmp(FindData.cFileName, ".") == 0     
|| strcmp(FindData.cFileName, "..") == 0 )   
{    continue;  }      

// 构造完整路径  
sprintf_s(FullPathName, "%s\\%s", pcPath, FindData.cFileName); 
FileCount++;  


//file operation
if (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)   
{     
// 输出本级的文件  
CString strDir(FullPathName);
strDir += L"******<dir>";
dlg->showInformation(strDir.GetBuffer(0));
TraverseFolder(FullPathName,flag, dlg);   
}
else
{
UINT nLine = 0;
char pcLine[10] = {0};
list<int> nArrLine;
list<int>::iterator iter;
switch (flag)
{
case MATCH_FILE:
{
int nRet = matchFile(FullPathName, nArrLine);
char arrTemp[FILE_PATH_LEN] = {0};
int nCount = 0;
switch(nRet)
{
case PARAMETER_ERROR:
strcat_s(FullPathName, "---error");
break;
case ENFORCE_SUCCESSED:


for(iter = nArrLine.begin(); iter != nArrLine.end(); iter++)
{
nCount++;
if(nCount>1)
{
strcpy_s(arrTemp,"---match in line: ");
_itoa_s((int)*iter, pcLine, 10);
strcat_s(arrTemp, pcLine);

}
else
{
strcat_s(FullPathName, "---match in line: ");
_itoa_s((int)*iter, pcLine, 10);
strcat_s(FullPathName, pcLine);

}
memset(arrTemp,0,1024);
}


break;

case ENFORCE_FAILED:
strcat_s(FullPathName, "---can not match");
if(!(dlg->m_bIsDisplayed))
{
continue;
}
break;
default:
strcat_s(FullPathName, "---unknown error");
break;
}
break;
}


case FIND_FILE:
{
int nRet = findFile(FullPathName,nArrLine);

char arrTemp[FILE_PATH_LEN]={0};
int nCount=0;
switch(nRet)
{
case PARAMETER_ERROR:
strcat_s(FullPathName, "---error");
break;
case ENFORCE_SUCCESSED:
for(iter = nArrLine.begin(); iter != nArrLine.end(); iter++)
{
nCount++;
if(nCount>1)
{
strcpy_s(arrTemp,"---find in line: ");
_itoa_s(*iter, pcLine, 10);
strcat_s(arrTemp, pcLine);
}
else
{
strcat_s(FullPathName, "---find in line: ");
_itoa_s(*iter, pcLine, 10);
strcat_s(FullPathName, pcLine);
}
memset(arrTemp,0,1024);
}
break;
case ENFORCE_FAILED:
strcat_s(FullPathName, "---can not find!");
if(!(dlg->m_bIsDisplayed))
{
continue;
}
break;
default:
strcat_s(FullPathName, "---unknown error");
break;
}
break;
}


case DELITE_FILE:
{
int nRet = deleteFile(FullPathName);
switch(nRet)
{
case PARAMETER_ERROR:
strcat_s(FullPathName, "---error");
break;
case ENFORCE_SUCCESSED:
strcat_s(FullPathName, "---delete successfully!");
break;
case ENFORCE_FAILED:
strcat_s(FullPathName, "---fail,not match!");
if(!(dlg->m_bIsDisplayed))
{
continue;
}
break;
default:
strcat_s(FullPathName, "---unknown error");
break;
}
break;
}


case DELETE_BY_EXTENSION:
{
int nPathLen = strlen(FullPathName);
if(strcmp((FullPathName + nPathLen - nExtLen), m_pcMatchLine) == 0)
{
BOOL bRes = DeleteFile(FullPathName);

if(bRes)
{
strcat_s(FullPathName, "---deleted by extension!");
}

}
else
{
strcat_s(FullPathName, "---faile,not match!");
if(!(dlg->m_bIsDisplayed))
{
continue;
}
}
break;
}


case DELETE_BY_PARTNAME:
{
if(strstr(FullPathName, m_pcMatchLine))
{
BOOL bRes = DeleteFile(FullPathName);

if(bRes)
{
strcat_s(FullPathName, "---deleted by part_name!");
}
}
else
{
strcat_s(FullPathName, "---faile,not match!");
if(!(dlg->m_bIsDisplayed))
{
continue;
}
}
break;
}


case REPLACE_FILE:
{
int nRet = replaceFile(FullPathName);
switch(nRet)
{
case PARAMETER_ERROR:
strcat_s(FullPathName, "---error");
break;
case ENFORCE_SUCCESSED:
strcat_s(FullPathName, "---replace successfully!");
break;
case ENFORCE_FAILED:
strcat_s(FullPathName, "---replace failly!,not match");
if(!(dlg->m_bIsDisplayed))
{
continue;
}
break;
default:
strcat_s(FullPathName, "---unknown error");
break;
}
break;
}


case INSERT_FRONT:
{
int nRet = insertFileFront(FullPathName);
switch(nRet)
{
case PARAMETER_ERROR:
strcat_s(FullPathName, "---error");
break;
case ENFORCE_SUCCESSED:
strcat_s(FullPathName, "---insert successfully!");
break;
case ENFORCE_FAILED:
strcat_s(FullPathName, "---insert failly!");
if(!(dlg->m_bIsDisplayed))
{
continue;
}
break;
default:
strcat_s(FullPathName, "---unknow error");
break;
}
break;
}


case INSERT_BACK:
{
int nRet = insertFileBack(FullPathName);
switch(nRet)
{
case PARAMETER_ERROR:
strcat_s(FullPathName, "---error");
break;
case ENFORCE_SUCCESSED:
strcat_s(FullPathName, "---insert successfully!");
break;
case ENFORCE_FAILED:
strcat_s(FullPathName, "---insert failly!");
if(!(dlg->m_bIsDisplayed))
{
continue;
}
break;
default:
strcat_s(FullPathName, "---unknow error");
break;
}
break;
}
default:
break;
}
dlg->showInformation(FullPathName);
nArrLine.clear();
}
}  
return ENFORCE_SUCCESSED; 
}




// FileHelperDlg.cpp : implementation file
//


#include "stdafx.h"
#include "FileHelper.h"
#include "FileHelperDlg.h"
#include "afxdialogex.h"
#include "FileManager.h"
//#include "ColorListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// CAboutDlg dialog used for App About


class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();


// Dialog Data
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()




// CFileHelperDlg dialog






// FileHelperDlg.cpp : implementation file
//


#include "stdafx.h"
#include "FileHelper.h"
#include "FileHelperDlg.h"
#include "afxdialogex.h"


#include "FileManager.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// CAboutDlg dialog used for App About


class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();


// Dialog Data
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()




// CFileHelperDlg dialog






CFileHelperDlg::CFileHelperDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CFileHelperDlg::IDD, pParent)
, m_bIsDisplayed(FALSE)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strFilePath=(_T(""));
m_strOperteLine=(_T(""));
m_strMatchLine=(_T(""));

}


void CFileHelperDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_MATCH, m_strMatchLine);
DDX_Text(pDX, IDC_EDIT_OPERATE, m_strOperteLine);
DDX_Text(pDX, IDC_EDIT_FILE_PATH, m_strFilePath);
DDX_Control(pDX, IDC_LIST_INFO, m_listBox);
DDX_Control(pDX, IDC_STATIC_STATE, m_state);
DDX_Check(pDX, IDC_CHECK_DISPLAY, m_bIsDisplayed);
}


BEGIN_MESSAGE_MAP(CFileHelperDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DROPFILES()
ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CFileHelperDlg::OnBnClickedButtonBrowse)
ON_BN_CLICKED(IDC_BUTTON_MATCH, &CFileHelperDlg::OnBnClickedButtonMatch)
ON_BN_CLICKED(IDC_BUTTON_FIND, &CFileHelperDlg::OnBnClickedButtonFind)
ON_BN_CLICKED(IDC_BUTTON_DELETE, &CFileHelperDlg::OnBnClickedButtonDelete)
ON_BN_CLICKED(IDC_BUTTON_REPLACE, &CFileHelperDlg::OnBnClickedButtonReplace)
ON_BN_CLICKED(IDC_BUTTON_DELETE_BY_FILENAME, &CFileHelperDlg::OnBnClickedButtonDeleteFileByName)
ON_BN_CLICKED(IDC_BUTTON_DELETE_BY_EXT, &CFileHelperDlg::OnBnClickedButtonDeleteFileByExtension)
ON_BN_CLICKED(IDC_BUTTON_INSERT_IN_FRONT, &CFileHelperDlg::OnBnClickedButtonInsertInFront)
ON_BN_CLICKED(IDC_BUTTON_INSERT_IN_BACK, &CFileHelperDlg::OnBnClickedButtonInsertInBack)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_CHECK_DISPLAY, &CFileHelperDlg::OnBnClickedCheckDisplay)
END_MESSAGE_MAP()




// CFileHelperDlg message handlers


BOOL CFileHelperDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();


// Add "About..." menu item to system menu.


// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);


CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}


// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon


// TODO: Add extra initialization here
SendDlgItemMessage(IDC_LIST_INFO, LB_SETHORIZONTALEXTENT, 1000, 0);


return TRUE;  // return TRUE  unless you set the focus to a control
}


void CFileHelperDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}


// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.


void CFileHelperDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting


SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);


// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;


// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}


// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFileHelperDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}






void CFileHelperDlg::OnDropFiles(HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default
UINT count;
TCHAR arrfilePath[200];
count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
if(count)
{
for(UINT i=0;i<count;i++)
{
int pathLen=DragQueryFile(hDropInfo,i,arrfilePath,sizeof(arrfilePath));
m_strFilePath=arrfilePath;
}
}
DragFinish(hDropInfo);
UpdateData(FALSE);
CDialogEx::OnDropFiles(hDropInfo);
}




void CFileHelperDlg::OnBnClickedButtonBrowse()
{
// TODO: Add your control notification handler code here
CString fileSrc;         //将选择的文件夹路径保存在此变量中
TCHAR Buffer[MAX_PATH];
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO));
bi.hwndOwner = NULL;
bi.ulFlags = BIF_RETURNONLYFSDIRS;    //要求返回文件系统的目录
bi.pszDisplayName = Buffer;            //此参数如为NULL则不能显示对话框
bi.lpszTitle = _T("Please choose directory...");
bi.lpfn = NULL;
bi.iImage=IDR_MAINFRAME;
LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框


if(pIDList)
{
SHGetPathFromIDList(pIDList, Buffer);
//取得文件夹路径到Buffer里
fileSrc = Buffer;//将文件夹路径保存在一个CString对象里
}


LPMALLOC lpMalloc;
if(FAILED(SHGetMalloc(&lpMalloc))) 
{
return ;
}


//释放内存
lpMalloc->Free(pIDList);
lpMalloc->Release();


//show 
SetDlgItemText(IDC_EDIT_FILE_PATH, fileSrc);
UpdateData();
}




void CFileHelperDlg::showInformation(CString strInfo)
{
m_listBox.AddString(strInfo);
UpdateData();
}


void CFileHelperDlg::OnBnClickedButtonMatch()
{
// TODO: Add your control notification handler code here
TraverseFolder(MATCH_FILE);
}




void CFileHelperDlg::OnBnClickedButtonFind()
{
// TODO: Add your control notification handler code here
TraverseFolder(FIND_FILE);
}




void CFileHelperDlg::OnBnClickedButtonDelete()
{
// TODO: Add your control notification handler code here
TraverseFolder(DELITE_FILE);
}




void CFileHelperDlg::OnBnClickedButtonReplace()
{
// TODO: Add your control notification handler code here
TraverseFolder(REPLACE_FILE);
}




void CFileHelperDlg::OnBnClickedButtonDeleteFileByName()
{
// TODO: Add your control notification handler code here
TraverseFolder(DELETE_BY_PARTNAME);
}




void CFileHelperDlg::OnBnClickedButtonDeleteFileByExtension()
{
// TODO: Add your control notification handler code here
TraverseFolder(DELETE_BY_EXTENSION);
}




void CFileHelperDlg::OnBnClickedButtonInsertInFront()
{
// TODO: Add your control notification handler code here
TraverseFolder(INSERT_FRONT);
}




void CFileHelperDlg::OnBnClickedButtonInsertInBack()
{
// TODO: Add your control notification handler code here
TraverseFolder(INSERT_BACK);
}




HBRUSH CFileHelperDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);


// TODO:  Change any attributes of the DC here
if(pDC->GetWindow() == GetDlgItem(IDC_LIST_INFO))
 {
pDC->SetTextColor(RGB(255, 0, 0));
 }
return hbr;
}






void CFileHelperDlg::OnBnClickedCheckDisplay()
{
// TODO: Add your control notification handler code here
UpdateData();
}




int CFileHelperDlg::TraverseFolder(int flag)
{
m_listBox.ResetContent();
UpdateData();
m_strOperteLine.Replace("\r", "");
CFileManager *pManager = new CFileManager(m_strMatchLine.GetBuffer(0), m_strOperteLine.GetBuffer(0));
int nRet = pManager->TraverseFolder(m_strFilePath.GetBuffer(0), flag, this);
delete pManager;


switch(nRet)
{
case ENFORCE_SUCCESSED:
m_state.SetWindowText("finished!");
break;
case ENFORCE_FAILED:
m_state.SetWindowText("path can not be empty!");
break;
case PARAMETER_ERROR:
m_state.SetWindowText("failed!");
break;
default:
break;
}
return ENFORCE_SUCCESSED;
}


0 0