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
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Importing multiple ASCII files with Origin C
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

lauransl

6 Posts

Posted - 01/09/2013 :  10:19:27 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi,
I am trying to open different .dat files from the same folder with origin c by:

ASCIMP ai;
string strFile = GetOpenBox("*.* All Files"); // or "*.txt"
if(strFile.IsEmpty())
return;// user cancel

but it only lets me open one .dat file each time. How can I do to open all the .dat files from the folder?

Thanks a lot!

Laura

greg

USA
1378 Posts

Posted - 01/09/2013 :  12:51:21 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
GetOpenBox is for single files. Provided you have 8.1 or later, you could use something like this to handle multiple files:

void multiple_files()
{
#include <XFbase.h>

string strXFdlg = "dlgfile";
XFBase xf(strXFdlg);
if( !xf.IsValid() )
printf("Failed to attach to XFunction\n");
else
{
// Setup
string strNames, strGroup, strTitle, strInit;
int iMulti;
// Or use FDLOG Group names - like ASCII, Image
strGroup = "*.*";
strTitle = "My Import Dialog"; // Your own title
strInit = "D:\\Temp\\"; // Path to open in
iMulti = 1; // Use 0 for just one file

xf.SetArg("fname", strNames);
xf.SetArg("group", strGroup);
xf.SetArg("title", strTitle);
xf.SetArg("init", strInit);
xf.SetArg("multi", iMulti);
xf.Run(0);
for( int idx = 0 ; idx < strNames.GetNumTokens('\x0D') ; idx++ )
{
printf("%s\n", strNames.GetToken(idx,'\x0D'));
}
}
}
Go to Top of Page

lauransl

6 Posts

Posted - 01/10/2013 :  04:26:10 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks Greg, but what I really need is something that allows me to open multiple files each in a new worksheet. I don't know what command to use.
Thanks for your help!
Laura
Go to Top of Page

lauransl

6 Posts

Posted - 01/10/2013 :  09:40:36 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
What I have done is what is shown below, but it only allows me to open one file each time. What I would like to use is GetMultiOpenBox to open all my data files at once in different workbooks. But as GetMultiOpenBox doesn't behave as a string... I don't know how to use it.
Thanks!
Laura


void Datasheet_ImportASCII_Ex2()
{

ASCIMP ai;
string strFile = GetOpenBox("*.* All Files"); // or "*.txt"
if(strFile.IsEmpty())
return;// user cancel


if(AscImpReadFileStruct(strFile, &ai)==0)
{

ai.iPartial = 1; // 1 to specify partial import
ai.iPartialC1 = 0; // import from 2nd column (0 offset)
ai.iPartialC2 = 7; // set the last column, so here just import 4 columns
ai.iPartialR1 = 130; // set the import row from
ai.iPartialR2 = 2000; // set the import row to

ai.iMaxLabels = 0; // Not set any header line to Comments label

ai.nNumSep = NF_IS_AMERICAN;

ai.iMode = 0; // import as New Wks(0), New Columns (1), New Rows (2)

Worksheet wks;
wks.Create();
if(0 == wks.ImportASCII(strFile, ai))
{
// Set user parameter labels to specified names
Grid grid;
grid.Attach(wks);
vector<string> vsUserLabels = {"Expanded Discription", "Type Indication"};
grid.SetUserDefinedLabelNames(vsUserLabels);


}
}
else
out_str("failed to read ascii file");



}
Go to Top of Page

greg

USA
1378 Posts

Posted - 01/10/2013 :  12:46:07 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You can't use GetOpenBox for multiple files.

My code showed how to do these two things:
Prompt for multiple files
Handle each file separately

You just need to hook that code to yours by replacing my

printf("%s\n", strNames.GetToken(idx,'\x0D'));

with

string strFileName = strNames.GetToken(idx,'\x0D');
strFileName.TrimLeft();
Datasheet_ImportASCII_Ex2(strFileName);

and modifying your code to accept a string argument:

void Datasheet_ImportASCII_Ex2(string strFile)

and commenting out your GetOpenBox

//string strFile = GetOpenBox("*.* All Files"); // or "*.txt"
Go to Top of Page

Thomas83

24 Posts

Posted - 05/24/2013 :  02:50:45 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi greg.
I have a very similar problem. My code is very close to luaransl´s except that I defined a stringarray to choose the filetype additionally. I want to import multiple files. Ok, I cannot use GetOpenBox. But what do you mean with "You just need to hook that code to yours ..."? What does the final code look like? Here is my code:
void Part_Imp()
{
Worksheet wks = Project.ActiveLayer();

ASCIMP ai;
StringArray saFileTypes;
saFileTypes.SetSize (2);
saFileTypes[0]="*.txt";
saFileTypes[1]="*.dat";
string strFile = GetOpenBox(saFileTypes, GetAppPath());
if(!strFile.IsFile())
{
strFile = GetOpenBox(saFileTypes, GetAppPath());
if(strFile.IsEmpty())
return;
}
if(AscImpReadFileStruct(strFile, &ai)==0)

{
ai.iMode = ASCIMP_MODE_APPEND_COLS;
ai.iRenameCols = 0;
ai.iPartial = 1;
ai.iPartialC1 =
ai.iPartialC2 =
ai.iPartialR1 = 0;
ai.iPartialR2 = 9;
ai.iDelimited = 1;
ai.iDelimiter = ASCIMP_DELIM_DEMICOLON;
ai.nNumSep = NF_IS_EUROPEAN;
int nRet = wks.ImportASCII(strFile, ai);
out_int("nRet = ", nRet);
if(0 == nRet)
wks.AutoSize();
}
else
out_str("failed to read ascii file");

Thanks a lot in advance.
Regards
Go to Top of Page

Thomas83

24 Posts

Posted - 05/24/2013 :  03:02:09 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply

I would like to have all imported files in only one worksheet!
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000