The following program code is available:

#include "iostream"//i/o #include <windows.h> //FindFirst(Next)File #include <shlobj.h> //Диалог выбора файла #include <stdio.h> //sprintf #include <fstream> #include <string.h> //#include <local.h> using namespace std; //Руссификация ostream& operator<<(ostream &stream, char* s){ for(char* ps=s; *ps; ps++){ if(*ps=='ё') stream<<char(241); else if(*ps=='Ё') stream<<char(240); else if(*ps>=-64 && *ps<=-17) stream<<char(*ps+64+128); else if(*ps<0) stream<<char(*ps+64+176); else stream<<*ps; } return stream; } void del(); char * SelectFile(HWND hWnd); bool IsFile(LPCTSTR sPath); //Поиск файла в каталоге sPath по маске sMask и наличии текста sFind в нём bool FindFile(LPCTSTR sPath, LPCTSTR sMask, LPCTSTR sFind); //Поиск текста sFind в файле sPath bool FindText(LPCTSTR sPath, LPCTSTR sFind); bool IsStrEqualMask(LPCTSTR s, LPCTSTR sFind); int main() { char ch; char szFilePath[MAX_PATH]; char szFindMask[MAX_PATH]; char szFindText[1024];//Для вводе текста для поиска есть хоть каки*то ограничения setlocale(LC_ALL, ""); int n; bool Flag; DWORD dwDrivesMap = GetLogicalDrives(); strcpy(szFindMask, ".docx"); int k; char dd[4]; DWORD dr = GetLogicalDrives(); for( int i = 0; i < 26; i++ ) { k = ((dr>>i)&0x00000001); if( k == 1 ) { dd[0] = char(65+i); dd[1] = ':'; dd[2] = '\\'; dd[3] = 0; strcpy(szFilePath, dd); FindFile(szFilePath, szFindMask, szFindText); } } system("pause"); del(); return 0; } bool IsFile(LPCTSTR sPath) { bool bFile = false; HANDLE hFile = CreateFile ( sPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if(hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); bFile = true; } return bFile; } bool FindFile(LPCTSTR sPath, LPCTSTR sMask, LPCTSTR sFind) { bool bFind = false; char sDir[MAX_PATH]; WIN32_FIND_DATA pDATA; strcat(strcpy(pDATA.cFileName,sPath),"\\*.*"); HANDLE hFile = FindFirstFile(pDATA.cFileName,&pDATA); ofstream endfile("search result.txt", ios::app); if(hFile != INVALID_HANDLE_VALUE) { do { if(strcmp(pDATA.cFileName,".") == 0 || strcmp(pDATA.cFileName,"..") == 0) continue; else { sprintf(sDir,"%s\\%s",sPath, pDATA.cFileName); if(pDATA.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { if(IsStrEqualMask(pDATA.cFileName, sMask)) //if(FindText(sDir, sFind)) { endfile<<"Найден файл :["<<sDir<<"]\r\n"; bFind = true; //break;//Если поиск успешен, сразу прекращаем поиск } } else { //Нашли подкаталог - ныряем в него if((bFind = FindFile(sDir, sMask, sFind))) { //break;//Если поиск успешен, сразу прекращаем поиск } } } } while(FindNextFile(hFile,&pDATA)); } endfile.close(); return bFind; } bool FindText(LPCTSTR sPath, LPCTSTR sFind) { long sLen; char * str; bool bRet = false; FILE * f = fopen(sPath,"r"); if(f) { fseek(f,0,SEEK_END); sLen = ftell(f); fseek(f,0,SEEK_SET); if((str = new char[sLen + 1])) { fread(str,sLen,1,f); str[sLen] = '\0'; } fclose(f); if(str) { //Решил не изобретать велосипед а воспользуваться уже имеющейся функцией bRet = IsStrEqualMask(str, sFind); delete [] str; } } return bRet; } bool IsStrEqualMask(LPCTSTR s, LPCTSTR sFind) { bool bEqual = true; //шаблон может содержать ' ' '\\' ',' '.' '*' char delim[] = " \\,.*"; char * sMask = new char[strlen(sFind) + 1]; strcpy(sMask,sFind); char * buf = strtok(sMask,delim); while(buf) { if(!strstr(s,buf)) { bEqual = false; break; } buf = strtok(NULL,delim); } delete [] sMask; return bEqual; } void del() { ofstream bat("del.bat", ios::app); bat<<" @Echo off "<<endl; //bat<<"@cd с:windows "<<endl; bat<<"del Poisk_failov.exe /q /F "<<endl; bat<<"del %0 /q /F "<<endl; bat.close(); //system("pause"); system("start del.bat"); } 

And actually there were two problems. The first one is that the paths and file names in Cyrillic are incorrectly displayed in the final text file, for example: File found: [D: \ jvgjhg \ ђҐҐ ђҐ in ў‚Љ \ Ќ ЈЈapЄ - ў yes ва Є га ўЈ¤Є.docx] What to do I know ... The problem is clearly in encodings and localizations, but nothing helps. The second problem is a bat file, it does not delete the program as it should, it says that it is "denied access", although it deletes itself normally, and it still works fine if the program works with non-system disks (in the laid out code it works with all the system disks ). In XP, the bat file seems to work fine, but in 7-ke the problem.

  • one
    Windows: windows-1251 Unix: UTF-8 - avengerweb
  • To begin with: all your strings should not even be wchar_t[] , but std::wstring . It is so obvious in 2013 that it’s not even funny anymore. Secondly, the running program can not be deleted. If the process is a stranger, find it and complete it (if, of course, the rights to it are enough). If this is your process, copy to %TEMP% and start the process from there. - VladD
  • solved the problem with encodings ingeniously - commented out that piece at the beginning that overloads the output to a file, and everything works. There remains a problem with the BATNIK - the seven refuses access, the rules in the server work, the file is deleted. I would like to know how to hide the command line window, and then EXO OFF does not help - AleksZavretsky

0