VB icon

Inbox messages extractor v1.0

Submitted on: 10/3/2016 10:47:38 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 131
     Extract messages from inbox files such as Thunderbird or usenet, Hope you find this code usfull please vote.
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
// Name: Inbox messages extractor v1.0
// Description:Extract messages from inbox files such as Thunderbird or usenet, Hope you find this code usfull please vote.
// By: Ben

// Mail message extractor
// Extract messages from inbox files such as Thunderbird or usenet
// by DreamVB 20:34 03/10/2016
// Version 1.0
#include <iostream>
#include <Windows.h>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
using std::cout;
using std::endl;
bool DirExists(string path){
	wstring ws(path.begin(), path.end());
	//Check if a file was found.
	if (GetFileAttributes(ws.c_str()) != FILE_ATTRIBUTE_DIRECTORY){
		return false;
	return true;
string Dec2Str(int value){
	ostringstream os;
	os << value;
	return os.str();
string UnixToDos(string src){
	int i = 0;
	string buffer = "";
	while (i < src.length()){
		if (src[i] == '\n'){
			//Check for \r
			if (src[i] != '\r'){
				buffer += "\r\n";
				buffer += "\n";
			buffer += src[i];
	return buffer;
int main(int argc, char *argv[]){
	fstream fs;
	fstream fout;
	string StrLine = "";
	string PrevStr = "";
	string InBoxFile = "";
	string sOut = "";
	string sOutFile = "";
	string sMessage = "";
	int x = 0;
	if (argc < 2){
		cout << "Usage: " << argv[0] << " <InboxFile> <OutputFolder>" << endl;
	//Get inbox filename.
	InBoxFile = argv[1];
	sOut = argv[2];
	//Check if output folder is here.
	if (!DirExists(sOut)){
		cout << "Directory Not Found:" << endl;
		cout << sOut.c_str() << endl;
	fs.open(InBoxFile.c_str(), ios::in | ios::binary);
	//Check if file is open.
	if (!fs.good()){
		cout << "Cannot Read Input File:" << endl;
		cout << InBoxFile.c_str() << endl;
	//Read the inbox file and extract the messages.
	while (getline(fs,StrLine)){
		//Check for line length
		if (StrLine.length() > 0){
			//Check for the string From this is were the message begins
			if (StrLine.substr(0, 5) == "From "){
				cout << "Extracting: " << StrLine.c_str() << endl;
				//If StrLine not equal Prev String then output message data
				if (StrLine != PrevStr){
					//Check the length of the message.
					if (sMessage.length() > 0){
						//Set output file
						sOutFile = sOut + "message_" + Dec2Str(x) + ".txt";
						//Open the file for output
						fout.open(sOutFile, ios::out | ios::binary);
						//Check that the file was open if not exit.
						if (!fout.good()){
							cout << "Cannot Create Output File: " << endl;
							cout << sOutFile.c_str() << endl;
						sMessage = UnixToDos(sMessage);
						//Write data to the file.
						fout.write(sMessage.c_str(), sMessage.length());
						//Close file
						//Clear message.
				//INC counter
			//Build the message.
			sMessage += StrLine + "\n";
			//Store prev string
			PrevStr = StrLine;
	//Output last one
	sMessage = UnixToDos(sMessage);
	sOutFile = sOut + "message_" + Dec2Str(x) + ".txt";
	fout.open(sOutFile, ios::out | ios::binary);
	fout.write(sMessage.c_str(), sMessage.length());
	//Close file
	//Display messages found.
	cout << "Finished " << x << " messages extracted." << endl;
	//Clear up.
	return 0;

Other 46 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.