-
November 2nd, 2001, 06:04 PM
#1
Registered User
wininet.dll function calls
I've been working on an FTP program for some time now and have run into a small block. The program needs to pull a directory listing off a UNIX server and store it on a local file. I need a complete listing that includes date/time. I have been using function calls to the wininet.dll file for my ftp connections and transfers. However, there is no direct function for the FTP dir command. I believe I need to use the FtpCommand function. Microsoft describes this function as "Allows an application to send commands directly to an FTP server." The syntax is:
BOOL FtpCommand( HINTERNET hConnect,
BOOL fExpectResponse,
DWORD dwFlags,
LPCSTR lpszCommand,
DWORD_PTR dwContext,
HINTERNET* phFtpCommand);
You can find MS info on it <a href="http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/wininet/reference/functions/ftpcommand.asp">here.</a>
I am able to connect to the FTP server and have been using my current code to transfer files and perform other common tasks. The problem is just with getting a directory from the server. My code so far looks like this:
Dim bRet As Boolean
Dim strCommand As String
Connect 'runs connection routine
strCommand = "dir " & strRemotePath & " " & strUNIXDirectory
bRet = FtpCommand(hConnection, False, FTP_TRANSFER_TYPE_BINARY, _
strCommand, 0, hConnection)
The strRemotePath and strUNIXDirectory are module variables that store the server's remote path and the local file for the UNIX directory respectively. Both of these variables are defined properly. The proper declare function was placed in a module specifically holding all wininet.dll variables and definitions. The FtpCommand is defined there as follows:
Public Const FTP_TRANSFER_TYPE_BINARY = &H1
Public Declare Function FtpCommand Lib "wininet.dll" Alias "FtpCommandA" _
(ByVal hFtpSession As Long, ByVal bExpectResponse As Boolean, ByVal dwFlags As Long, _
ByVal lpszCommand As String, ByVal dwContext As Long, _
ByVal hNewFtpSession As Long) As Boolean
When the program is executed, no error messages appear, but neither does the directory listing requested. Any help with this dilema will be greatly appreciated.
Thanks
A bored admin is a very dangerous person...
-
November 5th, 2001, 04:49 PM
#2
You can use the FtpGetCurrentDirectory call from the WinInet DLL which will return you a string with the current directory contents in it.
The Declare is:
[code]
Public Declare Function FtpGetCurrentDirectory Lib "wininet.dll" _
Alias "FtpGetCurrentDirectoryA" _
(ByVal hFtpSession As Long, _
ByVal lpszDirectory As String, _
ByRef lpdwCurrentDirectory As Long) _
As Boolean
</pre><hr></blockquote>
hFtpSession = handle to your current FTP session
lpszDirectory = return string
lpdwCurrentDirectory = return string length
So, you could wrap it up in your FTP class like this:
[code]
Public Property Get sCurDir() As String
' Returns the current server dir
Dim sDir As String
Dim lLenDir As Long
Dim bResult As Boolean
sDir = Space(255)
lLenDir = 255
bResult = FtpGetCurrentDirectory(mhFtpSession, sDir, lLenDir)
If bResult <> 0 Then
sCurDir = Left(sDir, lLenDir)
Else
Call RaiseAPIErr(Err.LastDllError)
End If
End Property
</pre><hr></blockquote>
I know this works 'cos it's straight out of my VB FTP DLL
HTH,
I'd rather die peacefully in my sleep like my Grandfather,
than screaming in terror like his passengers. Jim Harkins
<a href="http://www.Horrible.Demon.co.uk/" target="_blank">http://www.Horrible.Demon.co.uk/</a>
-
November 5th, 2001, 05:26 PM
#3
Registered User
Your code is great for retieving the name of the current directory, but what I'm trying to get is a detailed listing of the current directory. The command I am trying to pass would look like this(at a normal FTP prompt): dir /cumail/ipbatch c:\diretory.txt
This would create a file on the c drive called directory.txt and the contents would look like the following:
<font face=courier>
total 240
-rw-r----- 1 cumail R95 463 Aug 20 11:49 20010820_11_60
-rw-r----- 1 cumail R95 463 Aug 20 12:08 20010820_12_15
-rw-r----- 1 cumail R95 463 Aug 20 12:23 20010820_12_30
-rw-r----- 1 cumail R95 463 Aug 20 12:00 20010820_12_60
-rw-r----- 1 cumail R95 463 Aug 20 14:19 20010820_14_30
-rw-r----- 1 cumail R95 463 Aug 20 14:37 20010820_14_45
-rw-r----- 1 cumail R95 926 Aug 20 14:57 20010820_14_60
-rw-r----- 1 cumail R95 463 Aug 20 15:26 20010820_15_30
-rw-r----- 1 cumail R95 463 Aug 21 10:44 20010821_10_45
-rw-r----- 1 cumail R95 926 Aug 21 10:50 20010821_10_60
-rw-r----- 1 cumail R95 1389 Aug 21 11:04 20010821_11_15
-rw-r----- 1 cumail R95 1389 Aug 21 11:20 20010821_11_30
-rw-r----- 1 cumail R95 1389 Aug 21 11:52 20010821_11_60
-rw-r----- 1 cumail R95 1389 Aug 21 12:22 20010821_12_30
-rw-r----- 1 cumail R95 463 Aug 21 12:47 20010821_12_60
-rw-r----- 1 cumail R95 463 Aug 21 13:06 20010821_13_15
-rw-r----- 1 cumail R95 463 Aug 21 16:25 20010821_16_30
-rw-r----- 1 cumail R95 463 Aug 21 16:41 20010821_16_45
-rw-r----- 1 cumail R95 463 Aug 27 10:15 20010827_10_15
-rw-r----- 1 cumail R95 463 Aug 27 10:22 20010827_10_30
-rw-r----- 1 cumail R95 463 Aug 27 10:53 20010827_10_60
-rw-r----- 1 cumail R95 463 Aug 27 12:25 20010827_12_30
-rw-r----- 1 cumail R95 1389 Aug 27 12:54 20010827_12_60
-rw-r----- 1 cumail R95 926 Aug 27 14:24 20010827_14_30
-rw-r----- 1 cumail R95 463 Aug 27 14:00 20010827_14_60
-rw-r----- 1 cumail R95 926 Aug 27 15:30 20010827_15_30
-rw-r----- 1 cumail R95 463 Aug 27 15:31 20010827_15_45
-rw-r----- 1 cumail R95 1389 Aug 29 09:29 20010829_09_30
-rw-r----- 1 cumail R95 1389 Aug 29 09:45 20010829_09_45
-rwxrw-rw- 1 cumail R95 466 Sep 13 10:13 20010913_10_15
</font>
Thereby showing all details about the files within that directory. I need this list so that I may parse each line for the date/time stamp and the file name.
Here's a little more information...
Using the code mentioned above and using the Err.LastDllError I was able to construct the following error message:
11/05/2001 3:26:52 PM FtpCommand Extd Err: 0
500 'DIR /cumail/ipbatch C:\Directory.txt': command not understood.
One change was made to the original code:
bRet = FtpCommand(hConnection, False, FTP_TRANSFER_TYPE_BINARY, _
strCommand, 0, 0)
Again, thanks for your help antonye
A bored admin is a very dangerous person...
-
November 6th, 2001, 04:50 PM
#4
Ok, the reason you probably haven't sussed this yet is that you have to be a bit tricky in how you get a listing of the directory.
To do this, we use the FtpFindFirstFile and InternetFindNextFile functions:
[code]
Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _
(ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _
(ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
</pre><hr></blockquote>
This allows you, with an FTP handle through the WinInet DLL, locate the first file in the directory and then loop through the remaining files.
An example of how to do this is:
[code]
Dim hFileConnection As Long
Dim tFindData As WIN32_FIND_DATA
Dim bResult As Boolean
hFileConnection = FtpFindFirstFile(mhFtpSession, _
msPath, _
tFindData, _
0, _
0)
If hFileConnection <> 0 Then
' NOTE: we already have the first file at this point!
bResult = True
Do While bResult
If bResult Then
If tFindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
' it was a directory
[...code...]
Else
' it was a file
[...code...]
End If
End If
' get next file (if any)
bResult = InternetFindNextFile(hFileConnection, tFindData)
Loop
InternetCloseHandle (hFileConnection)
Else
' there may just be no files in this dir
If Err.LastDllError <> ERROR_NO_MORE_FILES Then
' it WAS an error!
Call RaiseAPIErr(Err.LastDllError)
End If
End If
</pre><hr></blockquote>
That should get you going!
HTH,
I'd rather die peacefully in my sleep like my Grandfather,
than screaming in terror like his passengers. Jim Harkins
<a href="http://www.Horrible.Demon.co.uk/" target="_blank">http://www.Horrible.Demon.co.uk/</a>
-
November 6th, 2001, 07:27 PM
#5
Registered User
Thanks so much antonye. I was looking in the wrong direction for the solution. I was so focused on the FtpCommand function that I didn't even think about the other functions available. I used the code you suggested with a few slight modifications and the project is back on track as it should be.
Thanks again for your help, I appreciate it as always.
A bored admin is a very dangerous person...
-
November 7th, 2001, 05:29 AM
#6
[quote]Originally posted by Deity:
Thanks again for your help, I appreciate it as always. <hr></blockquote>
No worries dude
I'd rather die peacefully in my sleep like my Grandfather,
than screaming in terror like his passengers. Jim Harkins
<a href="http://www.Horrible.Demon.co.uk/" target="_blank">http://www.Horrible.Demon.co.uk/</a>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks