Windows下列出所有檔案 FindFirstFile

來源:互聯網
上載者:User
#include <windows.h>#include <tchar.h> #include <stdio.h>#include <strsafe.h>#pragma comment(lib, "User32.lib")void DisplayErrorBox(LPTSTR lpszFunction);int _tmain(int argc, TCHAR *argv[]){   WIN32_FIND_DATA ffd;   LARGE_INTEGER filesize;   TCHAR szDir[MAX_PATH];   size_t length_of_arg;   HANDLE hFind = INVALID_HANDLE_VALUE;   DWORD dwError=0;      // If the directory is not specified as a command-line argument,   // print usage.   if(argc != 2)   {      _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);      return (-1);   }   // Check that the input path plus 3 is not longer than MAX_PATH.   // Three characters are for the "\*" plus NULL appended below.   StringCchLength(argv[1], MAX_PATH, &length_of_arg);   if (length_of_arg > (MAX_PATH - 3))   {      _tprintf(TEXT("\nDirectory path is too long.\n"));      return (-1);   }   _tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);   // Prepare string for use with FindFile functions.  First, copy the   // string to a buffer, then append '\*' to the directory name.   StringCchCopy(szDir, MAX_PATH, argv[1]);   StringCchCat(szDir, MAX_PATH, TEXT("\\*"));   // Find the first file in the directory.   hFind = FindFirstFile(szDir, &ffd);   if (INVALID_HANDLE_VALUE == hFind)    {      DisplayErrorBox(TEXT("FindFirstFile"));      return dwError;   }       // List all the files in the directory with some info about them.   do   {      if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)      {         _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);      }      else      {         filesize.LowPart = ffd.nFileSizeLow;         filesize.HighPart = ffd.nFileSizeHigh;         _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);      }   }   while (FindNextFile(hFind, &ffd) != 0);    dwError = GetLastError();   if (dwError != ERROR_NO_MORE_FILES)    {      DisplayErrorBox(TEXT("FindFirstFile"));   }   FindClose(hFind);   return dwError;}void DisplayErrorBox(LPTSTR lpszFunction) {     // Retrieve the system error message for the last-error code    LPVOID lpMsgBuf;    LPVOID lpDisplayBuf;    DWORD dw = GetLastError();     FormatMessage(        FORMAT_MESSAGE_ALLOCATE_BUFFER |         FORMAT_MESSAGE_FROM_SYSTEM |        FORMAT_MESSAGE_IGNORE_INSERTS,        NULL,        dw,        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),        (LPTSTR) &lpMsgBuf,        0, NULL );    // Display the error message and clean up    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,         (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));     StringCchPrintf((LPTSTR)lpDisplayBuf,         LocalSize(lpDisplayBuf) / sizeof(TCHAR),        TEXT("%s failed with error %d: %s"),         lpszFunction, dw, lpMsgBuf);     MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);     LocalFree(lpMsgBuf);    LocalFree(lpDisplayBuf);}

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.