FtpFindFirstFile 获取到的文件名有时带路径信息,为何?

zhangjiahuashibaichi 2018-03-01 10:08:06

Dim pData As WIN32_FIND_DATA
Dim lngHINet As Long
Dim strFile As String

pData.cFileName = String(260, 0)
lngHINet = FtpFindFirstFile(lnginetconn, "/DelEcc/conf/condpei.txt", pData, 0, 0)
strFile = Left(pData.cFileName, InStr(1, pData.cFileName, Chr(0)) - 1)
....


我得到strFile 为/DelEcc/conf/condpei.txt,按理说应该为condpei.txt,我换其他ftp服务器发现就可以得到不带路径信息的文件名,为何FtpFindFirstFile后有时得到文件名有时带路径信息?
...全文
591 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我发现获取linux目录下的点开头的文件或者点开头的目录时,FtpFindFirstFile(hConnection, "*.*", w32FindData, INTERNET_FLAG_RELOAD, 0) 和InternetFindNextFile 都找不到点开头的文件或者点开头的目录,这是为何? 如果我写死文件就可以获取到FtpFindFirstFile(hConnection, ".profile", w32FindData, INTERNET_FLAG_RELOAD, 0) ,难道*.*方式后去遍历不出来点开头的文件或者点开头的目录吗?

'======================================  
' 检索指定的路径  
'======================================  
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  


'===============================================  
' 从FTP中取得指定的目录下的内容  
'===============================================  
Private Sub Sample()  
  Dim hOpen As Long       'InternetServer的Handle  
  Dim hConnection As Long 'InternetSession的Handle  
  Dim result As Long  
  hOpen = 0  
  hConnection = 0  
  
  Dim hFind As Long  
  Dim w32FindData As WIN32_FIND_DATA  
  Dim strFile As String  
  
  Dim FileList() As String '文件名一栏  
  Dim cnt As Long  
  cnt = -1  
  
  '取得InternetServer的Handle - hOpen  
  hOpen = InternetOpen("FTPSample", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)  
  If (hOpen <> 0) Then 'Handle取得成功  
  
    '取得InternetSession的Handle(连接FTPServer) - hConnection  
    hConnection = InternetConnect(hOpen, "192.168.45.12", INTERNET_INVALID_PORT_NUMBER, _  
        "UserName", "Password", INTERNET_SERVICE_FTP, 0, 0)  
    If (hConnection <> 0) Then '连接成功  
  
      '改变FTPServer的CurrentDirectory  
      result = FtpSetCurrentDirectory(hConnection, "/home/monitor/")  
      If (result <> 0) Then '变更成功  
  
        '取得文件一览  
        hFind = FtpFindFirstFile(hConnection, "*.*", w32FindData, INTERNET_FLAG_RELOAD, 0)  
        If (hFind = 0) Then  
          MsgBox "文件名取得失败" & Err.LastDllError  
        Else  
          Do  
            strFile = Left(w32FindData.cFileName, InStr(w32FindData.cFileName, vbNullChar) - 1)  
            strFile = Mid(strFile, InStrRev(strFile, " ") + 1) '删除文件名中无用的字符  
            If ((w32FindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = &H10) Then  
              strFile = strFile & "/" '如果取得的是目录,在目录后面加上/  
            End If  
            cnt = cnt + 1  
            ReDim Preserve FileList(cnt)  
            FileList(cnt) = strFile '将取得的文件名或者目录名,追加到文件名一览中  
          Loop Until InternetFindNextFile(hFind, w32FindData) = 0 '取得下一个文件名  
        End If  
  
      Else  
        MsgBox "目录移动失败" & Err.LastDllError  
      End If  
    Else  
      MsgBox "FTPServer连接失败" & Err.LastDllError  
    End If  
  Else  
    MsgBox "FTPServer连接失败" & Err.LastDllError  
  End If  
  
  '关闭InternetSession  
  If (hConnection <> 0) Then InternetCloseHandle hConnection  
  
  '关闭InternetServer  
  If (hOpen <> 0) Then InternetCloseHandle hOpen  
  
End Sub  
海鸥软件 2018-03-05
  • 打赏
  • 举报
回复
这跟你传入 参数和对方服务器返回有关,FtpFindFirstFile(lnginetconn, "/DelEcc/conf/condpei.txt", pData, 0, 0),你仔细看一下这个函数每一个参数的意义,看有没有那个参数是带路径返回,还是不带的,如果参数没问题,你可以对返回的文件名,做一下是否带路径检查,再根据情况决定要不要路径,不要就分离路径和文件名
小噤 2018-03-03
  • 打赏
  • 举报
回复
可能和对方FTP服务器设置有关. 还是检测有没有"/" 有就处理一下较好
of123 2018-03-02
  • 打赏
  • 举报
回复
其实自己处理一下不是难事。 If InStr(strFile, "/") Then strFile = Mid(strFile, InStrConv(strFile, "/") + 1) End If
赵4老师 2018-03-02
  • 打赏
  • 举报
回复
百度搜相关关键字。

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧