The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 GetMultiOpenBox choose Filetype

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
Thomas83 Posted - 06/03/2013 : 08:02:10 AM
Origin 8G pro SR 6.
Hi.
I have a folder with (at least) two different filetypes (e.g. "*.dat" and "*.txt"). I want to import either the dat.files or the txt.files each with a different partial import, because the file structure is different. For example the dat.files contain 4 columns, the txt.files have only 2 columns. In my code I use the GetMultiOpenBox method where I can switch between both files. But how can I tell Origin, if file is of type "dat" than do "Partial Import I" if it is of type "txt" do "Partial Import II"?


void Part_Import_Box()
{
int iNumSelFiles;
string strPath;
StringArray saFileTypes;
StringArray saFilePaths;
string strFile = GetFileName(saFileTypes[0]);
saFileTypes.SetSize( 3 );
saFileTypes[0]="[FileTypeI (*.dat)] *.dat";
saFileTypes[1]="[FileTypeII (*.txt)] *.txt";
saFileTypes[2]="[all files (*.*)] *.*";
iNumSelFiles = GetMultiOpenBox( saFilePaths, saFileTypes,
//If file is of type dat than ...
{
Worksheet wks;
wks.Create();
ASCIMP ai;
AscImpReadFileStruct(strPath + saFilePaths[0], &ai);
ai.iMode = ASCIMP_MODE_APPEND_COLS
ai.iHeaderLines = 0;
ai.iSubHeaderLines = 0;
ai.iPartial = 1;
ai.iPartialC1 =5;
ai.iPartialC2 =5;
//etc.; Partial import is not the issue for

//If file is of type txt than => different partial import

You donīt need to rewrite the entire code. I just need the right syntax. If (??? File is "dat") => AscImpReadFileStruct(strPath + saFilePaths[0], &ai) ...

Thanks in advance
9   L A T E S T    R E P L I E S    (Newest First)
Thomas83 Posted - 06/07/2013 : 04:04:11 AM
Thanks a lot.
It works.
rlewis Posted - 06/06/2013 : 04:57:44 AM
The problems described above stem from the fact that The version of the function FileNameEndOdd that was originally originally ousted becomes unreliable when the string of numeric characters is longer than nine characters. I have therefore recoded the function (see below) to fix this. In fact the fix was quite simple since one only had to check whether the last number of the string was odd or even.


int FileNameEndOdd(string strPathToFile)
{
	/*
		Returns 0	...   File Name ends with Even Number
		Returns 1   ...   File Name ends with Odd Number
		Returns -1  ...   File Name does NOT end with Numeric characters
	*/
	string strFileName=GetFileName(strPathToFile,true);
	strFileName.TrimLeft();
	strFileName.TrimRight();
	if(strFileName.GetLength()==0)
	{
		return (-1);
	}
	char chNum=strFileName.GetAt(strFileName.GetLength()-1);
	if(chNum<48 || chNum>57)
	{
		return (-1);
	}
	string strFileNumber=chNum;
	int iFileNumber=atoi(strFileNumber);
	return(mod(iFileNumber,2));
}
Thomas83 Posted - 06/06/2013 : 04:35:57 AM
Some additional comments regarding my last reply:

question 1):

Case A:
The filenames are of type file001, file002, ...
=> everything is fine; the import works

Case B:
The filename itself is a long numeric character, e.g. 1234567001, 1234567002, ...
=> only files with even numbers are imported. Files with ODD numbers are either not imported or the sheet is deleted afterwards.
The message "File Name does not end with numeric Characters" occurs for files with ODD numbers (e.g. 1234567001, 1234567003,...), independently if the file is of type txt or dat.
This is strange.

Case C:
The filename is of tpye 123ABC456001, 123ABC456002, ...
=> all file are imported to one single sheet, not separated by ODD or EVEN numbers.
This means: 1st Col wavelength, 2nd Col 123ABC456001, 3rd Col 123ABC456002, 4th Col 123ABC003, ...
The error message does not occur.

This is not really an issue. If I know about that I will consider how I name the files in the future. Could you please comment on that?
Thank you for your trouble.
Thomas83 Posted - 06/06/2013 : 03:14:35 AM
Hi rlewis.
Absolutly amazing. It works. Thanks a lot.
Only two questions:
1) If the filename is something like "file001.txt" everything is fine, but if the filename itself is a long numeric character letīs say "1234567001.txt, 1234567002.txt" the message "File Name does not end with numeric Characters" (as written in the code). Is there a limit to the filename (e.g. limited number of numeric characters or so)?
2)I just started programming in Origin C. So far my motto was "try and error". I copied and changed some examples from the origin help or from the forum. Is there a "better" way to learn programming or a general help? I want to be able to write such codes like above on my own, but sometimes I get lost and donīt know how to go on.

Again, thank you very much for your help.
And maybe I here from you on my next issue
rlewis Posted - 06/05/2013 : 4:14:53 PM
There were a few errors in my last post ... I've corrected these errors added a few checks to make it more robust and annotated the code so as to make it easier to follow and edit to your requirements ...
It is designed to process a selection of txt files, dat files or both. I've tested it with some dat and txt files that I put together and it seems to work ... Hopefully this will work for you ..



void Part_Import_Box()
{
	int iNumSelFiles;
	StringArray saFileTypes;
	StringArray saFilePaths;
	saFileTypes.SetSize(3);
	saFileTypes[0]="[FileTypeI (*.dat)] *.dat";
	saFileTypes[1]="[FileTypeII (*.txt)] *.txt";
	saFileTypes[2]="[all files (*.*)] *.*";
	iNumSelFiles = GetMultiOpenBox(saFilePaths, saFileTypes);
	if (iNumSelFiles>0)
	{
		ASCIMP ai;
		WorksheetPage wpTXT; // Workbook to receive data from *.txt files
		WorksheetPage wpDAT; // Workbook to receive data from *.dat files
		WorksheetPage *wP;   // Pointer to a workbook object
		Worksheet wks;
		if(wpTXT.Create("Origin",CREATE_HIDDEN)==false || wpDAT.Create("Origin",CREATE_HIDDEN)==false)
		{
			// *.txt and *.dat files are imported into separate workbooks ...
			out_str("Workbook Creation Error ...");
			return;
		}
		if(wpTXT.AddLayer()<0 || wpDAT.AddLayer()<0)
		{
			out_str("Workbook Layer Creation Error ...");
			return;
		}
		int OddTxtFiles=0;
		int EvenTxtFiles=0;
		int OddDatFiles=0;
		int EvenDatFiles=0;
		for (int i=0;i<iNumSelFiles;i++)
		{
			string strFilePath=saFilePaths[i];
			
			//  Determine whether the file extension is "dat" or "txt"  and set the value of the boolean IsTXTfile accordingly
			//  file extension == "txt"  ... IsTXTfile=true
			//  file extension == "dat"  ... IsTXTfile=false
			bool IsTXTfile;
			int NumTokens=strFilePath.GetNumTokens('.');
			if(NumTokens<=0)
			{
				// Only *.txt and/or *.dat files are imported ...
				// Files that do not have an extension are ignored ...
				continue;
			}
			string strFileExt=strFilePath.GetToken(NumTokens-1, '.');
			strFileExt.TrimLeft();
			strFileExt.TrimRight();
			if(strFileExt.CompareNoCase("txt")!=0 && strFileExt.CompareNoCase("dat")!=0)
			{
				// Only *.txt and/or *.dat files are imported ...
				// Files with other File extensions are ignored ...
				out_str("Unrecognized File Type : "+GetFileName(strFilePath));
				continue;
			}
			IsTXTfile=(strFileExt.CompareNoCase("txt")==0);
			
			// Determine whether the filename ends with an ODD or EVEN number
			int OddFileName=FileNameEndOdd(strFilePath);
			if(OddFileName==-1)
			{
				// Only files with filenames ending with numeric cahracters are imported ...
				// Files with names that do not end with numeric characters are ignored ...
				out_str("File Name does not end with numeric Characters : "+GetFileName(strFilePath));
				continue;
			}
			
			//  Set the Workbook object pointer to the appropriate Workbook (TXT files to wpTXT and DAT files to wpDAT)
			if(IsTXTfile==true)
			{
				// *.txt files imported into workbook object wpTXT
				wP=&wpTXT;
			}
			else
			{
				// *.dat files imported into workbook object wpTXT
				wP=&wpDAT;
			}
			
			// Assign the  worksheet object to the appropriate layer (ODD-numbered to Workbook layer 0 and EVEN-numbered to Workbook layer 1)
			if(OddFileName==1)
			{
				// Odd Numbered Files imported into Workbook Layer 0
				wks=wP->Layers(0);
			}
			else
			{
				// Even Numbered Files imported into Workbook Layer 1
				wks=wP->Layers(1);
			}
			
			// Check that the Workbook and Worksheet objects are valid ... exit if any of the objects are not valid ...
			if(wP->IsValid()==false)
			{
				out_str("Workbook Adressing Error");
				return;				
			}
			if(wks.IsValid()==false)
			{
				out_str("Workbook Layer Adressing Error");
				return;
			}
			
			// Import the file as per the required import parameters ...
			if(IsTXTfile==true)
			{
				// Importing .txt files
				if(OddFileName==1)
				{
					// Processing ODD numbered txt file
					if(OddTxtFiles==0)
					{
					//partial import of "wavelength" column of the first file with ODD number
						AscImpReadFileStruct(strFilePath, &ai);
						ai.iMode = ASCIMP_MODE_APPEND_COLS;
						ai.iRenameCols = 0; 
						ai.iPartial = 1; 
						ai.iPartialC1 = 0; 
						ai.iPartialC2 = 0; 
						wks.ImportASCII(strFilePath, ai);
						wks.Columns(0).SetLongName("wavelength");
					}
					//partial import of "intensity" column of all file with ODD numbers
	    			AscImpReadFileStruct(strFilePath, &ai);
        			ai.iMode = ASCIMP_MODE_APPEND_COLS;
					ai.iRenameCols = 0; 
        			ai.iPartial = 1; 
        			ai.iPartialC1 = 1; 
        			ai.iPartialC2 = 1; 
					wks.ImportASCII(strFilePath, ai);
					OddTxtFiles ++;
				}
				else
				{
				//Processing EVEN numbered txt file
					if(EvenTxtFiles==0)
					{
						//partial import of "wavelength" column of the first file
						AscImpReadFileStruct(strFilePath, &ai);
						ai.iMode = ASCIMP_MODE_APPEND_COLS; 
						ai.iRenameCols = 0; 
						ai.iPartial = 1; 
						ai.iPartialC1 = 0; 
						ai.iPartialC2 = 0; 
						wks.ImportASCII(strFilePath, ai);
						wks.Columns(0).SetLongName("wavelength");
					}
					//partial import of "intensity" column of all files with EVEN numbers
					AscImpReadFileStruct(strFilePath, &ai);
					ai.iMode = ASCIMP_MODE_APPEND_COLS;
					ai.iPartial = 1; 
					ai.iPartialC1 = 4; 
					ai.iPartialC2 = 4;
					wks.ImportASCII(strFilePath, ai);
					EvenTxtFiles ++;
				}
			}
			else
			{
				// Importing .dat files
				if(OddFileName==1)
				{
					//processing ODD numbered dat file
					if(OddDatFiles==0)
					{
					//partial import of "wavelength" column of the first file with ODD number
						AscImpReadFileStruct(strFilePath, &ai);
						ai.iMode = ASCIMP_MODE_APPEND_COLS;
						ai.iRenameCols = 0; 
						ai.iPartial = 1; 
						ai.iPartialC1 = 0; 
						ai.iPartialC2 = 0; 
						wks.ImportASCII(strFilePath, ai);
						wks.Columns(0).SetLongName("wavelength"); 
					}
					//partial import of "intensity" column of all file with ODD numbers
	    			AscImpReadFileStruct(strFilePath, &ai);
        			ai.iMode = ASCIMP_MODE_APPEND_COLS;
					ai.iRenameCols = 0; 
        			ai.iPartial = 1; 
        			ai.iPartialC1 = 1; 
        			ai.iPartialC2 = 1; 
					wks.ImportASCII(strFilePath, ai);
					OddDatFiles ++;
				}
				else
				{
				// Processing EVEN numbered dat file
					if(EvenDatFiles==0)
					{
						AscImpReadFileStruct(strFilePath, &ai);
						//partial import of "wavelength" column of the first file
						ai.iMode = ASCIMP_MODE_APPEND_COLS; 
						ai.iRenameCols = 0; 
						ai.iPartial = 1; 
						ai.iPartialC1 = 0; 
						ai.iPartialC2 = 0; 
						wks.ImportASCII(strFilePath, ai);
						wks.Columns(0).SetLongName("wavelength");
					}
					//partial import of "intensity" column of all files with  EVEN numbers
					AscImpReadFileStruct(strFilePath, &ai);
					ai.iMode = ASCIMP_MODE_APPEND_COLS;
					ai.iPartial = 1; 
					ai.iPartialC1 = 1; 
					ai.iPartialC2 = 1;
					wks.ImportASCII(strFilePath, ai);
					EvenDatFiles ++;
				}
			}
		}
		// Clean-up the mess if any ...
		if(OddTxtFiles==0 && EvenTxtFiles==0)
		{
			// Destroy the workbook object wpTXT if no txt files were imported
			if(wpTXT.IsValid()==true)
			{
				wpTXT.Destroy();
			}
		}
		else
		{
			if(OddTxtFiles==0)
			{
				// Destroy the workbook layer 0 if no odd-numbered txt files were imported
				Layer Layr=wpTXT.Layers(0);
				if(Layr.IsValid()==true)
				{
					Layr.Destroy();
				}
			}
			if(EvenTxtFiles==0)
			{
				// Destroy the workbook layer 1 if no even-numbered txt files were imported
				Layer Layr=wpTXT.Layers(1);
				if(Layr.IsValid()==true)
				{
					Layr.Destroy();
				}
			}
		}
		if(OddDatFiles==0 && EvenDatFiles==0)
		{
			// Destroy the workbook object wpDAT if no dat files were imported
			if(wpDAT.IsValid()==true)
			{
				wpDAT.Destroy();
			}
		}
		else
		{
			if(OddDatFiles==0)
			{
				// Destroy the workbook layer 0 if no odd-numbered dat files were imported
				Layer Layr=wpDAT.Layers(0);
				if(Layr.IsValid()==true)
				{
					Layr.Destroy();
				}
			}
			if(EvenDatFiles==0)
			{
				// Destroy the workbook layer 1 if no even-numbered dat files were imported
				Layer Layr=wpDAT.Layers(1);
				if(Layr.IsValid()==true)
				{
					Layr.Destroy();
				}
			}
		}
	}
}

int FileNameEndOdd(string strPathToFile)
{
	/*
		Returns 0	...   File Name ends with Even Number
		Returns 1   ...   File Name ends with Odd Number
		Returns -1  ...   File Name does NOT end with Numeric characters
	*/
	string strFileName=GetFileName(strPathToFile,true);
	strFileName.TrimLeft();
	strFileName.TrimRight();
	int strtPosn=-1;
	for(int i=strFileName.GetLength()-1; i>=0;i--)
	{
		char chNum=strFileName.GetAt(i);
		if(chNum>=48 && chNum<=57)
		{
			strtPosn=i;
			continue;
		}
	}
	if (strtPosn==-1)
	{
		return (-1);
	}
	string strFileNumber=strFileName.Mid(strtPosn,(strFileName.GetLength()-strtPosn));
	int iFileNumber=atoi(strFileNumber);
	return(mod(iFileNumber,2));
}

rlewis Posted - 06/05/2013 : 04:48:20 AM
How about this ...


void Part_Import_Box()
{
	int iNumSelFiles;
	StringArray saFileTypes;
	StringArray saFilePaths;
	saFileTypes.SetSize(3);
	saFileTypes[0]="[FileTypeI (*.dat)] *.dat";
	saFileTypes[1]="[FileTypeII (*.txt)] *.txt";
	saFileTypes[2]="[all files (*.*)] *.*";
	iNumSelFiles = GetMultiOpenBox(saFilePaths, saFileTypes);
	if (iNumSelFiles>0)
	{
		ASCIMP ai;
		WorksheetPage wpTXT; // Workbook to receive data from *.txt files
		WorksheetPage wpDAT; // Workbook to receive data from *.dat files
		WorksheetPage *wP;   // pointer to a workbook object
		Worksheet wks;
		if(wpTXT.Create("Origin",CREATE_VISIBLE )==false || wpDAT.Create("Origin",CREATE_VISIBLE )==false)
		{
			// *.txt and *.dat files are imported into separate workbooks ...
			out_str("Workbook creation Error ...");
			return;
		}
		wpTXT.AddLayer();
		wpDAT.AddLayer();
		int OddTxtFiles=0;
		int EvenTxtFiles=0;
		int OddDatFiles=0;
		int EvenDatFiles=0;
		for (int i=0;i<iNumSelFiles;i++)
		{
			string strFilePath=saFilePaths[i];
			
			int NumTokens=strFilePath.GetNumTokens('.');
			if(NumTokens<=0)
			{
				continue;
			}
			string strFileExt=strFilePath.GetToken(NumTokens-1, '.');
			if(strFileExt.CompareNoCase("txt")!=0 && strFileExt.CompareNoCase("dat")!=0)
			{
				// Only *.txt and/or *.dat files are imported ...
				// Files with Other File extensions are ignored ...
				out_str("Unrecognized File Type : "+GetFileName(strFilePath));
				continue;
			}
			int OddFileName=FileNameEndOdd(strFilePath);
			if(OddFileName==-1)
			{
				// Ignore Files with names that do not end with numeric characters ...
				out_str("File Name does not end with numeric Characters : "+GetFileName(strFilePath));
				continue;
			}
			if(strFileExt.CompareNoCase("txt")==0)
			{
				wP=&wpTXT;
			}
			else
			{
				wP=&wpDAT;
			}
			if(OddFileName==1)
			{
				// Odd Numbered Files imported into Workbook Layer 0
				wks=wP->Layers(0);
			}
			else
			{
				// Even Numbered Files imported into Workbook Layer 1
				wks=wP->Layers(1);
			}
			if(wks.IsValid()==false)
			{
				out_str("Workbook Layer Adressing Error");
				return;
			}
			AscImpReadFileStruct(strFilePath, &ai);
			if(strFileExt.CompareNoCase("txt")==0)
			{
				// Importing .txt files
				if(OddFileName==1)
				{
					out_str("Processing odd-numbered .txt file :"+GetFileName(strFilePath));
					// Set up code for importing and proessing odd numbered .txt files
					//Importing txt file with ODD numbers
					
					if(OddTxtFiles==0)
					{
					//partial import of "wavelength" column of the first file with ODD number
						AscImpReadFileStruct(strFilePath, &ai);
						ai.iMode = ASCIMP_MODE_APPEND_COLS;
						ai.iRenameCols = 0; 
						ai.iPartial = 1; 
						ai.iPartialC1 = 0; 
						ai.iPartialC2 = 0; 
						wks.ImportASCII(strFilePath, ai);
						wks.Columns(0).SetLongName("wavelength");
					}
					//partial import of "intensity" column of all file with ODD numbers
	    			AscImpReadFileStruct(strFilePath, &ai);
        			ai.iMode = ASCIMP_MODE_APPEND_COLS;
					ai.iRenameCols = 0; 
        			ai.iPartial = 1; 
        			ai.iPartialC1 = 1; 
        			ai.iPartialC2 = 1; 
					wks.ImportASCII(strFilePath, ai);
					OddTxtFiles ++;
				}
				else
				{
					out_str("Processing even-numbered .txt file :"+GetFileName(strFilePath));
				// Set up code for importing and proessing even numbered numbered .txt files
				//Importing txt file with EVEN numbers
					AscImpReadFileStruct(strFilePath, &ai);
					if(EvenTxtFiles==0)
					{
						//partial import of "wavelength" column of the first file
						ai.iMode = ASCIMP_MODE_APPEND_COLS; 
						ai.iRenameCols = 0; 
						ai.iPartial = 1; 
						ai.iPartialC1 = 0; 
						ai.iPartialC2 = 0; 
						wks.ImportASCII(strFilePath, ai);
						wks.Columns(0).SetLongName("wavelength");
					}
					//partial import of "intensity" column of all files with EVEN numbers
						AscImpReadFileStruct(strFilePath, &ai);
						ai.iMode = ASCIMP_MODE_APPEND_COLS;
						ai.iPartial = 1; 
						ai.iPartialC1 = 4; 
						ai.iPartialC2 = 4;
						wks.ImportASCII(strFilePath, ai);
						EvenTxtFiles ++;
				}
			}
			else
			{
				// Importing .dat files
				if(OddFileName==1)
				{
					out_str("Processing odd-numbered .dat file :"+GetFileName(strFilePath));
					// Set up code for importing and proessing odd numbered .txt files
					//Importing txt file with ODD numbers
					
					if(OddTxtFiles==0)
					{
					//partial import of "wavelength" column of the first file with ODD number
						AscImpReadFileStruct(strFilePath, &ai);
						ai.iMode = ASCIMP_MODE_APPEND_COLS;
						ai.iRenameCols = 0; 
						ai.iPartial = 1; 
						ai.iPartialC1 = 0; 
						ai.iPartialC2 = 0; 
						wks.ImportASCII(strFilePath, ai);
						wks.Columns(0).SetLongName("wavelength"); 
					}
					//partial import of "intensity" column of all file with ODD numbers
	    			AscImpReadFileStruct(strFilePath, &ai);
        			ai.iMode = ASCIMP_MODE_APPEND_COLS;
					ai.iRenameCols = 0; 
        			ai.iPartial = 1; 
        			ai.iPartialC1 = 1; 
        			ai.iPartialC2 = 1; 
					wks.ImportASCII(strFilePath, ai);
					OddTxtFiles ++;
				}
				else
				{
					out_str("Processing even-numbered .dat file :"+GetFileName(strFilePath));
				// Set up code for importing and proessing even numbered numbered .txt files
				//Importing txt file with EVEN numbers
					AscImpReadFileStruct(strFilePath, &ai);
					if(EvenTxtFiles==0)
					{
						//partial import of "wavelength" column of the first file
						ai.iMode = ASCIMP_MODE_APPEND_COLS; 
						ai.iRenameCols = 0; 
						ai.iPartial = 1; 
						ai.iPartialC1 = 0; 
						ai.iPartialC2 = 0; 
						wks.ImportASCII(strFilePath, ai);
						wks.Columns(0).SetLongName("wavelength");
					}
					//partial import of "intensity" column of all files with  EVEN numbers
						AscImpReadFileStruct(strFilePath, &ai);
						ai.iMode = ASCIMP_MODE_APPEND_COLS;
						ai.iPartial = 1; 
						ai.iPartialC1 = 1; 
						ai.iPartialC2 = 1;
						wks.ImportASCII(strFilePath, ai);
						EvenTxtFiles ++;
				}
			}
		}
		// Cleaning up the mess if any ...
		if(OddTxtFiles==0 && EvenTxtFiles==0)
		{
			if(wpTXT.IsValid()==true)
			{
				wpTXT.Destroy();
			}
		}
		else
		{
			if(OddTxtFiles==0)
			{
				Layer Layr=wpTXT.Layers(0);
				if(Layr.IsValid()==true)
				{
					Layr.Destroy();
				}
			}
			if(EvenTxtFiles==0)
			{
				Layer Layr=wpTXT.Layers(1);
				if(Layr.IsValid()==true)
				{
					Layr.Destroy();
				}
			}
		}
		if(OddDatFiles==0 && EvenDatFiles==0)
		{
			if(wpDAT.IsValid()==true)
			{
				wpDAT.Destroy();
			}
		}
		else
		{
			if(OddDatFiles==0)
			{
				Layer Layr=wpDAT.Layers(0);
				if(Layr.IsValid()==true)
				{
					Layr.Destroy();
				}
			}
			if(EvenDatFiles==0)
			{
				Layer Layr=wpDAT.Layers(1);
				if(Layr.IsValid()==true)
				{
					Layr.Destroy();
				}
			}
		}
	}
}

int FileNameEndOdd(string strPathToFile)
{
	/*
		Returns 0	...   File Name ends with Even Number
		Returns 1   ...   File Name ends with Odd Number
		Returns -1  ...   File Name does NOT end with Numeric characters
	*/
	string strFileName=GetFileName(strPathToFile,true);
	strFileName.TrimLeft();
	strFileName.TrimRight();
	int strtPosn=-1;
	for(int i=strFileName.GetLength()-1; i>=0;i--)
	{
		char chNum=strFileName.GetAt(i);
		if(chNum>=48 && chNum<=57)
		{
			strtPosn=i;
			continue;
		}
	}
	if (strtPosn==-1)
	{
		return (-1);
	}
	string strFileNumber=strFileName.Mid(strtPosn,(strFileName.GetLength()-strtPosn));
	int iFileNumber=atoi(strFileNumber);
	return(mod(iFileNumber,2));
}




Thomas83 Posted - 06/04/2013 : 07:22:17 AM
Ok one problem solved, new one occurs.
I have to delete line #7 "string strFile = GetFileName(saFileTypes[0]);"
The runtime error does not occur anymore, but if I run the script I can choose the the files but nothing is imported. I only get an new empty workbook

Regards
Thomas83 Posted - 06/04/2013 : 06:11:03 AM

Hi rlewis.
Thanks for your replay, but unfortunately it doesnīt work. Compiling runs without any problems, but when I start the function an error message appears: "runtime error in Origin C function, unknown error". Maybe there is a fault in my code for partial import. The code is shown below. I try to explain what Iīm trying to do. I have 2 file types ("txt" and "dat"). The files are named with encountered numbers, i.e. file001.txt, file002.txt, ... or file001.dat, file002.dat, respectively.
I want to import (partial) either the txt OR the dat file, each separated for filenames with odd and even numbers.
Idea:
Import file001.txt, file003.txt,... to first worksheet "A",
file002.txt, file004.txt,... to second worksheet "B".
Accordingly for dat files.
The structure of the txt.files is as follows (2 columns):
wavelength / intensity
Because the wavelength column is the same for each file with odd numbers OR even numbers I just want to import the first column of files with odd numbers one time in worksheet "A"(set as X column in Origin). From all other txt files with odd numbers I want to import only the second column (intensity). The same for files with even numbers but in a second worksheet "B".
The dat file have a similar, but different structure (5 columns):
wavelength /C2/C3/C4/intensity.
Again, Iīm interessted only in the intensity.
I know this might be really confusing, but maybe you can help me. Maybe itīs a simple problem. I took your code and added the set up part for partial import. The partial itself works, but if the file type changes (from txt to dat) I have to change the code manually.
void Part_Import_Box()
{
//your code
    int iNumSelFiles;
    string strPath;
    StringArray saFileTypes;
    StringArray saFilePaths;
    string strFile = GetFileName(saFileTypes[0]);
    saFileTypes.SetSize( 3 );
    saFileTypes[0]="[FileTypeI (*.txt)] *.txt";
    saFileTypes[1]="[FileTypeII (*.dat)] *.dat";
    saFileTypes[2]="[all files (*.*)] *.*";
        iNumSelFiles = GetMultiOpenBox( saFilePaths, saFileTypes);  
	if (iNumSelFiles >0)
	{
		Worksheet wks;
		ASCIMP ai;
		for (int i=0; i < iNumSelFiles; i++)
		{
			wks.Create();
		
string strFilePath = saFilePaths[i]; int NumTokens = strFilePath.GetNumTokens('.'); if (NumTokens<1) { continue; } string strFileExt=strFilePath.GetToken(NumTokens-1); if (strFileExt.CompareNoCase("txt")==0||strFileExt.CompareNoCase("datt")==0) { if (strFileExt.CompareNoCase("txt")==0) //My code { //Import txt file with EVEN numbers //partial import of "wavelength" column of the first file with EVEN number AscImpReadFileStruct(strPath + saFilePaths[0], &ai); ai.iMode = ASCIMP_MODE_APPEND_COLS; ai.iRenameCols = 0; ai.iPartial = 1; ai.iPartialC1 = 0; ai.iPartialC2 = 0; wks.ImportASCII(strPath + saFilePaths[0], ai); //partial import of "intensity" column of all file with EVEN numbers AscImpReadFileStruct(strPath + saFilePaths[1], &ai); ai.iMode = ASCIMP_MODE_APPEND_COLS; ai.iPartial = 1; ai.iPartialC1 = 4; ai.iPartialC2 = 4; for(int j = 0; j < (saFilePaths.GetSize())/2; j++ ) //EVEN numbers wks.ImportASCII(strPath + saFilePaths[2*j], ai); string strName = "A"; wks.SetName(strName); //Add page string strNew; int nRows = 1; int nCols = 1; WorksheetPage wp = Project.Pages(); int nn = wp.AddLayer(strNew); Worksheet wks2 = wp.Layers(nn); wks2.SetSize(nRows, nCols); //Import txt file with ODD numbers //partial import of "wavelength" column of the first file with ODD number AscImpReadFileStruct(strPath + saFilePaths[1], &ai); ai.iMode = ASCIMP_MODE_APPEND_COLS; ai.iRenameCols = 0; ai.iPartial = 1; ai.iPartialC1 = 0; ai.iPartialC2 = 0; wks2.ImportASCII(strPath + saFilePaths[1], ai); //partial import of "intensity" column of all file with ODD numbers AscImpReadFileStruct(strPath + saFilePaths[1], &ai); ai.iMode = ASCIMP_MODE_APPEND_COLS; ai.iRenameCols = 0; ai.iPartial = 1; ai.iPartialC1 = 1; ai.iPartialC2 = 1; //Files with ODD number for(int i = 0; i < (saFilePaths.GetSize())/2; i++ ) wks2.ImportASCII(strPath + saFilePaths[2*i+1], ai); string strName2 = "B"; wks2.SetName(strName2); wks.Columns(0).SetLongName("wavelength"); wks2.Columns(0).SetLongName("wavelength"); } else //Now accordingly for dat files; The only difference is in the code for partial import (ai.iPartialC1 and C2 = 4 instead of 1) { } } } } }


Iīd be very grateful if you could fix the problem. Thank you very much
rlewis Posted - 06/03/2013 : 9:33:42 PM
Try something like ...

void Part_Import_Box()
{
	int iNumSelFiles;
	string strPath;
	StringArray saFileTypes;
	StringArray saFilePaths;
	string strFile = GetFileName(saFileTypes[0]);
	saFileTypes.SetSize( 3 );
	saFileTypes[0]="[FileTypeI (*.dat)] *.dat";
	saFileTypes[1]="[FileTypeII (*.txt)] *.txt";
	saFileTypes[2]="[all files (*.*)] *.*";
	iNumSelFiles = GetMultiOpenBox(saFilePaths, saFileTypes);
	if (iNumSelFiles>0)
	{
		ASCIMP ai;
		Worksheet wks;
		for (int i=0;i<iNumSelFiles;i++)
		{	
			wks.Create();
			string strFilePath=saFilePaths[i];
			int NumTokens=strFilePath.GetNumTokens('.');
			if(NumTokens<1)
			{
				continue;
			}
			string strFileExt=strFilePath.GetToken(NumTokens-1);
			if(strFileExt.CompareNoCase("txt")==0 || strFileExt.CompareNoCase("dat")==0)
			{
				if(strFileExt.CompareNoCase("txt")==0)
				{
					// Importing .txt files
					AscImpReadFileStruct(strFilePath, &ai);
					// Set up code for importing .txt files goes here ...
					wks.ImportASCII(strFilePath, ai);
				}
				else
				{
					// Importing .dat files
					AscImpReadFileStruct(strFilePath, &ai);
					// Set up code for importing .dat files goes here ...					
					wks.ImportASCII(strFilePath, ai);
				}
			}
		}
		
	}
}

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000