cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
shampton82
Level VII

Script to copy script file code into a text file and then be able to read out

Hey user group!

I had a question on how to create a scrip that would 1: loop through a folder that contains a bunch of scripts, copy the name of the script file and then copy the script itself below the name into a text file and keep concatenating the text file with all the scripts from the folder 2: Be able to read in the text from the text file and create new script files into a chosen folder.  I can create the user interface side to choose what to do (save out or read in) and select the folders no problem but not sure on how to save the file name and script to the text file or read it in.  

 

So for example the text file it would have crated would look like this:

script one

names default to here(1);

dt1=current data table();

script two

names default to here(1);

dt2=current data table();

script 3

names default to here(1);

dt3=current data table();

 

and when read would output three script files each containing the code that was under the file name in the text file.

 

Any suggestions would be appreciated.

3 REPLIES 3
jthi
Super User

Re: Script to copy script file code into a text file and then be able to read out

Is there specific reason of combining the scripts like this instead of using Include?

 

Creating the text file is simple, below is one example

Names Default To Here(1);

path = "folder/";
path = Convert File Path(path, "windows");

scriptfiles = Files In Directory(path);

scripts = {};

For Each({filename}, scriptfiles,
	If(!Ends With(filename, ".jsl"),
		continue();
	);
	Insert Into(scripts, "//" || filename);
	Insert Into(scripts, Load Text File(path || filename));
);

txt = Concat Items(scripts, "\!N");
Save Text File(path || "total.jsl", txt, mode("replace"));

but creating the script files might not be as easy. How do you differentiate between JSL and file name? Do you have very specific file names?

-Jarmo
shampton82
Level VII

Re: Script to copy script file code into a text file and then be able to read out

Thanks @jthi , this worked pretty well!

I changed the start of a file to a more distinct text  of "//##" so now the text file looks like this

//##label script.jsl
//rev 5-9-25

names default to here(1);

dt=current datatable();

selected_rows=dt<<get selected rows();


//##Make a PDF of a data table.jsl

etc......

 

So now I would know that //## signals the start of a new script file and  that the name of the file lies between the //## and .jsl.  I would also know that between .jsl and the next //## is the text of the script.  So it looks like all the identifiers are there, just wondering if there is a way in JSL to read through the code in this way that can take advantage of these identifiers to reconstruct the files or if I'd have to do that portion of it in a different program.

 

Thanks for any additional thoughts!

 

jthi
Super User

Re: Script to copy script file code into a text file and then be able to read out

This doesn't really differ from text parsing and you can quite easily do it with JSL. If we assume that we have combined file like this

//##label script.jsl
//rev 5-9-25

names default to here(1);

dt=current datatable();

selected_rows=dt<<get selected rows();


//##Make a PDF of a data table.jsl

names default to here(1);

show(1);

and it should be separated into label script.jsl and Make a PDF of a data table.jsl, I would do something like this

Names Default To Here(1);

path = "$DOWNLOADS/comb_script.jsl";
save_path = "$DOWNLOADS/";
separator = "//##";


txt = Load Text File(path);
lines = Words(txt, "\!N");

aa_scripts = Associative Array();
cur_filename = "";
For Each({line}, lines,
	If(Starts With(line, separator), // new file
		filename = Substitute(line, separator, "");
		cur_filename = filename;
		aa_scripts[cur_filename] = {};
	,
		Insert Into(aa_scripts[cur_filename], line);
	);
);

For Each({{filename, script_lines}}, aa_scripts,
	script_txt = Concat Items(script_lines, "\!N");
	Save Text File(save_path || filename, script_txt);
);

Write();

Do note that this will remove extra row changes from your data due to how Words works. If you wish to keep them, you can loop over the text using Contains and Substr or something similar

Names Default To Here(1);

path = "$DOWNLOADS/comb_script.jsl";
save_path = "$DOWNLOADS/";
separator = "//##";


txt = Load Text File(path);

While(Length(txt) > 0,
	separator_start = Contains(txt, separator);
	separator_end = Contains(txt, separator, separator_start + 1);
	If(separator_end == 0,
		separator_end = Length(txt) + 1;
	);
	cur_script = Remove From(txt, 1, separator_end - separator_start);
	cur_filename = Remove From(cur_script, 1, Contains(cur_script, "\!N") - 1);
	cur_script = Trim Whitespace(cur_script);
	filename = Substitute Into(cur_filename, separator, "");
	
	Save Text File(save_path || cur_filename, cur_script);	
);
-Jarmo

Recommended Articles