cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
B1234
Level III

Loop Saving Files

Hi, I have a script where a file saves to a particular location. I am saving data files as a string of the product lot number and raw data. I want to be able to run the script multiple times for the same lot number without the files saving over themselves. I want to create some sort of loop that if the file name already exists, then it will save as  lotnumber Raw Data (1), lotnumber Raw Data (2) etc. This is what I have coded now, but I don't know how to make a loop with it. Can someone help make this a loop please?

 

 

lotnumer = 1234;

path = "C:\Users\Desktop\JMP\";

rawdatafile = (path || "_" || lotnumber || " Raw Data.jmp");

If( File Exists( rawdatafile ),
    dt << save as( path || "_" || lotnumber || " Raw Data (1).jmp" ),
    dtrlu << save as( path || "_" || lotnumber || " Raw Data.jmp" )
);

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Loop Saving Files

Here is a simple example that loops until no matching file is found, and then saves the file under a new incremented

lotnumer = "1234";

path = "C:\Users\Desktop\JMP\"; 

rawdatafile = (path || "_" || lotnumber || " Raw Data.jmp");

If( File Exists( rawdatafile ),
	i = 1;
	While(
		File Exists(
			path || "_" || lotnumber || " Raw Data (" || Char( i ) ||  ").jmp"),
		i++
	);
	dt << save as( path || "_" || lotnumber || " Raw Data (" || Char( i ) ||  ").jmp" );
,
	dtrlu << save as( path || "_" || lotnumber || " Raw Data.jmp" )
);
Jim

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: Loop Saving Files

Here is a simple example that loops until no matching file is found, and then saves the file under a new incremented

lotnumer = "1234";

path = "C:\Users\Desktop\JMP\"; 

rawdatafile = (path || "_" || lotnumber || " Raw Data.jmp");

If( File Exists( rawdatafile ),
	i = 1;
	While(
		File Exists(
			path || "_" || lotnumber || " Raw Data (" || Char( i ) ||  ").jmp"),
		i++
	);
	dt << save as( path || "_" || lotnumber || " Raw Data (" || Char( i ) ||  ").jmp" );
,
	dtrlu << save as( path || "_" || lotnumber || " Raw Data.jmp" )
);
Jim
B1234
Level III

Re: Loop Saving Files

This is exactly what I needed! Thank you!