Skip to content

Instantly share code, notes, and snippets.

@hyrious
Created October 25, 2025 15:22
Show Gist options
  • Select an option

  • Save hyrious/6246e5bd66e1361937582f58613bd54c to your computer and use it in GitHub Desktop.

Select an option

Save hyrious/6246e5bd66e1361937582f58613bd54c to your computer and use it in GitHub Desktop.
LLM wrote this.
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#define BUFFER_SIZE 4096
int main(int argc, char *argv[]) {
HANDLE hDir;
DWORD bytesReturned;
char buffer[BUFFER_SIZE];
FILE_NOTIFY_INFORMATION *pNotify;
WCHAR fileName[MAX_PATH];
WCHAR fullPath[MAX_PATH];
WCHAR dirPathW[MAX_PATH];
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
SYSTEMTIME st;
const char *dirPath = (argc > 1) ? argv[1] : ".";
hDir = CreateFileA(dirPath, FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hDir == INVALID_HANDLE_VALUE) {
printf("Failed to open directory: %s (error %lu)\n", dirPath, GetLastError());
return 1;
}
printf("Watching directory: %s\n", dirPath);
printf("Press Ctrl+C to exit...\n\n");
// Convert directory path to wide string for later use
MultiByteToWideChar(CP_ACP, 0, dirPath, -1, dirPathW, MAX_PATH);
while (1) {
if (!ReadDirectoryChangesW(hDir, buffer, BUFFER_SIZE, TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_CREATION,
&bytesReturned, NULL, NULL)) {
printf("ReadDirectoryChangesW failed: %lu\n", GetLastError());
break;
}
pNotify = (FILE_NOTIFY_INFORMATION *)buffer;
while (1) {
wcsncpy(fileName, pNotify->FileName, pNotify->FileNameLength / sizeof(WCHAR));
fileName[pNotify->FileNameLength / sizeof(WCHAR)] = L'\0';
printf("Action: ");
switch (pNotify->Action) {
case FILE_ACTION_ADDED:
printf("ADDED");
break;
case FILE_ACTION_REMOVED:
printf("REMOVED");
break;
case FILE_ACTION_MODIFIED:
printf("MODIFIED");
break;
case FILE_ACTION_RENAMED_OLD_NAME:
printf("RENAMED_OLD_NAME");
break;
case FILE_ACTION_RENAMED_NEW_NAME:
printf("RENAMED_NEW_NAME");
break;
default:
printf("UNKNOWN");
}
printf(" - File: %ls", fileName);
// Construct full path and get file info
swprintf(fullPath, MAX_PATH, L"%ls\\%ls", dirPathW, fileName);
if (GetFileAttributesExW(fullPath, GetFileExInfoStandard, &fileInfo)) {
// Convert file time to system time
FILETIME localTime;
FileTimeToLocalFileTime(&fileInfo.ftLastWriteTime, &localTime);
FileTimeToSystemTime(&localTime, &st);
// Calculate file size
ULONGLONG fileSize = ((ULONGLONG)fileInfo.nFileSizeHigh << 32) | fileInfo.nFileSizeLow;
printf(" - Size: %llu bytes - Modified: %04d-%02d-%02d %02d:%02d:%02d.%03d\n",
fileSize, st.wYear, st.wMonth, st.wDay,
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
} else {
printf(" - (Unable to get file info)\n");
}
if (pNotify->NextEntryOffset == 0)
break;
pNotify = (FILE_NOTIFY_INFORMATION *)((char *)pNotify + pNotify->NextEntryOffset);
}
}
CloseHandle(hDir);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment