menfan 发表于 2009-3-17 09:11:12

AU3如何取出ftp服务器上的文件大小和最后修改时间?

请问:AU3如何取出ftp服务器上的文件大小和最后修改时间等?

通过c#实现如下:
在你的FTP类里要声明下文件信息

    #region 文件信息结构
    public struct FileStruct
    {
      public string Flags;
      public string Owner;
      public string Group;
      public bool IsDirectory;
      public DateTime CreateTime;
      public string Name;
    }
    public enum FileListStyle
    {
      UnixStyle,
      WindowsStyle,
      Unknown
    }
    #endregion

然后获取写一些方法
      #region 列出目录文件信息
      /// <summary>
      /// 列出FTP服务器上面当前目录的所有文件和目录
      /// </summary>
      public FileStruct[] ListFilesAndDirectories()
      {
            Response = Open(this.Uri, WebRequestMethods.Ftp.ListDirectoryDetails);
            StreamReader stream = new StreamReader(Response.GetResponseStream(), Encoding.Default);
            string Datastring = stream.ReadToEnd();
            FileStruct[] list = GetList(Datastring);
            return list;
      }
      /// <summary>
      /// 列出FTP服务器上面当前目录的所有文件
      /// </summary>
      public FileStruct[] ListFiles()
      {
            FileStruct[] listAll = ListFilesAndDirectories();
            List <FileStruct> listFile = new List <FileStruct>();
            foreach (FileStruct file in listAll)
            {
                if (!file.IsDirectory)
                {
                  listFile.Add(file);
                }
            }
            return listFile.ToArray();
      }

      /// <summary>
      /// 列出FTP服务器上面当前目录的所有的目录
      /// </summary>
      public FileStruct[] ListDirectories()
      {
            FileStruct[] listAll = ListFilesAndDirectories();
            List <FileStruct> listDirectory = new List <FileStruct>();
            foreach (FileStruct file in listAll)
            {
                if (file.IsDirectory)
                {
                  listDirectory.Add(file);
                }
            }
            return listDirectory.ToArray();
      }
      /// <summary>
      /// 获得文件和目录列表
      /// </summary>
      /// <param name="datastring">FTP返回的列表字符信息 </param>
      private FileStruct[] GetList(string datastring)
      {
            List <FileStruct> myListArray = new List <FileStruct>();
            string[] dataRecords = datastring.Split('\n');
            FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);
            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                  FileStruct f = new FileStruct();
                  f.Name = "..";
                  switch (_directoryListStyle)
                  {
                  case FileListStyle.UnixStyle:
                        f = ParseFileStructFromUnixStyleRecord(s);
                        break;
                  case FileListStyle.WindowsStyle:
                        f = ParseFileStructFromWindowsStyleRecord(s);
                        break;
                  }
                  if (!(f.Name == "." | | f.Name == ".."))
                  {
                        myListArray.Add(f);
                  }
                }
            }
            return myListArray.ToArray();
      }
      
      /// <summary>
      /// 从Windows格式中返回文件信息
      /// </summary>
      /// <param name="Record">文件信息 </param>
      private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
      {
            FileStruct f = new FileStruct();
            string processstr = Record.Trim();
            string dateStr = processstr.Substring(0, 8);
            processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();
            string timeStr = processstr.Substring(0, 7);
            processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();
            DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat;
            myDTFI.ShortTimePattern = "t";
            f.CreateTime = DateTime.Parse(dateStr + " " + timeStr, myDTFI);
            if (processstr.Substring(0, 5) == " <DIR>")
            {
                f.IsDirectory = true;
                processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
            }
            else
            {
                string[] strs = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);// true);
                processstr = strs;
                f.IsDirectory = false;
            }
            f.Name = processstr;
            return f;
      }


      /// <summary>
      /// 判断文件列表的方式Window方式还是Unix方式
      /// </summary>
      /// <param name="recordList">文件信息列表 </param>
      private FileListStyle GuessFileListStyle(string[] recordList)
      {
            foreach (string s in recordList)
            {
                if (s.Length > 10
                && Regex.IsMatch(s.Substring(0, 10), "(- |d)(- |r)(- |w)(- |x)(- |r)(- |w)(- |x)(- |r)(- |w)(- |x)"))
                {
                  return FileListStyle.UnixStyle;
                }
                else if (s.Length > 8
                && Regex.IsMatch(s.Substring(0, 8), "--"))
                {
                  return FileListStyle.WindowsStyle;
                }
            }
            return FileListStyle.Unknown;
      }

      /// <summary>
      /// 从Unix格式中返回文件信息
      /// </summary>
      /// <param name="Record">文件信息 </param>
      private FileStruct ParseFileStructFromUnixStyleRecord(string Record)
      {
            FileStruct f = new FileStruct();
            string processstr = Record.Trim();
            f.Flags = processstr.Substring(0, 10);
            f.IsDirectory = (f.Flags == 'd');
            processstr = (processstr.Substring(11)).Trim();
            _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);//跳过一部分
            f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
            f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
            _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);//跳过一部分
            string yearOrTime = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            if (yearOrTime.IndexOf(":") >= 0)//time
            {
                processstr = processstr.Replace(yearOrTime, DateTime.Now.Year.ToString());
            }
            f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8));
            f.Name = processstr;//最后就是名称
            return f;
      }

      /// <summary>
      /// 按照一定的规则进行字符串截取
      /// </summary>
      /// <param name="s">截取的字符串 </param>
      /// <param name="c">查找的字符 </param>
      /// <param name="startIndex">查找的位置 </param>
      private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex)
      {
            int pos1 = s.IndexOf(c, startIndex);
            string retString = s.Substring(0, pos1);
            s = (s.Substring(pos1)).Trim();
            return retString;
      }
      #endregion

[ 本帖最后由 menfan 于 2009-3-17 21:07 编辑 ]

menfan 发表于 2009-3-17 09:12:59

ftp.au3似乎实现不了该功能,代码如下:

;===============================================================================
;
; Function Name:    _FTPOpen()
; Description:      Opens an FTP session.
; Parameter(s):   $s_Agent              - Random name. ( like "myftp" )
;                   $l_AccessType         - I dont got a clue what this does.
;                   $s_ProxyName        - ProxyName.
;                   $s_ProxyBypass        - ProxyByPasses's.
;                   $l_Flags               - Special flags.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):On Success - Returns an indentifier.
;                   On Failure - 0and sets @ERROR
; Author(s):      Wouter van Kesteren.
;
;===============================================================================

Func _FTPOpen($s_Agent, $l_AccessType = 1, $s_ProxyName = '', $s_ProxyBypass = '', $l_Flags = 0)
       
        Local $ai_InternetOpen = DllCall('wininet.dll', 'long', 'InternetOpen', 'str', $s_Agent, 'long', $l_AccessType, 'str', $s_ProxyName, 'str', $s_ProxyBypass, 'long', $l_Flags)
        If @error OR $ai_InternetOpen = 0 Then
                SetError(-1)
                Return 0
        EndIf
               
        Return $ai_InternetOpen
       
EndFunc ;==> _FTPOpen()

;===============================================================================
;
; Function Name:    _FTPConnect()
; Description:      Connects to an FTP server.
; Parameter(s):   $l_InternetSession        - The Long from _FTPOpen()
;                   $s_ServerName                 - Server name/ip.
;                   $s_Username                - Username.
;                   $s_Password                        - Password.
;                   $i_ServerPort                - Server port ( 0 is default (21) )
;                                        $l_Service                        - I dont got a clue what this does.
;                                        $l_Flags                        - Special flags.
;                                        $l_Context                        - I dont got a clue what this does.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):On Success - Returns an indentifier.
;                   On Failure - 0and sets @ERROR
; Author(s):      Wouter van Kesteren
;
;===============================================================================

Func _FTPConnect($l_InternetSession, $s_ServerName, $s_Username, $s_Password, $i_ServerPort = 0, $l_Service = 1, $l_Flags = 0, $l_Context = 0)
       
        Local $ai_InternetConnect = DllCall('wininet.dll', 'long', 'InternetConnect', 'long', $l_InternetSession, 'str', $s_ServerName, 'int', $i_ServerPort, 'str', $s_Username, 'str', $s_Password, 'long', $l_Service, 'long', $l_Flags, 'long', $l_Context)
        If @error OR $ai_InternetConnect = 0 Then
                SetError(-1)
                Return 0
        EndIf
                       
        Return $ai_InternetConnect
       
EndFunc ;==> _FTPConnect()

;===============================================================================
;
; Function Name:    _FTPPutFile()
; Description:      Puts an file on an FTP server.
; Parameter(s):   $l_FTPSession        - The Long from _FTPConnect()
;                   $s_LocalFile         - The local file.
;                   $s_RemoteFile        - The remote Location for the file.
;                   $l_Flags                - Special flags.
;                   $l_Context        - I dont got a clue what this does.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):On Success - 1
;                   On Failure - 0
; Author(s):      Wouter van Kesteren
;
;===============================================================================

Func _FTPPutFile($l_FTPSession, $s_LocalFile, $s_RemoteFile, $l_Flags = 0, $l_Context = 0)

        Local $ai_FTPPutFile = DllCall('wininet.dll', 'int', 'FtpPutFile', 'long', $l_FTPSession, 'str', $s_LocalFile, 'str', $s_RemoteFile, 'long', $l_Flags, 'long', $l_Context)
        If @error OR $ai_FTPPutFile = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_FTPPutFile
       
EndFunc ;==> _FTPPutFile()

;===============================================================================
;
; Function Name:    _FTPDelFile()
; Description:      Delete an file from an FTP server.
; Parameter(s):   $l_FTPSession        - The Long from _FTPConnect()
;                   $s_RemoteFile        - The remote Location for the file.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):On Success - 1
;                   On Failure - 0
; Author(s):      Wouter van Kesteren
;
;===============================================================================

Func _FTPDelFile($l_FTPSession, $s_RemoteFile)
       
        Local $ai_FTPPutFile = DllCall('wininet.dll', 'int', 'FtpDeleteFile', 'long', $l_FTPSession, 'str', $s_RemoteFile)
        If @error OR $ai_FTPPutFile = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_FTPPutFile
       
EndFunc ;==> _FTPDelFile()

;===============================================================================
;
; Function Name:    _FTPRenameFile()
; Description:      Renames an file on an FTP server.
; Parameter(s):   $l_FTPSession        - The Long from _FTPConnect()
;                   $s_Existing         - The old file name.
;                   $s_New                - The new file name.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):On Success - 1
;                   On Failure - 0
; Author(s):      Wouter van Kesteren
;
;===============================================================================

Func _FTPRenameFile($l_FTPSession, $s_Existing, $s_New)
       
        Local $ai_FTPRenameFile = DllCall('wininet.dll', 'int', 'FtpRenameFile', 'long', $l_FTPSession, 'str', $s_Existing, 'str', $s_New)
        If @error OR $ai_FTPRenameFile = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_FTPRenameFile
       
EndFunc ;==> _FTPRenameFile()

;===============================================================================
;
; Function Name:    _FTPMakeDir()
; Description:      Makes an Directory on an FTP server.
; Parameter(s):   $l_FTPSession        - The Long from _FTPConnect()
;                   $s_Remote                 - The file name to be deleted.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):On Success - 1
;                   On Failure - 0
; Author(s):      Wouter van Kesteren
;
;===============================================================================

Func _FTPMakeDir($l_FTPSession, $s_Remote)
       
        Local $ai_FTPMakeDir = DllCall('wininet.dll', 'int', 'FtpCreateDirectory', 'long', $l_FTPSession, 'str', $s_Remote)
        If @error OR $ai_FTPMakeDir = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_FTPMakeDir
       
EndFunc ;==> _FTPMakeDir()

;===============================================================================
;
; Function Name:    _FTPDelDir()
; Description:      Delete's an Directory on an FTP server.
; Parameter(s):   $l_FTPSession        - The Long from _FTPConnect()
;                   $s_Remote                 - The Directory to be deleted.
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):On Success - 1
;                   On Failure - 0
; Author(s):      Wouter van Kesteren
;
;===============================================================================

Func _FTPDelDir($l_FTPSession, $s_Remote)
       
        Local $ai_FTPDelDir = DllCall('wininet.dll', 'int', 'FtpRemoveDirectory', 'long', $l_FTPSession, 'str', $s_Remote)
        If @error OR $ai_FTPDelDir = 0 Then
                SetError(-1)
                Return 0
        EndIf
               
        Return $ai_FTPDelDir
       
EndFunc ;==> _FTPDelDir()

;===============================================================================
;
; Function Name:    _FTPClose()
; Description:      Closes the _FTPOpen session.
; Parameter(s):   $l_InternetSession        - The Long from _FTPOpen()
; Requirement(s):   DllCall, wininet.dll
; Return Value(s):On Success - 1
;                   On Failure - 0
; Author(s):      Wouter van Kesteren
;
;===============================================================================

Func _FTPClose($l_InternetSession)
       
        Local $ai_InternetCloseHandle = DllCall('wininet.dll', 'int', 'InternetCloseHandle', 'long', $l_InternetSession)
        If @error OR $ai_InternetCloseHandle = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_InternetCloseHandle
       
EndFunc ;==> _FTPClose()

pusofalse 发表于 2009-3-17 10:01:11

只要你有权限,完全可以。 获取大小用FtpGetFileSize, 获取时间用FtpFindFirstFile和InternetFindNextFile, 只是这几个函数都不在FTP.AU3,得自己写~

[ 本帖最后由 pusofalse 于 2009-3-17 10:04 编辑 ]

menfan 发表于 2009-3-17 12:22:27

原帖由 pusofalse 于 2009-3-17 10:01 发表 http://www.autoitx.com/images/common/back.gif
只要你有权限,完全可以。 获取大小用FtpGetFileSize, 获取时间用FtpFindFirstFile和InternetFindNextFile, 只是这几个函数都不在FTP.AU3,得自己写~

------就是需要现成的FTP_NEW.AU3嘛,谁提供一下呢?:)

menfan 发表于 2009-3-17 21:08:11

Func _FTPGetFileSize($l_FTPSession, $s_FileName)

    Local $ai_FTPGetSizeHandle = DllCall('wininet.dll', 'int', 'FtpOpenFile', 'long', $l_FTPSession, 'str', $s_FileName, 'long', 0x80000000, 'long', 0x04000002, 'long', 0)
    Local $ai_FTPGetFileSize = DllCall('wininet.dll', 'int', 'FtpGetFileSize', 'long', $ai_FTPGetSizeHandle)
    If @error OR $ai_FTPGetFileSize = 0 Then
      SetError(-1)
      Return 0
    EndIf
    DllCall('wininet.dll', 'int', 'InternetCloseHandle', 'str', $ai_FTPGetSizeHandle)

    Return $ai_FTPGetFileSize
   
EndFunc ;==> _FTPGetFileSize()

呵呵,搞定!

huivip 发表于 2009-12-4 12:41:57

Func _FTPFileFindFirst($l_FTPSession, $s_RemoteFile, ByRef $h_Handle, ByRef $l_DllStruct, $l_Flags = 0, $l_Context = 0)
        Local $str = "int;uint;uint;uint;int;int;int;int;char;char"
        $l_DllStruct = DllStructCreate($str)
        If @error Then
        SetError(-2)
        Return ""
        EndIf
        Dim $a_FTPFileList
        $a_FTPFileList = 0
        Local $ai_FTPPutFile = DllCall('wininet.dll', 'int', 'FtpFindFirstFile', 'long', $l_FTPSession, 'str', $s_RemoteFile, 'ptr', DllStructGetPtr($l_DllStruct), 'long', $l_Flags, 'long', $l_Context)
        ;Sleep(1000)
        ;ConsoleWrite("ftp file:"&$ai_FTPPutFile&",error:"&@error)
        ;_ArrayDisplay($ai_FTPPutFile)
        If @error Or $ai_FTPPutFile = 0 Then
        SetError(-1)
        Return $a_FTPFileList
        EndIf
        $h_Handle = $ai_FTPPutFile
        $FileName = DllStructGetData($l_DllStruct, 9)
        Dim $a_FTPFileList
        $a_FTPFileList = 12
        $a_FTPFileList = DllStructGetData($l_DllStruct, 1) ; File Attributes
        $a_FTPFileList = DllStructGetData($l_DllStruct, 2, 1) ; Creation Time Low
        $a_FTPFileList = DllStructGetData($l_DllStruct, 2, 2) ; Creation Time High
        $a_FTPFileList = DllStructGetData($l_DllStruct, 3, 1) ; Access Time Low
        $a_FTPFileList = DllStructGetData($l_DllStruct, 3, 2) ; Access Time High
        $a_FTPFileList = DllStructGetData($l_DllStruct, 4, 1) ; Last Write Low
        $a_FTPFileList = DllStructGetData($l_DllStruct, 4, 2) ; Last Write High
        $a_FTPFileList = DllStructGetData($l_DllStruct, 5) ; File Size High
        $a_FTPFileList = DllStructGetData($l_DllStruct, 6) ; File Size Low
        $a_FTPFileList = DllStructGetData($l_DllStruct, 9); File Name
        $a_FTPFileList = DllStructGetData($l_DllStruct, 10) ; Altername
        Return $a_FTPFileList
EndFunc ;==>_FTPFileFindFirst

chenronting 发表于 2010-7-2 09:00:46

好东西。 谢谢分享。我已经收藏了。~~呵

My2009 发表于 2012-4-23 11:31:11

好东西。 谢谢分享。我已经收藏了。~~呵

lilufb 发表于 2012-5-11 14:53:50

之前也坐过一个,谢谢分享,收了

給点阳光 发表于 2012-6-30 11:41:58

惭愧!哪位告诉我一下,这两个函数怎么调用啊?_FTPFileFindFirst()里面参数怎么写?

Yuan310 发表于 2014-8-20 14:19:18

不错的自定义函数,收藏,留着以后用,感谢分享,感谢楼主

lin6051 发表于 2014-8-22 08:40:48

有用收下了

今夜风真冷 发表于 2014-10-20 16:13:44

暂时没用到,先收藏,LZ辛苦了。{:face (303):}

jsdn2000 发表于 2017-1-15 17:45:56

论坛中果然大神比较多,ftp的UDF在这方面比较空白,谢谢P版提示。

xyx115 发表于 2017-2-27 12:36:01

如何获取静态网站上的文件(压缩包)修改时间,请指教
页: [1] 2
查看完整版本: AU3如何取出ftp服务器上的文件大小和最后修改时间?