WIN32_FIND_DATA and FtpFindFirstFile_()

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by altesocke.

hi ppl,

i hope anyone can help me.

i try to get a file listing from a ftp-server. as i don´t got it with the network lib, i used api-calls.
but now the compiler says that my syntax is not correct:

;---------------only FTP-CODE---------------
SPS.WIN32_FIND_DATA

Dim file_ar.WIN32_FIND_DATA(5)

#INTERNET_DEFAULT_FTP_PORT = 21
#INTERNET_SERVICE_FTP = 1
#INTERNET_OPEN_TYPE_DIRECT = 1
#MAX_PATH = 260
userAgent$ = "Ftp-Tool"
user$ = "anonymous"
pass$ = "[url]mailto:alex@rechenkeks.de[/url]"

hopen = InternetOpen_(userAgent$, #INTERNET_OPEN_TYPE_DIRECT, "", "", 0)

conn = InternetConnect_(hopen, 213.160.69.175, #INTERNET_DEFAULT_FTP_PORT, user$, pass$, #INTERNET_SERVICE_FTP, "", 0) ;connect to server

file_find = FtpFindFirstFile_(conn, "*.*", file_ar(0), 0, 0) ;get first file
If file_find=0
MessageRequester("Info", "No file found!", 0)
Else
MessageRequester("Info", "first file found!", 0)
EndIf

file_name$ = file_ar(0)\cFileName ;<---- Syntax Error??

MessageRequester("Info", "File Name = "+file_name$, 0)

;------------------------------------------------------------------

cu socke
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Yes, WINDOWS structure define a string as an array of byte. So you have to specify [0] at the end. And as you want the address, put a '@' before. Correct line is:

file_name$ = PeekS(@file_ar(0)\cFileName[0])


Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by altesocke.

thx fred, but...
cFileName is the file name as a string, isn´t it?

PeekS(@file_ar(0)\cFileName[0]) is always a dot (.) and
file_ar(0)\cFileName[0] is always 46 as numeric.

how can i get the filename as a string in the array?
or how can i get a filelisting with the network lib?

cu socke
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

A dot (.) seems correct to me as your function returns only the first file founds. So a FTP directory is unix like and looks like that:

.
..
Directoy1
Directoy2
file1

Just use the function to get the next file in the FTP dir ! (may be FtpFindNextFile_(conn, "*.*", file_ar(0), 0, 0)) but not sure


Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by altesocke.

it works, i got my file listing.
the api-call for the next file is InternetFindNextFile_().

and now i try it with the network lib.

cu socke



Edited by - altesocke on 11 February 2002 00:40:07
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by altesocke.

hmm, it worked with an array for the filenames.
but in real-life, i don´t know how much files i have on the ftp.

so i tried it with a linked-list.
at first it seems to work, then the prog crashes.

can anyone help me?

see my source at the bottom.

cu socke

;---------------------------------------------------------------------
server$ = "213.160.69.175" ;rechenkeks.de
userAgent$ = "Ftp-Tool"
user$ = "anonymous"
pass$ = "[url]mailto:alex@rechenkeks.de[/url]"

SPS.WIN32_FIND_DATA

Newlist file_lst.WIN32_FIND_DATA()

InitGadget(20)

If OpenWindow(0, 100, 150, 410, 450, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget, "File Listing")

If CreateGadgetList(WindowID())

ListViewGadget(0, 10, 10, 300, 400)
ButtonGadget(1, 320, 10, 75, 25, "Log In")
ButtonGadget(2, 320, 45, 75, 25, "Get Files")
ButtonGadget(3, 320, 80, 75, 25, "Trennen")

EndIf

Repeat

Select WaitWindowEvent()

Case #PB_EventGadget

Select EventGadgetID()

Case 1 ;log in
ClearGadgetItemList(0)
#INTERNET_DEFAULT_FTP_PORT = 21
#INTERNET_SERVICE_FTP = 1
#INTERNET_OPEN_TYPE_DIRECT = 1
#MAX_PATH = 260
#INTERNET_FLAG_PASSIVE = 0
hopen = InternetOpen_(userAgent$, #INTERNET_OPEN_TYPE_DIRECT, "", "", 0)
conn = InternetConnect_(hopen, server$, #INTERNET_DEFAULT_FTP_PORT, user$, pass$, #INTERNET_SERVICE_FTP, #INTERNET_FLAG_PASSIVE, 0)
MessageRequester("Info", Str(conn), 0)

Case 2 ;file listing
cnt = 0

AddElement(file_lst()) ;linked-list
file_find = FtpFindFirstFile_(conn, "*.*", file_lst(), 0, 0) ;linked-list

Repeat

cnt = cnt+1

AddElement(file_lst())
next_file_find = InternetFindNextFile_(file_find, file_lst()) ;linked-list

Until next_file_find = 0
InternetCloseHandle_(file_find)

file_cnt = CountList(file_lst())
ResetList(file_lst())
While NextElement(file_lst())
file_name$ = PeekS(@file_lst()\cFileName[0])
AddGadgetItem(0, -1, file_name$)
Wend

MessageRequester("Info", "Alle Files gelesen!", 0)

Case 3 ;verbindung trennen
discon = InternetCloseHandle_(conn)
MessageRequester("Info", Str(discon), 0)
InternetCloseHandle_(hopen)

EndSelect

Case #PB_EventCloseWindow
close = 1

EndSelect

Until close = 1

EndIf

End



Edited by - altesocke on 20 February 2002 23:59:11
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
hmm, it worked with an array for the filenames.
but in real-life, i don´t know how much files i have on the ftp.

so i tried it with a linked-list.
at first it seems to work, then the prog crashes.

can anyone help me?
Hi i tried your program and it did crash, i traced the crash to the second call to the 'AddElement()' command. There seem to be some problems with the '.WIN32_FIND_DATA' structure probably some memory outside of this structure is overwritten by the call to 'FTPFindFirstFile' and 'InternetFindNextFile' and that's what causing the crashes.

The cure then... Well see below to learn what i did to get your program to work:

Code: Select all

; I declared a new WIN32_FIND_DATA structure!
#MAX_PATH=1024 ; This should be enough for most files

Structure NEW_WIN32_FIND_DATA
  dwFileAttributes.l
  ftCreationTime.FILETIME 
  ftLastAccessTime.FILETIME
  ftLastWriteTime.FILETIME
  nFileSizeHigh.l
  nFileSizeLow.l
  dwReserved0.l
  dwReserved1.l
  cFileName.b[#MAX_PATH]
  cAlternateFileName.b[14]
EndStructure
; The only thing left is to change all
; variable.WIN32_FIND_DATA to variable.NEW_WIN32_FIND_DATA
; and that's it!
Enjoy!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by altesocke.

hum, is it working for you?

tried it, but it crashes again.

Win2000, SP2, PB 2.90

cu socke
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
hum, is it working for you?
tried it, but it crashes again.
Win2000, SP2, PB 2.90
cu socke
Ok i tested it again, and now for some reason i did not work, when i changed the #MAX_PATH to 2048 and compiled again, it worked, then i changed back to 1024 and compiled and it still worked so i'm a little confused here. Try changing this aswell and see if something happends.
I'm guessing that you've already changed these two lines but if you haven't do it!(that is if you're using the structure i provided in my previous post)

Code: Select all

SPS.NEW_WIN32_FIND_DATA

NewList file_lst.NEW_WIN32_FIND_DATA()
Sorry but other than this i can not help you, your code seems, to me, to be correct and as far as i see it has no logical flaws.

Update...
I tried with a single variable in 'FtpFindFirstFile' and it crashed every time i.e. i changed to the code below:

Code: Select all

; the line below produces a crash everytime
FtpFindFirstFile_(conn, "*.*", tmp1.WIN32_FIND_DATA, 0, 0)

; but the next line works fine(#MAX_PATH=2048) (see structure
; in previous post)
FtpFindFirstFile_(conn, "*.*", tmp2.NEW_WIN32_FIND_DATA, 0, 0)


Edited by - Pupil on 21 February 2002 18:08:13
Post Reply