在指定路径、文件或者默认当前目录下的所有文件中,查找某个字符串

#include <windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;

int _GetPosInFile(const char * pFileName,const char * pDstContent,const wchar_t *wszcontent)
{
	HANDLE hFile = CreateFileA(pFileName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
	if (hFile == INVALID_HANDLE_VALUE)
	{
		return FALSE;
	}

	int asclen = lstrlenA(pDstContent);

	int iSize = GetFileSize(hFile,0);
	if (iSize < asclen)
	{
		CloseHandle(hFile);
		return FALSE;
	}

	char * pFileBuf = new char [iSize + 1024];

	DWORD dwCnt = 0;
	int iRes = ReadFile(hFile,pFileBuf,iSize,&dwCnt,0);
	CloseHandle(hFile);
	if (iRes == 0)
	{
		delete [] pFileBuf;
		return 0;
	}

	char * pFilePtr = pFileBuf;

	for (int i = 0; i <= iSize - asclen; i ++,pFilePtr ++)
	{
		if (memcmp(pDstContent,pFilePtr, asclen) == 0)
		{
			int iFilePos = pFilePtr - pFileBuf;
			 
			delete [] pFileBuf;

			return iFilePos;
		}
	}


	pFilePtr = pFileBuf;
	int unilen = wcslen(wszcontent);
	for (int i = 0; i <= iSize - unilen * 2; i++, pFilePtr++)
	{
		if (memcmp((char*)wszcontent, pFilePtr, unilen * 2) == 0)
		{
			int iFilePos = pFilePtr - pFileBuf;

			delete[] pFileBuf;

			return iFilePos;
		}
	}
	
	delete [] pFileBuf;
	return FALSE;
}

int _FindFilesInDir(const char * PreStrPath, int iLayer,const char * pDstContent,const wchar_t * wszcontent)   
{   
	int ret = 0;

	static int result = 0;

	int prepathlen = lstrlenA(PreStrPath);

	char strPath[1024] = {0};
	memcpy(strPath,PreStrPath, prepathlen);
	memcpy(strPath + prepathlen,"\\*.*",lstrlenA("\\*.*"));


	WIN32_FIND_DATAA stWfd = { 0 };
	HANDLE hFind = FindFirstFileA(strPath,&stWfd);
	if(hFind== INVALID_HANDLE_VALUE)
	{
		printf("FindFirstFileA:%s error\r\n",strPath);
		return 0;  
	}

	char szLastDir[] = { '.','.',0 };
	char szCurDir[] = { '.',0 };
	do
	{   
		if (stWfd.dwFileAttributes ==FILE_ATTRIBUTE_DIRECTORY) 
		{  
			if (lstrcmpiA(stWfd.cFileName, szLastDir) == 0 || lstrcmpiA(stWfd.cFileName, szCurDir) == 0)
			{
				continue;
			}

			char strNextPath[1024] = {0};
			memcpy(strNextPath, PreStrPath,prepathlen);
			*(strNextPath + prepathlen) = 0x5c;
			memcpy(strNextPath + prepathlen + 1,stWfd.cFileName,lstrlenA(stWfd.cFileName));
			ret = _FindFilesInDir(strNextPath,iLayer+1,pDstContent,wszcontent);  
			if (ret)
			{
				//
			}
		}   
		else     
		{   
			char szFileName[1024] = {0};
			memcpy(szFileName,PreStrPath, prepathlen);
			*(szFileName + prepathlen) = 0x5c;
			memcpy(szFileName + prepathlen + 1,stWfd.cFileName ,lstrlenA(stWfd.cFileName));
			int iFilePos = _GetPosInFile(szFileName,pDstContent,wszcontent);
			if (iFilePos)
			{
				printf("found %s in file:%s,position on:%u,searching next...\r\n", pDstContent,szFileName,iFilePos);
				result++;
			}
		}  
	} while(FindNextFileA(hFind,&stWfd)) ;

	FindClose(hFind);

	return result;
}

int _findInFile(const char * fn,const char * szcontent,wchar_t * wszcontent) {
	int ret = _GetPosInFile(fn, szcontent, wszcontent);
	return ret;
}

int main(int argc, char ** argv)
{
	int ret = 0;

	if ( argc < 2 )
	{
		printf("parameter wrong!\r\n");
		printf("usage:F:\\android\\apktool\\pptreader\\smali Activity\r\n");
		printf("usage:Activity\r\n");
		getchar();
		return FALSE;
	}

	wchar_t wszcontent[1024] = { 0 };
	if (argc == 2)
	{
		MultiByteToWideChar(CP_ACP,0,argv[1],-1,wszcontent, 1024);

		char szcurdir[MAX_PATH];
		GetCurrentDirectoryA(MAX_PATH,szcurdir);
		
		printf("starting search string:\"%s\" in path:\"%s\",please wait...\r\n\r\n",argv[1],szcurdir);

		ret = _FindFilesInDir(szcurdir,1,argv[1],wszcontent);

	}else if (argc >= 3)
	{
		string path = argv[1];
		if (path.back() == '/' || path.back() == '\\')
		{
			path = path.substr(0, path.length() - 1);
		}
		MultiByteToWideChar(CP_ACP,0,argv[2],-1,wszcontent, 1024);

		printf("starting search string:\"%s\" in path:\"%s\",please wait...\r\n\r\n",argv[2],path.c_str());

		ret = GetFileAttributesA(path.c_str());
		if (ret & FILE_ATTRIBUTE_DIRECTORY)
		{
			ret = _FindFilesInDir(path.c_str(), 1, argv[2], wszcontent);
		}
		else {
			ret = _GetPosInFile(path.c_str(), argv[2], wszcontent);
		}
		
	}
	
	if (ret == 0)
	{
		printf("sorry to not found string in path\r\n");
		getchar();
		return FALSE;
	}
	else
	{
		printf("work completely successfully found result:%u\r\n",ret);
		getchar();
		return TRUE;
	}
	return TRUE;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值