Subscribe Bookmark RSS Feed

Append text to Save Text File function

vishwasanj

Frequent Contributor

Joined:

Dec 27, 2016

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.

 

3 REPLIES
Craige_Hales

Staff

Joined:

Mar 21, 2013

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

Joined:

Mar 17, 2015

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. 

 

 

txnelson

Super User

Joined:

Jun 22, 2012

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