FTP recursive file list
Posted: Thu Mar 27, 2008 10:58 am
I'm trying to create a script (using also a routine from "ftp commander" stack) to get a complete list of files (path included) in a FTP site, including all files in subdirectories. The script works partially, but returns only 171 files of 460, and, after many tests, I cannot understand why. Any help?
on listDir myDir
set cursor to watch
put urlEncode(fld "USER") into tUs
put urlEncode(fld "PASS") into tPa
put "ftp://"&tUs&":"&tPa&"@"&myDir into ftpFolder
getFTPlist ftpFolder
put fld "ftp file list" into temp
repeat for each line x in temp
put myDir & "/" & x & return after fld "List"
end repeat
put fld "ftp folder list" into tDirList
repeat with x = 2 to the number of lines of tDirList
listDir (myDir & "/" & (line x of tDirList))
end repeat
end listDir
on getFTPlist ftpfolder
libURLSetFTPListCommand "NLST"
if char -1 of ftpfolder is not "/" then
put "/" after ftpfolder
end if
put URL ftpfolder into tData
if the result contains "error" then
answer the result
exit to top
end if
replace crlf with cr in tData
replace lf with cr in tData
libURLSetFTPListCommand "LIST"
put URL ftpfolder into tLongData
put empty into fld "ftp folder list"
put empty into fld "ftp file list"
repeat with x = 1 to the number of lines of tData
get line x of tData
get lineoffset(it, tLongData)
if char 1 of line it of tLongData is "d" or char 1 of line x of tLongData is "l" then
put line x of tData & cr after fld "ftp folder list"
else
put line x of tData & cr after fld "ftp file list"
end if
end repeat
end getFTPlist
on listDir myDir
set cursor to watch
put urlEncode(fld "USER") into tUs
put urlEncode(fld "PASS") into tPa
put "ftp://"&tUs&":"&tPa&"@"&myDir into ftpFolder
getFTPlist ftpFolder
put fld "ftp file list" into temp
repeat for each line x in temp
put myDir & "/" & x & return after fld "List"
end repeat
put fld "ftp folder list" into tDirList
repeat with x = 2 to the number of lines of tDirList
listDir (myDir & "/" & (line x of tDirList))
end repeat
end listDir
on getFTPlist ftpfolder
libURLSetFTPListCommand "NLST"
if char -1 of ftpfolder is not "/" then
put "/" after ftpfolder
end if
put URL ftpfolder into tData
if the result contains "error" then
answer the result
exit to top
end if
replace crlf with cr in tData
replace lf with cr in tData
libURLSetFTPListCommand "LIST"
put URL ftpfolder into tLongData
put empty into fld "ftp folder list"
put empty into fld "ftp file list"
repeat with x = 1 to the number of lines of tData
get line x of tData
get lineoffset(it, tLongData)
if char 1 of line it of tLongData is "d" or char 1 of line x of tLongData is "l" then
put line x of tData & cr after fld "ftp folder list"
else
put line x of tData & cr after fld "ftp file list"
end if
end repeat
end getFTPlist