VB icon

Get FileInfo 2

Email
Submitted on: 9/16/2016 9:16:15 AM
By: Ben128  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 1651
 
     This is a new C++ version of my old FileInfo source. this one is a lot easyer to understand and also includes a few extra things anyway hope you like it. comments welcome.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Get FileInfo 2
// Description:This is a new C++ version of my old FileInfo source. this one is a lot easyer to understand and also includes a few extra things anyway hope you like it. comments welcome.
// By: Ben128
//**************************************

// 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;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.