// Filinfo version 2
// By Ben a.k.a DreamVB
// Comments/questions welcome at dreamvb@outlook.com

#include <iostream>
#include <Windows.h>

using namespace std;
using std::cout;
using std::endl;
//Keep all the file information.
struct TFileInfo
{
	string Drive;
	string FullPath;
	string Filename;
	string FullName;
	string Extension;
	DWORD FileSize;
	bool Exsits;
	DWORD Attributes;
};

void GetFileInfo(string Filename, TFileInfo &fInfo){
	//First split the path and file name
	WIN32_FIND_DATAA wfd;
	HANDLE f;

	int i = 0;
	int pos = 0;

	string Temp = Filename;
	string vDrive = "";
	string vFile = "";
	string vPath = "";
	string vExt = "";

	fInfo.FileSize = 0;
	fInfo.Exsits = false;
	fInfo.Attributes = 0;

	//Get drive
	pos = Temp.find(':');
	if (pos != string::npos){
		vDrive = Temp.substr(0, pos);
		//Erase drive from path
		Temp.erase(0, pos+2);
	}

	//Get filename
	pos = Temp.find_last_of('\\');
	if (pos != string::npos){
		vFile = Temp.substr(pos + 1);
		//Erase filename from Temp
		vPath = Temp.substr(0, pos);
		//Get file ext
		pos = vFile.find_last_of('.');
		if (pos != string::npos){
			vExt = vFile.substr(pos);
		}
	}

	//Fill file info type
	fInfo.Drive = vDrive;
	fInfo.FullPath = vPath;
	fInfo.Filename = vFile;
	fInfo.FullName = Filename;
	fInfo.Extension = vExt;
	
	//Get some file info
	f = FindFirstFileA(Filename.c_str(), &wfd);

	if (f != INVALID_HANDLE_VALUE){
		fInfo.Exsits = true;
		fInfo.FileSize = wfd.nFileSizeLow;
		fInfo.Attributes = wfd.dwFileAttributes;
	}

	FindClose(f);
}

int main(int argc, char *argv[]){
	TFileInfo fi;
	GetFileInfo("C:\\Work\\customers.txt",fi);
	system("title FileInfo");
	cout << "Full Name  : " << fi.FullName.c_str() << endl;
	cout << "Drive      : " << fi.Drive.c_str() << endl;
	cout << "Path       : " << fi.FullPath.c_str() << endl;
	cout << "File       : " << fi.Filename.c_str() << endl;
	cout << "Extension  : " << fi.Extension.c_str() << endl;
	cout << "Exsits     : " << fi.Exsits << endl;
	cout << "FileSize   : " << fi.FileSize << endl;
	cout << "attributes : " <<fi.Attributes << endl;

	system("pause");
	return 0;
}