从系统文件系统获取文件信息

9 篇文章 0 订阅
#include<iostream>
#include<string>
#include<ctime>
#include<vector>
#include<Windows.h>
#include<io.h>
#include<WinBase.h>
#include<stdio.h>

using namespace std;


string DeTime_t(const time_t t)
{
	string sp;
	if (t == 0)
	{
		sp = "1970/1/1 0:0:0";
	}
	else if (t != 0)
	{
		tm* m_t = localtime(&t);
		sp = to_string(m_t->tm_year + 1900) + "/" + to_string(m_t->tm_mon) + "/" + to_string(m_t->tm_mday) + " " +
			to_string(m_t->tm_hour) + ":" + to_string(m_t->tm_min) + ":" + to_string(m_t->tm_sec);
	}

	return sp;
}


void GetFileInfoFormFS(const char* filename)
{
	struct _finddata_t   fileInfo;
	memset(&fileInfo, 0, sizeof(fileInfo));
	intptr_t fhandle = _findfirst(filename, &fileInfo);
	if (-1L == fhandle)
	{
		cout << "找不到文件\n";
	}
	_findclose(fhandle);
	cout << "文件名:" << fileInfo.name << endl;
	cout << "文件大小:" << fileInfo.size << endl;
	cout << "创建时间:" << DeTime_t(fileInfo.time_create) << endl;
	cout << "修改时间:" << DeTime_t(fileInfo.time_write) << endl;
	cout << "最近访问时间:" << DeTime_t(fileInfo.time_access) << endl;
	cout << "文件属性:" << fileInfo.attrib << endl;
}

size_t FileSize(const char* filename)
{
	struct _finddata_t fileInfo = { 0 };
	intptr_t fhandle = _findfirst(filename, &fileInfo);
	if (-1L == fhandle)
	{
		cout << "找不到文件!\n";
	}
	return fileInfo.size;
}

void getFiles(string path,vector<string> files)
{
	intptr_t hfile = 0;//文件句柄
	struct _finddata_t fileinfo;
	string tmp;
	hfile = _findfirst(path.c_str(), &fileinfo);
	if (-1 != hfile)
	{
		do
		{
			if (fileinfo.attrib & _A_SUBDIR)//如果是目录,迭代之             
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(tmp.assign(path).append("\\").append(fileinfo.name), files);
			}
			else //如果不是,加入列表
			{
				files.push_back(tmp.assign(path).append("\\").append(fileinfo.name));
			}

		} while (_findnext(hfile, &fileinfo) == 0);
		_findclose(hfile);
	}
}
void gettime()
{
	time_t rawtime;
	struct tm* ptminfo;
	time(&rawtime);
	ptminfo = localtime(&rawtime);
	printf("current: %02d-%02d-%02d %02d:%02d:%02d\n",
		ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,
		ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
}

void GetComputerTime()
{
	LPSYSTEMTIME p_st = (LPSYSTEMTIME)malloc(sizeof(SYSTEMTIME));
	GetLocalTime(p_st);
	cout << "现在时间:" << p_st->wYear << "/" << p_st->wMonth << "/" << p_st->wDay << " "
		<< p_st->wHour << ":" << p_st->wMinute << ":" << p_st->wSecond << endl;
	//printf("%d:%d:%d\n", p_st->wHour, p_st->wMinute, p_st->wSecond);
	free(p_st);
}
int main()
{
	gettime();
	GetComputerTime();
	GetFileInfoFormFS("res/1.jpg");
	GetFileInfoFormFS("res/2.jpg");
	GetFileInfoFormFS("res/3.jpg");
	GetFileInfoFormFS("res/4.jpg");
	GetFileInfoFormFS("res/5.jpg");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Risehuxyc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值