Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > API > FTP not setting current directory


Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2003, 07:59 AM
kkonkle kkonkle is offline
Centurion
 
Join Date: Mar 2002
Location: Michigan
Posts: 186
Default 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
Reply With Quote
  #2  
Old 09-29-2003, 09:43 AM
Mathimagics's Avatar
Mathimagics Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,123
Default

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.
Reply With Quote
  #3  
Old 09-29-2003, 10:30 AM
Mathimagics's Avatar
Mathimagics Mathimagics is offline
Algorithms 'R' Us

Forum Leader
* Guru *
 
Join Date: Jun 2002
Location: Canberra
Posts: 4,123
Default

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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to select a file from FTP directory lofrank General 4 07-02-2003 04:41 PM
need help with setting directory and saving itzyouknowwho General 1 06-23-2003 12:00 AM
Setting default directory mikenowo General 11 01-17-2003 05:14 PM
Current directory kimjo Database and Reporting 8 08-21-2002 03:07 PM
How do you "Get" and "Set" Current Directory? CornMaster General 2 12-13-2000 10:13 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->