PDA

View Full Version : déjà vu


Bruce
10-16-2007, 10:40 AM
I've brought this up before, sorry, I just can't seem to work through it.
I get an error stating:
On click, line 32: attempt to index feild '?'(a number value)
the red below shows the line. Help please.

-- step 1: connect anonymously to your web site.com
FTP.Connect("www.diginetx.com", "logon", "password", "", true);
err = Application.GetLastError();
if err ~= FTP.OK then
Dialog.Message("Error", _tblErrorMessages[err]);
end

--step 2: change to the directory where the file you want is located
FTP.ChangeDir("websites/diginetx.com/docs/folder1/folder2");
err = Application.GetLastError();
if err ~= FTP.OK then
Dialog.Message("Error", _tblErrorMessages[err]);
end

ftp_file_result = FTP.ListFiles();

--stop the listbox from updating
ListBox.SetUpdate("DirList", false);

-- empty the listbox
ListBox.DeleteItem("DirList",LB_ALLITEMS);

tbFiles = FTP.ListFiles();

-- add each item to the listbox in this format:
-- <name>
for numIndex, tblInfo in tbFiles do
--if String.SplitPath(tbFiles.Name).Extension == ".zip" then
if String.SplitPath(tbFiles[numIndex]["Name"]).Extension == ".zip" then
ListBox.AddItem("DirList", tblInfo.Name .. "");

end

--allow the listbox to update
ListBox.SetUpdate("DirList", true);

FTP.Disconnect ();
end

Bruce
10-16-2007, 11:46 PM
Bump :D Com'on ya old salts! Where is this thing jacked up?

arb
10-17-2007, 01:53 AM
if String.SplitPath(tbFiles[numIndex]["Name"]).Extension == ".zip" then


I haven't FTP Plugin here but i think you should try this

if String.Right(tbFiles[numIndex].Name, 3) == "zip" then

Bruce
10-17-2007, 10:39 AM
I haven't FTP Plugin here but i think you should try this

if String.Right(tbFiles[numIndex].Name, 3) == "zip" then

Hey arb thanks for the reply! But... nope , same error message. ;)

mindstitchdr
10-17-2007, 10:59 AM
Try changing:
if String.SplitPath(tbFiles[numIndex]["Name"]).Extension == ".zip" then

To:
tbExt = String.SplitPath(tbFiles);
if tbExt.Extension == ".zip" then;

Lorne
10-17-2007, 11:30 AM
Try this for the loop instead.

--stop the listbox from updating
ListBox.SetUpdate("DirList", false);

-- empty the listbox
ListBox.DeleteItem("DirList",LB_ALLITEMS);

tbFiles = FTP.ListFiles();

-- can disconnect here (unless the FTP connection is needed for something else later)
FTP.Disconnect();

for numIndex, tblInfo in tbFiles do
if tblInfo then
local tbPath = String.SplitPath(tblInfo.Name);
if (tbPath) and (tbPath.Extension == ".zip") then
ListBox.AddItem("DirList", tblInfo.Name);
end
end
end

--allow the listbox to update
ListBox.SetUpdate("DirList", true);



Do you really want to call FTP.Disconnect() each time through the loop? I would disconnect right after calling FTP.ListFiles (assuming you don't need the FTP connection for anything else later).
You don't need to call FTP.ListFiles() twice.
Use code tags when posting script, it preserves the indentation and makes it easier to read.

Lorne
10-17-2007, 11:33 AM
Try changing:
if String.SplitPath(tbFiles[numIndex]["Name"]).Extension == ".zip" then

To:
tbExt = String.SplitPath(tbFiles);
if tbExt.Extension == ".zip" then;

No. tbFiles contains numerically indexed subtables, it doesn't contain a path string.

mindstitchdr
10-17-2007, 03:04 PM
Sorry just trying to help.

Lorne
10-17-2007, 03:32 PM
Sorry just trying to help.
That's okay! I just wanted to point it out since both you and arb suggested the same thing. It's a pretty easy mistake to make when reading code like that.

arb
10-17-2007, 10:55 PM
if String.Right(tbFiles[numIndex].Name, 3) == "zip" then

AAAAAAAAH, that was stupid, I forgot what i had in mind

if String.Right(tbFiles.Name, 3) == "zip" then

Is that true?

bule
10-18-2007, 03:13 AM
I would use it like this:

if String.Lower(String.Right(tbFiles[numIndex].Name, 3)) == "zip" then

Lorne
10-18-2007, 09:01 AM
AAAAAAAAH, that was stupid, I forgot what i had in mind

if String.Right(tbFiles.Name, 3) == "zip" then

Is that true?Ah, sorry arb I misremembered your post, you were right the first time -- I forgot you had the [numIndex] accessor in there.

tbFiles.Name won't work, because there is no "Name" element in the tbFiles table. The Name element is in one of the subtables inside the tbFiles table. So it's tbFiles[numIndex].Name, assuming numIndex is in range.

But there's no need to use tbFiles at all if you're using a for loop to get each key/value pair in tbFiles.


for numIndex, tblInfo in tbFiles do
-- at this point, tblInfo is the same as tbFiles[numIndex]
end

-- same thing with different variable names:
for k, v in tbFiles do
-- at this point, v is the same as tbFiles[k]
end

Bruce
10-18-2007, 11:49 AM
Try this for the loop instead.

--stop the listbox from updating
ListBox.SetUpdate("DirList", false);

-- empty the listbox
ListBox.DeleteItem("DirList",LB_ALLITEMS);

tbFiles = FTP.ListFiles();

-- can disconnect here (unless the FTP connection is needed for something else later)
FTP.Disconnect();

for numIndex, tblInfo in tbFiles do
if tblInfo then
local tbPath = String.SplitPath(tblInfo.Name);
if (tbPath) and (tbPath.Extension == ".zip") then
ListBox.AddItem("DirList", tblInfo.Name);
end
end
end

--allow the listbox to update
ListBox.SetUpdate("DirList", true);



Do you really want to call FTP.Disconnect() each time through the loop? I would disconnect right after calling FTP.ListFiles (assuming you don't need the FTP connection for anything else later).
You don't need to call FTP.ListFiles() twice.
Use code tags when posting script, it preserves the indentation and makes it easier to read.


Unfortunently I still get the same ERROR... But thank you Lorne

Lorne
10-18-2007, 12:16 PM
Unfortunently I still get the same ERROR... But thank you LorneWhat line causes the error now?

Bruce
10-18-2007, 12:21 PM
The same bugger!
if String.SplitPath(tbFiles[numIndex]["Name"]).Extension == ".zip" then

Lorne
10-18-2007, 12:39 PM
The same bugger!
if String.SplitPath(tbFiles[numIndex]["Name"]).Extension == ".zip" thenThat line isn't in the code I posted.

What error do you get when you use the version I suggested.

You might want to post your entire script for that event, between [CODE] blocks.

Dermot
10-18-2007, 12:39 PM
if String.SplitPath(tbFiles[numIndex]["Name"]).Extension == ".zip" then
But that line is not in Lorne's code.

This is Lorne's code
ListBox.SetUpdate("DirList", false);

-- empty the listbox
ListBox.DeleteItem("DirList",LB_ALLITEMS);

tbFiles = FTP.ListFiles();

-- can disconnect here (unless the FTP connection is needed for something else later)
FTP.Disconnect();

for numIndex, tblInfo in tbFiles do
if tblInfo then
local tbPath = String.SplitPath(tblInfo.Name);
if (tbPath) and (tbPath.Extension == ".zip") then
ListBox.AddItem("DirList", tblInfo.Name);
end
end
end

--allow the listbox to update
ListBox.SetUpdate("DirList", true);

Bruce
10-18-2007, 01:45 PM
oops sorry, this one: (see Image below)



local tbPath = String.SplitPath(tblInfo.Name);

Bruce
10-18-2007, 01:46 PM
That line isn't in the code I posted.

What error do you get when you use the version I suggested.

You might want to post your entire script for that event, between [CODE] blocks.

The script is at the very top, less your idea. ;)

Dermot
10-18-2007, 01:59 PM
This works for me

ListBox.SetUpdate("DirList", false);

-- empty the listbox
ListBox.DeleteItem("DirList",LB_ALLITEMS);

tbFiles = FTP.ListFiles();

-- can disconnect here (unless the FTP connection is needed for something else later)
FTP.Disconnect();

if tblFiles then

for i = 1, tbFiles.Count do

local strFileExt = String.Right(tblFiles[i].Name, 4)
if strFileExt == ".zip" then
ListBox.AddItem("DirList", tblFiles[i].Name);
end

end

end

--allow the listbox to update
ListBox.SetUpdate("DirList", true);

Lorne
10-18-2007, 02:19 PM
Ah, serves me right for not trying my code first. I completely forgot that the FTP.ListFiles action stores the number of items in a Count element inside the table it returns.

If you use the "for each" style of loop on that table, you'll also call SplitPath for that Count element, which is numeric.

Dermot's version is the way to go. (Thanks Dermot!)

Bruce
10-18-2007, 02:27 PM
Now I get nothing... :huh I'll look closer. List box is not updating, no more error box though!

Here's the entire code again:

-- step 1: connect anonymously to your web site.com

FTP.Connect("www.diginetx.com", "log in", "password", "", true);
err = Application.GetLastError();
if err ~= FTP.OK then
Dialog.Message("Error", _tblErrorMessages[err]);
end

--step 2: change to the directory where the file you want is located
FTP.ChangeDir("websites/diginetx.com/docs/file1/file2");
err = Application.GetLastError();
if err ~= FTP.OK then
Dialog.Message("Error", _tblErrorMessages[err]);
end





--############### Demots code #################################

ListBox.SetUpdate("DirList", false);

-- empty the listbox
ListBox.DeleteItem("DirList",LB_ALLITEMS);

tbFiles = FTP.ListFiles();

-- can disconnect here (unless the FTP connection is needed for something else later)
FTP.Disconnect();

if tblFiles then

for i = 1, tbFiles.Count do

local strFileExt = String.Right(tblFiles[i].Name, 4)
if strFileExt == ".zip" then
ListBox.AddItem("DirList", tblFiles[i].Name);
end

end

end

--allow the listbox to update
ListBox.SetUpdate("DirList", true);

Lorne
10-18-2007, 04:17 PM
It's this line:

if tblFiles then

...but your variable name is actually tbFiles, not tblFiles, so that test will never succeed.

BTW, please, please, please learn how to use [code] blocks.

Dermot
10-18-2007, 04:23 PM
It's this line:

if tblFiles then
Yes my fault, sorry. Just make sure the variable names match and she should work fine.

Bruce
10-18-2007, 05:19 PM
It's this line:
BTW, please, please, please learn how to use [code] blocks.

Oops sorry, you got it! :yes :o

Works great, thanks guys

arb
10-19-2007, 11:20 PM
And happily ever after ... ;):yes