I have the following Function that copies files. The function shows a percent done but I would like to show the user a "Time Remaining." To further things along aI found a java script and formula that should show the time remaining but I am having trouble converting it to LUA

the formula: time diff*bytes copied/bytes remaining.

I put my entire Copy function and the sample java script code below.

The biggest problem is

Code:
function COPYCB(Source,Destination,Copied,Total,FileCopied)
	DlgProgressBar.SetPos(CTRL_PROGRESS_BAR_02, (nfl / nflcnt) * 100);
	E_Time = System.GetTime(TIME_FMT_MIN)..":"..System.GetTime(TIME_FMT_SEC)
	End_Time = Time2Num(E_Time);--Start  End 
	Elapsed = End_Time - Start_Time;--Elapsed 40
	bytescopyied = BytesCSF - OBytesCSF
	bytesremaining = TBytes - BytesCSF
	T_Remaining = Elapsed * bytescopyied / bytesremaining  
	T_Left = Time2Str(T_Remaining)
    --Dialog.TimedMessage("Please Wait...", Start_Time.." - "..End_Time.." = "..Elapsed.." BytesCSF: "..BytesCSF.." TBytes: "..TBytes.." Pcnt: "..Pcnt.." "..T_Left.."\r\n", 500, MB_ICONINFORMATION);

	return true; --continue copying
end    


function Delfls(Path,delSF,delTOT)
flscnt = flscnt + 1
    Pcnt = Math.Round((flscnt / TFECount * 100), 0)
	if Pcnt == 50 and tp50 == false then 
		DlgProgressBar.SetPos(CTRL_PROGRESS_BAR_01,(DlgProgressBar.GetPos(CTRL_PROGRESS_BAR_01)+ 1));
		tp50 = true
	end
	if Pcnt == 75 and tp75 == false then 
		DlgProgressBar.SetPos(CTRL_PROGRESS_BAR_01,(DlgProgressBar.GetPos(CTRL_PROGRESS_BAR_01)+ 1));
		tp75 = true
	end
    return true;
end

function copyfls(Srce,Dst,MDerror)
--set source and destination values, then 'find files' to table
g_FolderSize = 0;TCOPIED = 0;T_str = "  ";
NUM_OSEC = String.ToNumber(System.GetTime(TIME_FMT_SEC));

local Ssrce = Srce;
local Sdest = Dst;
local l25 = false 
local l50 = false
local l75 = false
tfldrs = Folder.Find(Ssrce, "*", true, nil);
GTBytes = File.Find(Ssrce, "*.*", true, true, nil, gtbs);
local tfiles = File.Find(Ssrce, "*.*", true, false, nil, OnFileFound);
if tfiles then
	if (not Folder.DoesExist(Sdest)) then
		Folder.Create(Sdest); --create Destination 'Parent Folder
	end
	if tfldrs then
		LnSsrce = String.Length(Ssrce);
		Allfldrs = Table.Count(tfldrs);
		for x=1, Allfldrs do
			tst = String.Mid(tfldrs[x], LnSsrce, -1);
			tsttf = Folder.DoesExist(Sdest..tst);
			if testtf ~= true then
				Folder.Create(Sdest..tst);
			end 
		end	
	end
local nlngth = String.Length(Ssrce); nflcnt = Table.Count(tfiles);

--cycle and copy each file , calling callback function named: COPYCB()
	for n=1,nflcnt do
		nfl = n; local dest = String.Mid(tfiles[n], nlngth +1, -1);
		--note: it may be worth considering the File.Install action instead of file.copy
		
		BytesCSF = BytesCSF + File.GetSize(tfiles[n]);
		File.Copy(tfiles[n], Sdest..dest, true, true, true, true, COPYCB);
		
		MDerror = Application.GetLastError()
		if MDerror > 0 then
			SetupData.WriteToLogFile("Error could not copy "..tfiles[n].." to "..Sdest..dest.."\r\n", true);
		end
			
		--Jerry's Update Progress Bar
		Pcnt = Math.Round((nfl / nflcnt * 100), 0)
		if Pcnt == 25 and l25 == false then
			DlgProgressBar.SetPos(CTRL_PROGRESS_BAR_01,(DlgProgressBar.GetPos(CTRL_PROGRESS_BAR_01)+ 1));
			l25 = true
		end
		if Pcnt == 50 and l50 == false then 
			DlgProgressBar.SetPos(CTRL_PROGRESS_BAR_01,(DlgProgressBar.GetPos(CTRL_PROGRESS_BAR_01)+ 1));
			l50 = true
		end
		if Pcnt == 75 and l75 == false then 
			DlgProgressBar.SetPos(CTRL_PROGRESS_BAR_01,(DlgProgressBar.GetPos(CTRL_PROGRESS_BAR_01)+ 1));
			l75 = true
		end
		gpos = DlgProgressBar.GetPos(CTRL_PROGRESS_BAR_01)
		shrt = String.AbbreviateFilePath(tfiles[n], 72);
		DlgStaticText.SetProperties(CTRL_STATICTEXT_LABEL_04,{Text = shrt.."  "});
		DlgProgressBar.SetProperties(CTRL_PROGRESS_BAR_02, {Text = Pcnt.."%"..T_Left});
		tpnct = Math.Round(gpos * 5,0)
		if gpos == 0 then
			DlgProgressBar.SetProperties(CTRL_PROGRESS_BAR_01, {Text = "0%"});	
		else  
			DlgProgressBar.SetProperties(CTRL_PROGRESS_BAR_01, {Text = tpnct.."%"});
		end
	OBytesCSF = BytesCSF
	end
end
nflcnt = nil; nfl = nil; --clear the two global values

end


-- This function determines the size of a file and adds it to the total.
-- This function is used as the callback function for the File.Find action in this example.
function OnFileFound(Path)
    local nSize = File.GetSize(Path);
    g_FolderSize = g_FolderSize + nSize;
    
    return true;
end
function gtbs(Path)

    local nSize = File.GetSize(Path);
    TBytes = TBytes + nSize;
    
    return true;
end
The Java Script code

Code:
Well there are various formules you can use. A simple one is:
time diff*bytes copied/bytes remaining

int timeDiff = newTime - oldTime;
int bytesCopied = currentCopiedBytes - oldCopiedBytes;
int bytesRemaining = size - currentCopiedBytes;
String format = "";
try
{
  int timeRemaining = timeDiff*bytesCopied/bytesRemaining;
  int sec = (timeRemaining/1000) % 60;
  int min = (timeRemaining/(1000*60)) % 60;
  int hours = (timeRemaining/(1000*60*60)) % 24;
  int days = (timRemaining/(1000*60*60*24)) % 7;
  int weeks = (timeRemaining/(1000*60*60*24*7)); 
  if (sec != 0)
     format = sec + " seconds";
  if (min != 0)
     format = min + " minutes " + format; 
  //ect
}
catch(ArithmeticException e)
{
  //we did not recieve any bytes durring the last transfer
  format = "NEVER!"
}