Click here to Skip to main content
15,895,799 members
Articles / Desktop Programming / MFC

ScanR - Text file string search and replace engine

Rate me:
Please Sign up or sign in to vote.
4.30/5 (7 votes)
3 Jul 20035 min read 87.2K   3.7K   35  
A set of classes that allows you to search for strings in text files and replace them.
#include "stdafx.h"
#include "ScanR.h"
#include "ScanRDlg.h"

#include "CleanR.h"
#include "GetFileList.h"

#include <shlobj.h> //For shell folder


#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 };
	//}}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
	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)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScanRDlg dialog

CScanRDlg::CScanRDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CScanRDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CScanRDlg)
	m_sSearchString = _T("");
	m_sExtension = _T("*.*"); //Default is *.*
	m_sDirWildcard = _T("");
	m_sDriveName = _T("G:");
	m_dwMaxFileSize = (1024*1024);
	m_sReplacementString = _T("");
	m_bStrict = FALSE;
	m_bCheckOnlyWords = FALSE;
	m_bLastWordMayNotHavePunct = FALSE;
	m_bCaseSensetive = TRUE;
	m_bOverWriteFile = FALSE;
	m_bReplaceLFWithCRLF = FALSE;
	m_bReplaceCRLFWithLF = FALSE;
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CScanRDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScanRDlg)
	DDX_Text(pDX, IDC_EDIT1, m_sSearchString);
	DDX_Text(pDX, IDC_EDIT3, m_sExtension);
	DDX_Text(pDX, IDC_EDIT4, m_sDirWildcard);
	DDX_Text(pDX, IDC_EDIT5, m_sDriveName);
	DDX_Text(pDX, IDC_EDIT6, m_dwMaxFileSize);
	DDX_Text(pDX, IDC_EDIT2, m_sReplacementString);
	DDX_Check(pDX, IDC_CHECK1, m_bStrict);
	DDX_Check(pDX, IDC_CHECK2, m_bCheckOnlyWords);
	DDX_Check(pDX, IDC_CHECK3, m_bLastWordMayNotHavePunct);
	DDX_Check(pDX, IDC_CHECK4, m_bCaseSensetive);
	DDX_Check(pDX, IDC_CHECK5, m_bOverWriteFile);
	DDX_Check(pDX, IDC_CHECK6, m_bReplaceLFWithCRLF);
	DDX_Check(pDX, IDC_CHECK7, m_bReplaceCRLFWithLF);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CScanRDlg, CDialog)
	//{{AFX_MSG_MAP(CScanRDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_EDIT1, OnChangeSearchString)
	ON_EN_CHANGE(IDC_EDIT3, OnChangeFileExtension)
	ON_EN_CHANGE(IDC_EDIT4, OnChangeDirWildcard)
	ON_EN_CHANGE(IDC_EDIT5, OnChangeDriveName)
	ON_BN_CLICKED(IDC_BUTTON1, OnDrivePathName)
	ON_EN_CHANGE(IDC_EDIT6, OnChangeMaxFileSize)
	ON_EN_CHANGE(IDC_EDIT2, OnReplacementString)
	ON_BN_CLICKED(IDC_CHECK1, OnStrict)
	ON_BN_CLICKED(IDC_CHECK2, OnWordsOnly)
	ON_BN_CLICKED(IDC_CHECK3, OnLastPunct)
	ON_BN_CLICKED(IDC_CHECK4, OnCaseSensetive)
	ON_BN_CLICKED(IDC_CHECK5, OnOverwriteFile)
	ON_BN_CLICKED(IDC_CHECK6, OnReplaceLFtoCRLF)
	ON_BN_CLICKED(IDC_CHECK7, OnCRLFtoLF)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScanRDlg message handlers

BOOL CScanRDlg::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);
		}
	}

	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 CScanRDlg::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 CScanRDlg::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();
	}
}

HCURSOR CScanRDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CScanRDlg::OnChangeSearchString() 
{
	UpdateData();	
}

void CScanRDlg::OnChangeFileExtension() 
{
	UpdateData();	
}

void CScanRDlg::OnChangeDirWildcard() 
{
	UpdateData();
	if(m_sDirWildcard.FindOneOf("\\")!=-1) 
	{
		//We cannot allow anything EXCEPT a SINGLE directory wildcard
		MessageBox("It seems that you are entering a (sub)path string.You can enter only the wildcard for ONE directory.\nFor e.g a value of \"New*\" is valid, but \"Something\\New*\" is not.\nIf you want to specify a path, enter it into the \"Drive(path)\" edit box","Invalid value detected!",MB_YESNOCANCEL|MB_ICONINFORMATION);
		m_sDirWildcard=m_sDirWildcard.Left(m_sDirWildcard.FindOneOf("\\"));
		GetDlgItem(IDC_EDIT4)->SetWindowText(m_sDirWildcard);//Reflect the corrected contents into the edit box.
	}
	UpdateData();
}

void CScanRDlg::OnChangeDriveName() 
{
	UpdateData();	
}

void CScanRDlg::OnDrivePathName() 
{
	GetDlgItem(IDC_EDIT5)->SetWindowText("");

	TCHAR dname[MAX_PATH];
	TCHAR szSelectedFolder[MAX_PATH];

	IMalloc *imalloc;
	SHGetMalloc(&imalloc);
	BROWSEINFO bi; 
	ZeroMemory(&bi,sizeof(bi));
	bi.pszDisplayName=dname;
	bi.lpszTitle=TEXT("Here you can select a specific (sub)directory under which this program will base it's search on.");
	bi.ulFlags=BIF_RETURNONLYFSDIRS;
	LPITEMIDLIST lpItemIDList=SHBrowseForFolder(&bi);
	//Displays the selected folder
	if (lpItemIDList!=NULL)	SHGetPathFromIDList(lpItemIDList,szSelectedFolder);
	imalloc->Free(lpItemIDList);
	imalloc->Release();
	m_sDriveName=szSelectedFolder;
	GetDlgItem(IDC_EDIT5)->SetWindowText(szSelectedFolder);

	UpdateData();	
}

void CScanRDlg::OnChangeMaxFileSize() 
{
	UpdateData();	
}
void CScanRDlg::OnReplacementString() 
{
	UpdateData();	
}
void CScanRDlg::OnStrict() 
{
	UpdateData();
}
void CScanRDlg::OnWordsOnly() 
{
	UpdateData();	
}
void CScanRDlg::OnLastPunct() 
{
	UpdateData();
}
void CScanRDlg::OnCaseSensetive() 
{
	UpdateData();
}
void CScanRDlg::OnOverwriteFile() 
{
	UpdateData();	
}
void CScanRDlg::OnReplaceLFtoCRLF() 
{
	UpdateData();	
}
void CScanRDlg::OnCRLFtoLF() 
{
	UpdateData();
}

void CScanRDlg::OnOK() 
{
	/*
	Files with m_sExtension wildcard matching are passed to an CCleanR object.
	If return value of .Process() is nonzero, implies that the search string exists in the file, and it's added to the list.
	*/
	if(m_sSearchString=="")
	{
		MessageBox("Please enter a search string to base file search upon!","Search string value missing",MB_OK|MB_ICONSTOP);
		return;
	}
	if(m_sExtension=="")
	{
		MessageBox("Please enter the filename wildcard to base file search upon!","Filename wildcard missing",MB_OK|MB_ICONSTOP);
		return;
	}
	if(m_sDirWildcard=="")	MessageBox("The Directory wildcard is NULL.\n\nThis program interprets this information as the user wanting to scan the root directory also.\n\nIf you want to scan only subdirectories, set this to \"*\" ","Directory wildcard NULL",MB_OK|MB_ICONINFORMATION);
	
	if(m_sDriveName=="")
	{
		MessageBox("The Drive name is empty.\n\nA valid example is \"C:\\\" ","Drive name empty.",MB_OK|MB_ICONINFORMATION);
		return;
	}
	
	char szMsg[512];
	SetSearchReplaceString((LPCTSTR)m_sSearchString,(LPCTSTR)m_sReplacementString);//Quick hack function
	CCleanRboolSet theSet;
	theSet.bStrict=m_bStrict;
	theSet.bCheckOnlyWords=m_bCheckOnlyWords;
	theSet.bLastWordMayNotHavePunct=m_bLastWordMayNotHavePunct;
	theSet.bCaseSensetive=m_bCaseSensetive;
	theSet.bOverWriteFile=m_bOverWriteFile;
	theSet.bReplaceLFWithCRLF=m_bReplaceLFWithCRLF;
	theSet.bReplaceCRLFWithLF=m_bReplaceCRLFWithLF;
	CGetFileList oC((LPCTSTR)m_sDriveName,(LPCTSTR)m_sDirWildcard,(LPCTSTR)m_sExtension,m_dwMaxFileSize,theSet);
	DWORD nInfectedFiles=oC.GetFileCount();
	wsprintf(szMsg,"Scan of %s completed!\nFound %d matching file(s).\n\nDetails have been logged to InfectedFiles.LOG",(LPCTSTR)m_sDriveName,nInfectedFiles);
	MessageBox(szMsg,"Text file search and replace utility results - Kamal Shankar.",MB_OK|MB_ICONINFORMATION);

	CDialog::OnOK();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Kamal Shankar is a programming freak (or so he feels).He currently lives in the Salt Lake City and loves doing what he has been since 1990 - coding horribly Wink | ;)

Comments and Discussions