Last active
October 12, 2024 23:22
-
-
Save davidglezz/7308631 to your computer and use it in GitHub Desktop.
Some file operations with the Windows API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<windows.h> | |
#include<iostream> | |
using namespace std; | |
// Example of file explorer | |
int main() | |
{ | |
WIN32_FIND_DATA FindFileData; | |
HANDLE hFind; | |
hFind = FindFirstFile("C:\\*", &FindFileData); | |
if (hFind == INVALID_HANDLE_VALUE) | |
{ | |
cout << "FindFirstFile failed: " << GetLastError() << endl; | |
return 1; | |
} | |
do cout << FindFileData.cFileName << endl; | |
while (FindNextFile(hFind, &FindFileData)); | |
FindClose(hFind); | |
return 0; | |
} | |
// Example Function: Change File Attributes | |
BOOL remove_ro_attr(TCHAR *file) | |
{ | |
/* | |
FILE_ATTRIBUTE_ARCHIVE | |
FILE_ATTRIBUTE_HIDDEN | |
FILE_ATTRIBUTE_NORMAL | |
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | |
FILE_ATTRIBUTE_OFFLINE | |
FILE_ATTRIBUTE_READONLY | |
FILE_ATTRIBUTE_SYSTEM | |
FILE_ATTRIBUTE_TEMPORARY | |
*/ | |
int attr = GetFileAttributes(file); | |
if (attr == INVALID_FILE_ATTRIBUTES) | |
return FALSE; | |
return SetFileAttributes(file, attr & (~FILE_ATTRIBUTE_READONLY)); | |
} | |
// return NULL if the file does not exist. | |
WIN32_FIND_DATA* file_exists(TCHAR *filename) | |
{ | |
static WIN32_FIND_DATA FindFileData; | |
HANDLE hFind = FindFirstFile(filename, &FindFileData); | |
if (hFind == INVALID_HANDLE_VALUE) | |
return NULL; | |
FindClose(hFind); | |
return &FindFileData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment