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
 LabTalk Forum
 Looping over Origin files

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
Francesco84 Posted - 11/21/2018 : 06:19:02 AM
Origin Ver. OriginPro 2018 (64bit) and Service Release 1
Operating System: Windows 10

Hi everyone,
I would like to ask a question.

I need to open Origin files to perform the same operations on each one. Each file is contained in a sub-folder (all contained in a folder).

This is my code:

path_folder$ = %1; // subfolders path, passed as a parameter

findfolders path:=path_folders$
addpath:=1
n:=number; //subfolders number

for (int i=1; i<=number; i++)
{
string FileName$;

findFiles path:=folder.GetToken(i, CRLF)$
ext:="*.opj"
addpath:=1
fname:=FileName$; // File contained in the subfolder number i

doc -s;
doc -o %(FileName$); // Open the project


// Others operations...

}

I can open only the first files and not the next ones.

Indeed, I noticed that the string folder.GetToken(i, CRLF)$ no longer contains the path of the i-th subfolder.

Is there a way to solve the problem?

Thanks in advance,
Francesco











4   L A T E S T    R E P L I E S    (Newest First)
Francesco84 Posted - 11/23/2018 : 09:31:46 AM
Thank you so much Cpyang!

Francesco
cpyang Posted - 11/22/2018 : 1:42:40 PM
The issue was indeed due to OPJ open. You need to use locally declared variables such that they will survive between opening different files. In your code, you had used variable folder without declaration, and such variables will be removed when project is closed. Basically if you declare a Labtalk variable, its scope is with the code, but if not declared, Origin assume global project variables and the scope is with the current project. I know this was a very bad design, so we should do something to warn you of such situation.

The following is my code that is based on yours with folder list.


//declare default, assumed variables
string fname$;//assumed in FindFiles
string folder$;//assumed by findfolders
string path$ = %1; // path$ is assumed by findfolders
int number;
findfolders addpath:=1 n:=number;
for (int i=1; i<=number; i++)
{
	findFiles path:=folder.GetToken(i, CRLF)$
	ext:="*.opj"
	addpath:=1;
	if(fname.IsEmpty())
		continue;
	//use only the 1st file found
	string opjFile$ = fname.GetToken(1, CRLF)$;
	type "openning $(i): %(opjFile$)";
	
	doc -s;doc -d;
	doc -o %(opjFile$); // Open the project
	type "$(i): %G is openned.";
	// Others operations...

}


CP


Francesco84 Posted - 11/22/2018 : 06:44:40 AM
Hi Cpyang,
Thanks for the reply.

I get the folders list because I have a general folder, where inside there are subfolders (subfolder 1, subfolder2,...). Within each subfolder, there is a single Origin file (file1.opj in subfolder1, file2.opj in subfolder2,...)

So, I pass as a parameter the common path of the subfolders, and then I go to look for the file contained in each of them.


Unfortunately I could not solve the problem.
In fact, if I add the statement

type folder.GetToken(i, CRLF)$;

at the beginning of the For cycle, it works only for i = 1. For example, for i = 2, in output we have folder.GetToken(i, CRLF)$.

Instead, if I remove the "doc -o" statement, then the "type" statement works correctly. So I think the problem is related to the opening of a new project.

Francesco









cpyang Posted - 11/21/2018 : 9:03:38 PM
I changed to simpler code like this, as I don't quite understand why you separately get folder list.


[Main]
	run.section(,LoopFiles, C:\2016\Samples\);


[LoopFiles]
string path_folder$ = %1; // subfolders path, passed as a parameter
string fname$;
findfiles path:=path_folder$ ext:="*.opj" addpath:=1;
int number = fname.GetNumTokens(CRLF);

for (int i=1; i<=number; i++)
{
	string FileName$ = fname.GetToken(i, CRLF)$;
	type -q "$(i): %(FileName$)";
	doc -s 1;doc -d;
	doc -o %(FileName$);
	type "$(i): %(FileName$) is openned.";
}




CP


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