#include #include #include #include #include #include #define ID_DIR 914 #define ID_BTN 879 #define ID_CBOX 806 #define ID_RES 435 #define MAX_CHARS 260 #define MAX_LENGTH 32767 TCHAR dNames[MAX_PATH]; // computer name DWORD dLen = MAX_PATH; // length DWORD ldResult; TCHAR buf[MAX_CHARS], path[MAX_CHARS], newPath[MAX_CHARS], mask[MAX_CHARS], res[MAX_LENGTH]; WIN32_FIND_DATA dirInfo; LONG WINAPI WndProc(HWND, UINT, WPARAM,LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS w; memset(&w, 0, sizeof(WNDCLASS)); w.style = WS_TILED | CS_HREDRAW | CS_VREDRAW; w.lpfnWndProc = WndProc; w.hInstance = hInstance; w.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); w.lpszClassName = L"Lab2"; w.hIcon = LoadIcon(NULL, IDI_EXCLAMATION); w.hCursor = LoadCursor(NULL, IDC_ARROW); RegisterClass(&w); hwnd = CreateWindow(L"Lab2", L"Nepryakhin 3121", WS_OVERLAPPEDWINDOW, 400, 400, 360, 360, NULL, NULL, hInstance, NULL); HWND hCbox = CreateWindow(L"Combobox", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST, 5, 5, 50, 240, hwnd, (HMENU)ID_CBOX, hInstance, 0); HWND hDir = CreateWindow(L"Edit", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_LEFT, 60, 6, 200, 20, hwnd, (HMENU)ID_DIR, hInstance, 0); HWND hBtn = CreateWindow(L"Button", NULL, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER, 265, 6, 80, 20, hwnd, (HMENU)ID_BTN, hInstance, 0); HWND hRes = CreateWindow(L"Edit", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE | ES_READONLY | ES_AUTOVSCROLL, 5, 32, 340, 290, hwnd, (HMENU)ID_RES, hInstance, 0); SetWindowText(hBtn, L"Задать"); SetWindowText(hDir, L"windows"); SetWindowText(hRes, L"Select disk, type path of directory in textbox and click button to get all .cpp and .txt files in its oldest subdirectory."); ldResult = GetLogicalDriveStrings(dLen, dNames); // get drive names if (ldResult > 0 && ldResult <= MAX_PATH) // validation { TCHAR* sDrive = dNames; while (*sDrive) // add each drive to combobox { SendMessage(hCbox, CB_ADDSTRING, 0, (LPARAM) sDrive); sDrive += wcslen(sDrive) + 1; } } SendMessage(hCbox, CB_SETCURSEL, 0, 0); // select first item ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } bool checkDir() // only directory, exlude . and .. { return ((dirInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && _tcscmp(dirInfo.cFileName, L".") != 0 && _tcscmp(dirInfo.cFileName, L"..") != 0); } bool GetAncientDir() { bool result = false; // success flag FILETIME cur; // flush memory, get first dir ZeroMemory(&dirInfo, sizeof(WIN32_FIND_DATA)); StringCbPrintf(mask, MAX_CHARS, L"%s\\*", path); HANDLE hFind = FindFirstFile(mask, &dirInfo); if (checkDir()) // its ok dir { // set time as default cur = dirInfo.ftCreationTime; StringCbPrintf(newPath, MAX_CHARS, L"%s\\%s", path, dirInfo.cFileName); result = true; // there is one dir at least, ok } while (FindNextFile(hFind, &dirInfo)) // for each file if (checkDir() && CompareFileTime(&cur, &dirInfo.ftCreationTime) > -1) // check if its dir and its creation time { cur = dirInfo.ftCreationTime; // new time, new path StringCbPrintf(newPath, MAX_CHARS, L"%s\\%s", path, dirInfo.cFileName); result = true; // flag ok } FindClose(hFind); // close handle return result; } void GetFiles(LPCWSTR ext) { // flush memory, find first file with specified extension ZeroMemory(&dirInfo, sizeof(WIN32_FIND_DATA)); StringCbPrintf(mask, MAX_CHARS, L"%s\\*.%s", newPath, ext); HANDLE hFind = FindFirstFile(mask, &dirInfo); if(hFind == INVALID_HANDLE_VALUE) // no file StringCbPrintf(res, MAX_LENGTH, L"%s\r\nThere's no .%s files\r\n", res, ext); else { StringCbPrintf(res, MAX_LENGTH, L"%s\r\n.%s files:\r\n%s\r\n", res, ext, dirInfo.cFileName); // first one while (FindNextFile(hFind, &dirInfo)) // get more StringCbPrintf(res, MAX_LENGTH, L"%s%s\r\n", res, dirInfo.cFileName); } FindClose(hFind); // close handle } LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam) { switch (Message){ case WM_DESTROY: PostQuitMessage(0); break; case WM_COMMAND: if (HIWORD(wparam) == BN_CLICKED && LOWORD(wparam) == ID_BTN) { // get combobox drive letter and directory, concatenate GetDlgItemText(hwnd, ID_DIR, buf, MAX_CHARS); GetDlgItemText(hwnd, ID_CBOX, path, MAX_CHARS); StringCbPrintf(path, MAX_CHARS, L"%s%s", path, buf); // flush memory and find directory ZeroMemory(&dirInfo, sizeof(WIN32_FIND_DATA)); FindFirstFile(path, &dirInfo); // it's found if (dirInfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { if(!GetAncientDir()) // get oldest subdir StringCbPrintf(res, MAX_LENGTH, L"There is no subdirectories in %s", path); else { StringCbPrintf(res, MAX_LENGTH, L"Files in %s\r\n", newPath); GetFiles(L"txt"); // get all .txt files GetFiles(L"cpp"); // .cpp } } else StringCbPrintf(res, sizeof(res), L"Can't find or open directory."); // type result in textbox SetDlgItemText(hwnd, ID_RES, res); } break; default: return DefWindowProc(hwnd, Message, wparam, lparam); } return 0; }