cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
vishwasanj
Level V

Append text to Save Text File function

Hi All,

 

I am using this function as part of the FOR loop.

 

 

If( File Exists(location)
csvlist[N Items(FilesIndex)]= Open(location),
//else below
continue() &      // I am not sure this & function works.
Save Text File( path || "\" || "error.txt", "
\!"File doesnt exist\!";
" ||char(location)) ;
);

 

 

Basically what I want to do is, I want to open the location if file exists, if not, I want to create a text file and iterate the loop. Also, I want to keep appending to the same error. txt file without replacing it everytime.

 

Anyone know how to do this? I really appreciate your help.

Thank you.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Append text to Save Text File function

The scripting index is a great place to look up JSL functions.

Scripting Index showing mode("append")Scripting Index showing mode("append")

You probably don't want to use continue() like that. Continue() and Break() are statements in JSL that jump to the end of the containing loop and either continue the loop or break out of the loop. Possibly you want 

for(i=1,i<10,i+=1,
  if( condition,
    doSomethingIfTrue();
    doMoreThingsIfTrue();
  , // else
    doSomethingIfFalse();
    continue();
  ); // end of if statement
  codeThatIsSkippedByContinue();
); // end of for loop
Craige

View solution in original post

3 REPLIES 3
Craige_Hales
Super User

Re: Append text to Save Text File function

The scripting index is a great place to look up JSL functions.

Scripting Index showing mode("append")Scripting Index showing mode("append")

You probably don't want to use continue() like that. Continue() and Break() are statements in JSL that jump to the end of the containing loop and either continue the loop or break out of the loop. Possibly you want 

for(i=1,i<10,i+=1,
  if( condition,
    doSomethingIfTrue();
    doMoreThingsIfTrue();
  , // else
    doSomethingIfFalse();
    continue();
  ); // end of if statement
  codeThatIsSkippedByContinue();
); // end of for loop
Craige
vince_faller
Super User (Alumni)

Re: Append text to Save Text File function

Continue() is kind of like break. It immediately iterates and skips everything past it.  For instance

for(i=1, i<=50, i++, 
	errortxt = ""
	If( File Exists( location ),
		csvlist[N Items( FilesIndex )] = Open( location )
	, //else
		//could just concat one text file and save after the for loop
		errortxt ||= "File doesnt exist" || Char( location ) ) || "\!n" //just a newline to make it more readable
		//or if you just want to append to the file
		Save Text File( path || "\" || "error.txt", 
			"File doesnt exist" || Char( location ) ) || "\!n", 
			mode("append")
		);
		Continue();
	);
	print("Doing other stuff")
	
);
//if using the errortxt method
Save Text File( path || "\" || "error.txt", errortxt);

In this script, if it doesn't find the file, it will either append to the file or (really it does both) concatenate the error to the errortxt string.  Once it hits Continue(), it will immediately start the for loop over.  Skipping Print("Doing other stuff").  Check out the scripting index under Help>>Scripting Index, it has really good documentation and examples on the functions and how they work.  

Hope this helped. 

 

 

Vince Faller - Predictum
txnelson
Super User

Re: Append text to Save Text File function

The logic to your code is there.  You just don't seem to be looking up the syntax or the definitions in the Scripting Index.

If( File Exists(location)

you are missing a comma and/or a completion

If( File Exists(location),

or

If( File Exists(location) == 1,

I don't understand the logic of the next line

csvlist[N Items(FilesIndex)]= Open(location),

the way it is written, the pointer to every data table would be written on top of each other, unless somewhere in your code FilesIndex is getting added to between openning data tables.  I suggest an easier, safer method

dt = Open(location);
insert into(csvlist,dt),

there is no need for "continue", the code will contine automatically 

Your "Save Text File" function is syntactically fine, if you want the each execution to write over the previous execution of the line.  Take a look in the Scripting Index to see how to keep from doing that if you want to add to the file rather than writing on top of it.

I would also suggest that if you want the error lines to be added to the code, that you add "\!n" to each line, to force each listing to a new line.

Jim