Morozov_I_V_Labs/Project1/Project1/hello.c
2023-11-23 01:32:06 +03:00

221 lines
6.2 KiB
C

#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>
#include <locale.h>
TCHAR szDrive[] = _T(" A:");
int main()
{
setlocale(LC_ALL, "ru_RU.UTF8");
SetConsoleOutputCP(65001);
SetConsoleCP(65001);
//getLogicalDrives();
//getDiskFreeSpaceExA();
//get_file_list();
//getfiletime();
//createDir();
//removeDir();
//compareFileTime();
//setVolumeLabel();
//moveFile();
//deleteFile();
return 0;
}
int getLogicalDrives() {
DWORD uDriveMask = GetLogicalDrives();
// printf("The bitmask of the logical drives in hex: %0X\n", uDriveMask);
// printf("The bitmask of the logical drives in decimal: %d\n", uDriveMask);
if (uDriveMask == 0)
printf("GetLogicalDrives() failed with failure code: %d\n", GetLastError());
else
{
printf("This machine has the following logical drives:\n");
while (uDriveMask)
{
// Use the bitwise AND, 1â€"available, 0-not available
if (uDriveMask & 1)
printf("%S ", (const char*)szDrive);
// increment, check next drive
++szDrive[1];
// shift the bitmask binary right
uDriveMask >>= 1;
}
printf("\n ");
}
return 0;
}
int getDiskFreeSpaceExA() {
char n[10] = "";
printf("Enter disc name\n");
scanf(" %s", n);
LPCWSTR pszDrive = n;
BOOL test, fResult;
// 64 bits integer, low and high bytes
__int64 lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes;
DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;
// If the function succeeds, the return value is nonzero. If the function fails, the return value is 0 (zero).
test = GetDiskFreeSpaceExA(
pszDrive,
(PULARGE_INTEGER)&lpFreeBytesAvailable,
(PULARGE_INTEGER)&lpTotalNumberOfBytes,
(PULARGE_INTEGER)&lpTotalNumberOfFreeBytes
);
printf("\nUsing GetDiskFreeSpaceEx()...\n");
// Check the return value
printf("The return value: %d, error code: %d\n", test, GetLastError());
printf("Total number of free bytes available for user-caller: %ul\n", lpFreeBytesAvailable);
printf("Total number of bytes available for user: %ul\n", lpTotalNumberOfBytes);
printf("Total number of free bytes on disk: %ul\n", lpTotalNumberOfFreeBytes);
return 0;
}
int get_file_list()
{
char n[8] = "";
printf("Enter disc name\n");
scanf("%s:", n);
HANDLE hFind, hFile;
WIN32_FIND_DATA data;
FILETIME ftCreate;
SYSTEMTIME systime, sysLocal;
hFind = FindFirstFileA(n, &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
printf("%s\n ", data.cFileName);
//GetFileTime(&hFind, &ftCreate, NULL, NULL);
//FileTimeToSystemTime(&ftCreate, &systime);
//SystemTimeToTzSpecificLocalTime(NULL, &systime, &sysLocal);
//wprintf(L"CreationTime: %lu:%lu:%lu\n", sysLocal.wHour, sysLocal.wMinute, sysLocal.wSecond);
//printf("Created on: %02d/%02d/%d %02d:%02d\n", ssystimeystime.wDay, systime.wMonth, systime.wYear, systime.wHour, systime.wMinute);
} while (FindNextFileA(hFind, &data));
FindClose(hFind);
}
return 0;
}
int getfiletime()
{
HANDLE hFile1;
FILETIME ftCreate;
SYSTEMTIME stUTC, stLocal;
hFile1 = CreateFile(TEXT("text.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile1 == INVALID_HANDLE_VALUE)
{
printf("Could not open file, error %ul\n", GetLastError());
return -1;
}
if (!GetFileTime(hFile1, &ftCreate, NULL, NULL))
{
printf("Something wrong!\n");
return FALSE;
}
FileTimeToSystemTime(&ftCreate, &stUTC);
printf("UTC System Time format:\n");
printf("text.txt Created on: %02d/%02d/%d %02d:%02d\n",stUTC.wDay, stUTC.wMonth, stUTC.wYear, stUTC.wHour, stUTC.wMinute);
return 0;
}
int createDir()
{
BOOL createD = CreateDirectory(L"NewFolder", NULL);
if (createD)
{
printf("Directory Created\n");
}
return 0;
}
int removeDir()
{
BOOL removeD = RemoveDirectory(L"NewFolder", NULL);
if (removeD)
{
printf("Directory Deleted\n");
}
return 0;
}
int compareFileTime() {
FILETIME fileTime1, fileTime2;
// первый файл
HANDLE file1 = CreateFile(TEXT("text.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
// время изменения первого файла
GetFileTime(file1, NULL, NULL, &fileTime1);
// закрываем первый файл
CloseHandle(file1);
// Открываем второй файл
HANDLE file2 = CreateFile(TEXT("text2.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
// Получаем время изменения второго файла
GetFileTime(file2, NULL, NULL, &fileTime2);
// Закрываем второй файл
CloseHandle(file2);
// Сравниваем время изменения файлов
int comparisonResult = CompareFileTime(&fileTime1, &fileTime2);
if (comparisonResult == -1) {
wprintf(L"Файл 1 был изменен раньше файла 2\n");
}
else if (comparisonResult == 1) {
wprintf(L"Файл 2 был изменен раньше файла 1\n");
}
else {
wprintf(L"Время изменения файлов одинаково\n");
}
return 0;
}
int setVolumeLabel(){
LPSTR dirch = "S:\\";
LPSTR dirn = "Storage";
//LPSTR dirn = "Test";
BOOL bbb = SetVolumeLabelA(dirch, dirn);
//if(bbb){wprintf("%b",bbb);}
return 0;
}
int moveFile(){
if (MoveFile(L"Old.txt",L"New.txt")){
wprintf(L"Файл перемещён\n");
}
else {
wprintf(L"Ошибка\n");
}
return 0;
}
int deleteFile() {
if (DeleteFile(L"New.txt")) {
wprintf(L"Файл удалён\n");
}
else {
wprintf(L"Ошибка\n");
}
return 0;
}
/*
GetLogicalDrivesFindFistFile +
GetDiskFreeSpaceExA+
FindFirstFile +
FindNextFile +
GetFileTime+
CompareFileTime+
SetVolumeLabel+
CreatDirectory+
RemoveDirectory+
MoveFile +
DeleteFile +
*/