I noticed a typo in your code
i<=1number
As James pointed out, this is indeed a very tricky issue, as the XF framework will create project variable if not found. To help others, I posted code below to make it clear.
dlgpath;//pick a path and result put to path$
pathfolder$ = path$; // Main folder
int number = 2; // Number of subfolders
string results$;//use declared session variable
findFolders folder:= results$ path:=pathfolder$ addpath:=1 n:=number; // Find the subfolders
for (int i=1; i<=number; i++) // For each subfolder
{
string fld$ = results.GetToken(i, CRLF)$;
string fns;
findFiles path:=fld$ ext:="*.opj" addpath:=1 fname:=fns;
string FileName$ = fns.GetToken(1,CRLF)$;
if(FileName.IsEmpty())
continue;
doc -s;
doc -d; // Close the previous file
doc -o %(FileName$); // Open the Origin file
type -v "proj: %X%G";
}
CP