 |

09-29-2003, 07:59 AM
|
|
Centurion
|
|
Join Date: Mar 2002
Location: Michigan
Posts: 186
|
|
FTP not setting current directory
|
I have a function that needs to gather some file info from an FTP site. The root of the FTP site contains X numbers of folders.
My current function connects to the FTP site and loops through the root directory gathering all the directory names and sotring them into an array. Then in a second loop I loop through the array, set the current directory to the directory name in the array, and then do a search of the files in that dir.
The first section works fine and it gathers all the dir names correctly. It is the second loop that doesn't work. It doesn't seem to set the directory, although it will return True, and claims there are 0 files in every directory.
Most of my function is below. I have a connection function that is called before this and hConnection is a module-wide variable.
Code:
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the first directory
hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
'if there are no dirs, then exit sub
If hFind = 0 Then
MsgBox "No directories found!"
Exit Sub
End If
'*********************************
' Gather all the directory names *
'*********************************
dirArray(0) = String(MAX_PATH, 0)
dirArray(0) = (Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1))
dirCount = 0
Do
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the next file
lRet = InternetFindNextFile(hFind, pData)
'if there's no next file, exit do
If lRet = 0 Then Exit Do
dirCount = dirCount + 1
dirArray(i) = String(MAX_PATH, 0)
'save the filename
dirArray(dirCount) = (Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1))
Loop
'*******************************
' Gather File info in each dir *
'*******************************
For i = 0 To dirCount
buffer = Space(200)
'Set up new directory
If FtpSetCurrentDirectory(hConnection, "/" & dirArray(i)) = False Then
ShowError 'This function calls getLastError and does a MsgBox
End If
'This is here so I can see if the current Dir is correct if I want
'FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
pData.cFileName = String(MAX_PATH, 0)
'find the first file. I have tried *.* here as well with the same results
hFind = FtpFindFirstFile(hConnection, "*.Doc", pData, 0, 0)
fCount = 0
'Make sure there are files
If hFind <> 0 Then
'Get first file name
fName = (Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1))
'Loop through the rest
Do
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the next file
lRet = InternetFindNextFile(hFind, pData)
'if there's no next file, exit do
If lRet = 0 Then Exit Do
fCount = fCount + 1
'show the filename
fName = (Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1))
Loop
End If
'This Msgbox comes up with the correct dir name, but says each one has 0 files.
MsgBox "Files for " & dirArray(i) & ": " & fCount & vbCrLf & fName
Next i
'close the search handle
InternetCloseHandle hFind
|
|

09-29-2003, 09:43 AM
|
 |
Algorithms 'R' Us
Forum Leader * Guru *
|
|
Join Date: Jun 2002
Location: Canberra
Posts: 4,123
|
|
Code:
If FtpSetCurrentDirectory(hConnection, "/" & dirArray(i)) = False Then
Because the directory you want is below the "current working directory", the relative directory path will be
Code:
If FtpSetCurrentDirectory(hConnection, "./" & dirArray(i)) = False Then
You can leave out the "./" - if you just give the dirname (without a leading /) it's assumed relative to current dir anyway ...
Code:
If FtpSetCurrentDirectory(hConnection, dirArray(i)) = False Then
|
__________________
Sept, 2006: 2 ^ 232,582,657 - 1 is prime!
At first, I was iridescent. Then, I became transparent. Finally, I was absent.
Last edited by MathImagics; 09-29-2003 at 10:10 AM.
|

09-29-2003, 10:30 AM
|
 |
Algorithms 'R' Us
Forum Leader * Guru *
|
|
Join Date: Jun 2002
Location: Canberra
Posts: 4,123
|
|
I have just noticed you have another problem. You can't search a sub-directory while your current search handle is active.
The usual approach when a fully-recursive directory scan is required is as follows:
1. The routine deals with files as required, but builds a separate list of sub-dirs
2. When the current directory has been scanned, close the hFind handle, then call the scan recursively on the sub-directory.
This requires a simple mechanism to keep track of the current directory path. Here's a typical example of a recursive scan (avoiding any calls to SetCurrentDirectory):
Code:
Sub FTPscan(Optional ByVal sDir$ = "")
Static DLEVEL%
Dim dirList$, dirName$()
hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
If hFind = 0 Then
' ** No files found **
Exit Sub
End If
Do
With pData
ln = InStr(pData.cFileName, Chr(0)) - 1
ItemName = Left(pData.cFileName, ln)
If (.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <> 0 Then
' sub-Dir, add it to list
dirList = dirList & "," & ItemName
Else
' Itemname is a file - report it, list it, whatever
End If
End With
Loop Until InternetFindNextFile(hFind, pData) = 0
InternetCloseHandle hFind
' now deal with sub-dirs
Dim d%
dirName = Split(dirList, ",")
For d = 1 To UBound(dirName)
ItemName = dirName(d)
If DLEVEL > 0 Then Itemname = sDir & "/" & ItemName
DLEVEL = DLEVEL + 1
FTPscan ItemName
DLEVEL = DLEVEL - 1
Next
Exit Sub
So, with a single call i.e. FTPscan you will scan the entire current working directory
|
__________________
Sept, 2006: 2 ^ 232,582,657 - 1 is prime!
At first, I was iridescent. Then, I became transparent. Finally, I was absent.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|