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
|
|
|