-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManagement.cpp
More file actions
135 lines (120 loc) · 3.27 KB
/
Copy pathFileManagement.cpp
File metadata and controls
135 lines (120 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "stdafx.h"
#include "Gr7Api.h"
#include <io.h>
#include <string.h>
#include <Shobjidl.h>
#include <shellapi.h>
#include <strsafe.h>
#include <wchar.h>
#include <Shlwapi.h>
#include <iostream>
// Function to get the drive letter where the OS is installed to
int Grass7API::FileManagement::GetSystemDriveLetter(wchar_t *driveletter)
{
std::wstring windir(MAX_PATH, 0);
UINT errWinDir = GetWindowsDirectoryW(&windir[0], (int)windir.size());
if (errWinDir == 0) {
return 1;
}
std::wstring windirWSTR = windir.c_str();
std::wstring driveletter1 = windirWSTR.substr(0, windirWSTR.find(L":"));
driveletter1.append(L":\\");
const wchar_t *drivelettertext = driveletter1.c_str();
wcsncpy_s(driveletter, sizeof(driveletter), drivelettertext, sizeof(drivelettertext));
return 0;
}
// Function to check if file exists
int Grass7API::FileManagement::fileExistsA(LPCSTR file)
{
WIN32_FIND_DATAA FindFileData;
HANDLE handle = FindFirstFileA(file, &FindFileData);
int found = handle != INVALID_HANDLE_VALUE;
if (found)
{
FindClose(handle);
}
return found;
}
int Grass7API::FileManagement::fileExistsW(LPCWSTR file)
{
WIN32_FIND_DATAW FindFileData;
HANDLE handle = FindFirstFileW(file, &FindFileData);
int found = handle != INVALID_HANDLE_VALUE;
if (found)
{
FindClose(handle);
}
return found;
}
// Function to check if directory exists
int Grass7API::FileManagement::dirExists(LPCWSTR pathname)
{
struct _stat64i32 info;
if (_wstat(pathname, &info) != 0)
return 0;
else if (info.st_mode & S_IFDIR)
return 1;
else
return 0;
}
// Function to delete directory recursively
LONG Grass7API::FileManagement::DeleteDirectory(LPCWSTR sPath)
{
WCHAR szDir[MAX_PATH + 1]; // +1 for the double null terminate
SHFILEOPSTRUCTW fos = { 0 };
StringCchCopyW(szDir, MAX_PATH, sPath);
int len = lstrlenW(szDir);
szDir[len + 1] = 0; // double null terminate for SHFileOperation
// delete the folder and everything inside
fos.wFunc = FO_DELETE;
fos.pFrom = szDir;
fos.fFlags = FOF_NO_UI;
return SHFileOperationW(&fos);
}
BOOL Grass7API::FileManagement::FindFile(LPCWSTR path, LPCWSTR fileName, LPWSTR fullPath)
{
WIN32_FIND_DATAW fd = {};
DWORD dwError;
WCHAR str[MAX_PATH] = {};
PathCombineW(str, path, L"*");
HANDLE hFind = FindFirstFileW(str, &fd);
if (hFind == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
if (dwError != ERROR_FILE_NOT_FOUND)
std::cout << "ERROR " << dwError;
return false;
}
do
{
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((lstrcmpW(fd.cFileName, L".") != 0) &&
(lstrcmpW(fd.cFileName, L"..") != 0))
{
PathCombineW(str, path, fd.cFileName);
if (Grass7API::FileManagement::FindFile(str, fileName, fullPath))
{
CloseHandle(hFind);
return true;
}
}
}
else
{
if ((lstrcmpiW(fd.cFileName, fileName) == 0) ||
(lstrcmpiW(fd.cAlternateFileName, fileName) == 0))
{
if (fullPath)
PathCombineW(fullPath, path, fd.cFileName);
CloseHandle(hFind);
return true;
}
}
} while (FindNextFileW(hFind, &fd));
dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES)
std::cout << "ERROR " << dwError;
CloseHandle(hFind);
return false;
}