Guest User

CheckIfFileExist

a guest
Jun 3rd, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. /* Credits: w2ch Anon */
  2. /* File:CheckIfFileExist.cpp */
  3.  
  4. #include <windows.h>
  5. #include <stdio.h>
  6.  
  7.  
  8. bool DoesFileExist(const char *szpFilePath, const char *szFileName)
  9. {
  10.     WIN32_FIND_DATA ffd;
  11.     char szDir[MAX_PATH];
  12.     HANDLE hFind = INVALID_HANDLE_VALUE;
  13.     DWORD dwRet;
  14.  
  15.  
  16.     /* If no file to search for: Return false. */
  17.     if (szFileName == NULL) return false;
  18.  
  19.     /* Check that the input path plus 3 is not longer than MAX_PATH.
  20.      * Three characters are for the "\*" plus NULL appended below.
  21.      * At the same time prepare string for use with FindFile functions. */
  22.     if (szpFilePath == NULL)
  23.     {
  24.         /* If no szpFilePath, use the directory the file is in */
  25.         dwRet = GetCurrentDirectory((sizeof(szDir) / sizeof(*szDir)) - 3, szDir);
  26.  
  27.         /* ERROR: GetCurrentDirectory failed, GetLastError() */
  28.         if (dwRet == 0) return 0;
  29.         /* ERROR: Buffer(string) too small; need %d characters\n, */
  30.         if (dwRet > MAX_PATH - 3) return 0;
  31.     }
  32.     else
  33.     {
  34.         for (dwRet = 0; szpFilePath[dwRet] != '\0'; ++dwRet);
  35.         /* ERROR: Buffer(string) too small; need %d characters\n, */
  36.         if (dwRet > MAX_PATH - 3) return 0;
  37.         for (int n = dwRet; n; szDir[n] = szpFilePath[n], --n);
  38.         szDir[0] = szpFilePath[0];
  39.     }
  40.  
  41.     /* Add "\*" to the directory name. */
  42.     szDir[dwRet]   = '\\';
  43.     szDir[++dwRet] = '*';
  44.     szDir[++dwRet] = '\0';
  45.  
  46.  
  47.     /* Find the first file in the directory. */
  48.     hFind = FindFirstFileA(szDir, &ffd);
  49.     if (INVALID_HANDLE_VALUE == hFind) return 0; /* ERROR "FindFirstFile" */
  50.  
  51.     /* List all the files in the directory with some info about them. */
  52.     do
  53.     {
  54.         /* Only compare to files and not other objects, e.g: directorys */
  55.         if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  56.         {
  57.             /* Compare if ffd.cFileName match szFileName and if yes return true */
  58.             for (dwRet = 0; ffd.cFileName[dwRet] == szFileName[dwRet] && szFileName[dwRet]; ++dwRet);
  59.             if (!ffd.cFileName[dwRet])
  60.             {
  61.                 FindClose(hFind);
  62.                 return true;
  63.             }
  64.         }
  65.     } while (FindNextFile(hFind, &ffd) != 0);
  66.  
  67.     dwRet = GetLastError();
  68.     if (dwRet != ERROR_NO_MORE_FILES); // ERRPR: "FindFirstFile"
  69.  
  70.     /* If we return here we did not find the file we were looking for */
  71.     FindClose(hFind);
  72.     return false;
  73. }
  74.  
  75.  
  76. /* find if in the current directory there is a file named argv[0] */
  77. int main(int argc, char *argv[])
  78. {
  79.     bool bFileExist;
  80.     if (argc == 2)      bFileExist = DoesFileExist(NULL, argv[1]);
  81.     else if (argc == 3) bFileExist = DoesFileExist(argv[1], argv[2]);
  82.     else
  83.     {
  84.         fprintf(stdout, "Usages:%s [filepath] [file]", argv[0]);
  85.         return 0;
  86.     }
  87.  
  88.     if (bFileExist)
  89.     {
  90.         fprintf(stdout, "The file exist!");
  91.     }
  92.     else
  93.     {
  94.         fprintf(stdout, "The file dose not exist!");
  95.     }
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment