找回密码
 加入
搜索
查看: 11630|回复: 15

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

 火.. [复制链接]
发表于 2009-3-17 09:11:12 | 显示全部楼层 |阅读模式
请问: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[1];
                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), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]"))
                {
                    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[0] == '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)[2];
            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 编辑 ]
 楼主| 发表于 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 - 0  and 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] = 0 Then
                SetError(-1)
                Return 0
        EndIf
               
        Return $ai_InternetOpen[0]
       
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 - 0  and 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] = 0 Then
                SetError(-1)
                Return 0
        EndIf
                       
        Return $ai_InternetConnect[0]
       
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] = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_FTPPutFile[0]
       
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] = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_FTPPutFile[0]
       
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] = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_FTPRenameFile[0]
       
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] = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_FTPMakeDir[0]
       
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] = 0 Then
                SetError(-1)
                Return 0
        EndIf
               
        Return $ai_FTPDelDir[0]
       
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] = 0 Then
                SetError(-1)
                Return 0
        EndIf
       
        Return $ai_InternetCloseHandle[0]
       
EndFunc ;==> _FTPClose()
发表于 2009-3-17 10:01:11 | 显示全部楼层
只要你有权限,完全可以。 获取大小用FtpGetFileSize, 获取时间用FtpFindFirstFile和InternetFindNextFile, 只是这几个函数都不在FTP.AU3,得自己写~

[ 本帖最后由 pusofalse 于 2009-3-17 10:04 编辑 ]
 楼主| 发表于 2009-3-17 12:22:27 | 显示全部楼层
原帖由 pusofalse 于 2009-3-17 10:01 发表
只要你有权限,完全可以。 获取大小用FtpGetFileSize, 获取时间用FtpFindFirstFile和InternetFindNextFile, 只是这几个函数都不在FTP.AU3,得自己写~


------就是需要现成的FTP_NEW.AU3嘛,谁提供一下呢?:)
 楼主| 发表于 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[0])
    If @error OR $ai_FTPGetFileSize[0] = 0 Then
        SetError(-1)
        Return 0
    EndIf
    DllCall('wininet.dll', 'int', 'InternetCloseHandle', 'str', $ai_FTPGetSizeHandle[0])

    Return $ai_FTPGetFileSize[0]
   
EndFunc ;==> _FTPGetFileSize()

呵呵,搞定!
发表于 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[2];uint[2];uint[2];int;int;int;int;char[256];char[14]"
        $l_DllStruct = DllStructCreate($str)
        If @error Then
        SetError(-2)
        Return ""
        EndIf
        Dim $a_FTPFileList[1]
        $a_FTPFileList[0] = 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[0]&",error:"&@error)
        ;_ArrayDisplay($ai_FTPPutFile)
        If @error Or $ai_FTPPutFile[0] = 0 Then
        SetError(-1)
        Return $a_FTPFileList
        EndIf
        $h_Handle = $ai_FTPPutFile[0]
        $FileName = DllStructGetData($l_DllStruct, 9)
        Dim $a_FTPFileList[12]
        $a_FTPFileList[0] = 12
        $a_FTPFileList[1] = DllStructGetData($l_DllStruct, 1) ; File Attributes
        $a_FTPFileList[2] = DllStructGetData($l_DllStruct, 2, 1) ; Creation Time Low
        $a_FTPFileList[3] = DllStructGetData($l_DllStruct, 2, 2) ; Creation Time High
        $a_FTPFileList[4] = DllStructGetData($l_DllStruct, 3, 1) ; Access Time Low
        $a_FTPFileList[5] = DllStructGetData($l_DllStruct, 3, 2) ; Access Time High
        $a_FTPFileList[6] = DllStructGetData($l_DllStruct, 4, 1) ; Last Write Low
        $a_FTPFileList[7] = DllStructGetData($l_DllStruct, 4, 2) ; Last Write High
        $a_FTPFileList[8] = DllStructGetData($l_DllStruct, 5) ; File Size High
        $a_FTPFileList[9] = DllStructGetData($l_DllStruct, 6) ; File Size Low
        $a_FTPFileList[10] = DllStructGetData($l_DllStruct, 9); File Name
        $a_FTPFileList[11] = DllStructGetData($l_DllStruct, 10) ; Altername
        Return $a_FTPFileList
EndFunc ;==>_FTPFileFindFirst
发表于 2010-7-2 09:00:46 | 显示全部楼层
好东西。 谢谢分享。我已经收藏了。~~呵
发表于 2012-4-23 11:31:11 | 显示全部楼层
好东西。 谢谢分享。我已经收藏了。~~呵
发表于 2012-5-11 14:53:50 | 显示全部楼层
之前也坐过一个,谢谢分享,收了
发表于 2012-6-30 11:41:58 | 显示全部楼层
惭愧!哪位告诉我一下,这两个函数怎么调用啊?_FTPFileFindFirst()里面参数怎么写?
发表于 2014-8-20 14:19:18 | 显示全部楼层
不错的自定义函数,收藏,留着以后用,感谢分享,感谢楼主
发表于 2014-8-22 08:40:48 | 显示全部楼层
有用收下了
发表于 2014-10-20 16:13:44 | 显示全部楼层
暂时没用到,先收藏,LZ辛苦了。
发表于 2017-1-15 17:45:56 | 显示全部楼层
论坛中果然大神比较多,ftp的UDF在这方面比较空白,谢谢P版提示。
发表于 2017-2-27 12:36:01 | 显示全部楼层
如何获取静态网站上的文件(压缩包)修改时间,请指教
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-20 00:49 , Processed in 0.079102 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表