⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 replacedlg.cpp

📁 替换多个文件中的文本的程序
💻 CPP
字号:

///********************************************************
//                               \\\|///
//                             \\  - -  //
//                              (  @ @  )
//┏━━━━━━━━━━━━━oOOo-(_)-oOOo━━━━━━━━━━━━┓
//┃☆☆☆☆☆☆   ≈≈≈大家好!我是【王晓钰】≈≈≈  ☆☆☆☆☆☆┃
//┃№№№№$$$$№№№№$$$$№№№№$$$$№№№№$$$ ┃
//┃¤¤※※※※ ≈≈≈≈≈≈谢谢大家的厚爱≈≈≈≈≈≈※※※※¤¤┃
//┃★★★★★主页“笑语小园”:http://xiaoyuyeah.yeah.net★★★★★┃
//┃★★★★★主页“笑语小园”:http://xiaoyuyeah.126.com ★★★★★┃
//┃★★★★★☆☆电子邮件:   Garden.XiaoYu@263.net  ☆☆★★★★★┃
//┃★★★★★OICQ: 285126                               ★★★★★┃
//┃☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ ┃
//┃                                   Oooo〖王晓钰〗2001年02月12日┃
//┗━━━━━━━━━━━━━━oooO---(  )━━━━━━━━━━━━┛
//                             (   )   ) /
//                              \ (   (_/
//                               \_)
///*******************************************************
/// replaceDlg.cpp : implementation file
//

#include "stdafx.h"
#include "replace.h"
#include "replaceDlg.h"
#include "hyperlink.h"
#include "input.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CHyperLink	m_web2;
	CHyperLink	m_web1;
	CHyperLink	m_mail;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDC_XY_WEB2, m_web2);
	DDX_Control(pDX, IDC_XY_WEB1, m_web1);
	DDX_Control(pDX, IDC_XY_MAIL, m_mail);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReplaceDlg dialog

CReplaceDlg::CReplaceDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CReplaceDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CReplaceDlg)
	m_dir = _T("");
	m_isSub = FALSE;
	m_log = _T("");
	m_lookfor = _T("");
	m_replace = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CReplaceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReplaceDlg)
	DDX_Control(pDX, IDC_XY_COMBO, m_combBox);
	DDX_Text(pDX, IDC_XY_DIR, m_dir);
	DDX_Check(pDX, IDC_XY_INCSUB, m_isSub);
	DDX_Text(pDX, IDC_XY_LOG, m_log);
	DDX_Text(pDX, IDC_XY_LOOKFOR, m_lookfor);
	DDX_Text(pDX, IDC_XY_REPLACE, m_replace);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CReplaceDlg, CDialog)
	//{{AFX_MSG_MAP(CReplaceDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_XY_TYPE_ADD, OnXyTypeAdd)
	ON_BN_CLICKED(IDC_XY_TYPE_DEL, OnXyTypeDel)
	ON_BN_CLICKED(IDC_XY_VIEWDIR, OnXyViewdir)
	ON_BN_CLICKED(IDC_XY_VIEWLOG, OnXyViewlog)
	ON_BN_CLICKED(IDC_XY_INCSUB, OnXyIncsub)
	ON_BN_CLICKED(IDABOUT, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReplaceDlg message handlers

BOOL CReplaceDlg::OnInitDialog()
{
	CDialog::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)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CReplaceDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::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 CReplaceDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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
	{
		CDialog::OnPaint();
	}
}

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

void CReplaceDlg::OnXyTypeAdd() 
{
	// TODO: Add your control notification handler code here
	CInput inDlg;
	if(inDlg.DoModal())
	{	m_combBox.AddString(inDlg.m_input);
		m_combBox.SetCurSel(m_combBox.GetCount()-1);
		UpdateData(false);
	}
}

void CReplaceDlg::OnXyTypeDel() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	int	index=m_combBox.GetCurSel();
	m_combBox.DeleteString(index);
	m_combBox.SetCurSel(index-1);
	UpdateData(false);
}

void CReplaceDlg::OnXyViewdir() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	char directory[50];
	if(!SelectDirDlg(directory))
	{	m_dir=directory;
		m_log=m_dir+"\\log.log";
		UpdateData(false);
	}
	
}

void CReplaceDlg::OnXyViewlog() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString filter;
	filter="记录文件(*.log)|*.log|所有文件(*.*)|*.*||";
	CFileDialog dlg(FALSE,"*.log",m_log,
		OFN_LONGNAMES|OFN_OVERWRITEPROMPT ,filter,this);
	if(dlg.DoModal()==IDOK )
	{	m_log=dlg.GetPathName();
		UpdateData(false);
	}

}

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

void CReplaceDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();
	log.Empty();
	count=0;
	if(m_isSub)
		multiRep(m_dir,m_lookfor,m_replace);
	else
		dirRep(m_dir,m_lookfor,m_replace);
	UpdateData(false);
	//write log file
	CFile	file;
	file.Open(m_log,CFile::modeCreate|CFile::modeWrite);
	file.Write(LPCTSTR(log),log.GetLength());
	file.Close();
	MessageBox("处理完毕");
}

int CReplaceDlg::SelectDirDlg(char Dir[])
{	BROWSEINFO bi; 
	static ITEMIDLIST* pidl; 

	bi.hwndOwner=NULL; 
	bi.pidlRoot=NULL; 
	bi.pszDisplayName=Dir; 
	bi.lpszTitle="请选择要提取目录的文件夹";
	bi.ulFlags=BIF_RETURNONLYFSDIRS; 
	bi.lpfn=NULL; 
	bi.lParam=0; 
	bi.iImage=0; 

	pidl=SHBrowseForFolder(&bi);/*Display"SelectFolder"dialogbox,Get the foldernameandconvertit 
	intoaITEMLIST 	datastructure.*/ 
	if(pidl==NULL) 
	{	Dir[0]=0; 
		return 1;
	}
	if(!SHGetPathFromIDList(pidl,Dir))/*Retrievefoldername from ITEMLIST structure.*/ 
	{	Dir[0]=0; return 2;}
	return 0;
}

int CReplaceDlg::fileRep(CString fileName, CString find, CString replace)
{	CFile	file;
	CFileStatus	status;
	CString	tmpStr;
	if(!file.GetStatus(fileName,status))
		return 1;	//file not exist;
	if(!file.Open(fileName,CFile::modeReadWrite|CFile::shareDenyWrite))
		return 2;	//error in read;
	char*	fileChar;
	//char	newline[3];
	//newline[0]=0x0d;	newline[1]=0x0A;	newline[2]='0';
	DWORD	len=file.GetLength();
	fileChar=new char[len];
	len=file.Read(fileChar,len);
	CString	fileStr(fileChar,len);
	int	findIndex;
	do
	{	findIndex=fileStr.Replace(find,replace);
		if(findIndex)
		{	count++;
			tmpStr.Empty();
			tmpStr.Format("\x0d\x0a第%d个文件:%s\x0d\x0a\t“",count,fileName);
			tmpStr+=find;
			tmpStr+="”\x0d\x0a\t“";
			tmpStr+=replace;
			tmpStr+="”";
			log+=tmpStr;
		}
	}while(findIndex);
	file.SeekToBegin();
	file.Write(LPCTSTR(fileStr),fileStr.GetLength());
	file.Close();
	return 0;
}

int CReplaceDlg::dirRep(CString fileName, CString find, CString replace)
{	CFileFind	finder;
	CString	findStr,fileStr;
	BOOL	findReturn;
	if(fileName.Right(2)=="..")	return	1;
	findStr=fileName+"\\";
	for(int	i=0;i<m_combBox.GetCount();i++)
	{	m_combBox.GetLBText(i,fileStr);
		findReturn=finder.FindFile(findStr+fileStr);
		while(findReturn)
		{	findReturn=finder.FindNextFile();
			fileRep(finder.GetFilePath(),find,replace);
		}
	}
	return 0;
}

int CReplaceDlg::multiRep(CString fileName, CString find, CString replace)
{	CFileFind	finder;
	BOOL	findReturn;
	findReturn=finder.FindFile(fileName+"\\*.*");
	while(findReturn)
	{	findReturn=finder.FindNextFile();
		if(finder.IsDots())
			dirRep(finder.GetFilePath(),find,replace);
		else
			multiRep(finder.GetFilePath(), find,replace);
	}
	return 0;

}

void CReplaceDlg::OnAbout() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg	dlg;
	dlg.DoModal();
}

int CAboutDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	m_web1.SetURL("http://XiaoyuYeah.yeah.net");
	m_web2.SetURL("http://XiaoyuYeah.126.com");
	m_mail.SetURL("mail to:Garden.XiaoYu@263.net");
	
	// TODO: Add your specialized creation code here
	
	return 0;
}

///********************************************************
//                               \\\|///
//                             \\  - -  //
//                              (  @ @  )
//┏━━━━━━━━━━━━━oOOo-(_)-oOOo━━━━━━━━━━━━┓
//┃☆☆☆☆☆☆   ≈≈≈大家好!我是【王晓钰】≈≈≈  ☆☆☆☆☆☆┃
//┃№№№№$$$$№№№№$$$$№№№№$$$$№№№№$$$ ┃
//┃¤¤※※※※ ≈≈≈≈≈≈谢谢大家的厚爱≈≈≈≈≈≈※※※※¤¤┃
//┃★★★★★主页“笑语小园”:http://xiaoyuyeah.yeah.net★★★★★┃
//┃★★★★★主页“笑语小园”:http://xiaoyuyeah.126.com ★★★★★┃
//┃★★★★★☆☆电子邮件:   Garden.XiaoYu@263.net  ☆☆★★★★★┃
//┃★★★★★OICQ: 285126                               ★★★★★┃
//┃☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ ┃
//┃                                   Oooo〖王晓钰〗2001年02月12日┃
//┗━━━━━━━━━━━━━━oooO---(  )━━━━━━━━━━━━┛
//                             (   )   ) /
//                              \ (   (_/
//                               \_)
///*******************************************************
/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -