2010-07-02 40 views

Trả lời

4

Nếu bạn chỉ cần hoàn thành công việc, thì SmartFTP có thể giúp bạn, it also has a PHP and ASP script để nhận được tổng kích thước thư mục bằng cách đệ quy đi qua tất cả các tệp.

+0

chức năng smartftp rất mát mẻ, nhờ –

+1

WinSCP thực hiện điều này cũng như – aaiezza

2

Bạn có thể gửi lệnh LIST sẽ cung cấp cho bạn danh sách tệp trong thư mục và một số thông tin về chúng (chắc chắn kích thước được bao gồm), sau đó bạn có thể phân tích và thêm.

Phụ thuộc vào cách bạn kết nối với máy chủ, nhưng nếu bạn đang sử dụng lớp WebRequest.Ftp, thì có phương pháp ListDirectoryDetails để thực hiện việc này. Xem here để biết chi tiết và here đối với một số mã mẫu. Chỉ cần lưu ý, nếu bạn muốn có tổng kích thước bao gồm tất cả các thư mục con, tôi nghĩ bạn sẽ phải nhập từng thư mục con và gọi nó là đệ quy để nó có thể khá chậm. Nó có thể được suy nghĩ khá chậm vì vậy bình thường tôi đề nghị, nếu có thể, để có một kịch bản trên máy chủ tính toán kích thước và trả về kết quả theo một cách nào đó (có thể lưu trữ nó trong một tập tin bạn có thể tải về và đọc).

Chỉnh sửa: Hoặc nếu bạn chỉ có nghĩa là bạn sẽ hài lòng với một công cụ thực hiện nó cho bạn, tôi nghĩ FlashFXP sẽ làm điều đó và có lẽ các ứng dụng khách FTP nâng cao khác cũng sẽ làm như vậy. Hoặc nếu đó là một máy chủ Unix tôi có một bộ nhớ mơ hồ mà bạn chỉ có thể đăng nhập và nhập ls -laR hoặc một cái gì đó để có được một danh sách thư mục đệ quy.

-2

Chỉ cần sử dụng FTP "SIZE" lệnh ...

73

Nếu bạn có FileZilla, bạn có thể sử dụng thủ thuật này:

  • nhấp chuột phải vào thư mục (s) có kích thước bạn muốn tính
  • nhấp vào Add files to queue

Thao tác này sẽ quét tất cả thư mục và tệp và thêm chúng vào hàng đợi. Sau đó nhìn vào ô xếp hàng và bên dưới nó (trên thanh trạng thái), bạn sẽ thấy một thông báo cho biết kích thước hàng đợi.

+3

Giải pháp tốt. Bằng cách này, bạn có thể nhận tổng số tệp và tổng kích thước mà không cần tải xuống bất kỳ tệp nào. [Lệnh FTP] (http://en.wikipedia.org/wiki/List_of_FTP_commands) được Filezilla sử dụng là MLSD (đệ quy) nếu có ai tò mò về nó. –

+3

Lưu ý rằng bạn cần phải có một đích hợp lệ được chọn trong ngăn cây thư mục cục bộ (nếu không thì các tập tin _Add vào hàng đợi sẽ bị chuyển sang màu xám). – jbaums

1

Tôi sử dụng FTPS library từ Alex Pilotti với C# để thực thi một số lệnh FTP trong một số môi trường sản xuất. Thư viện hoạt động tốt, nhưng bạn phải đệ quy lấy danh sách các tệp trong thư mục và thêm kích thước của chúng lại với nhau để có kết quả. Điều này có thể mất một chút thời gian trên một số máy chủ lớn hơn của chúng tôi (đôi khi 1-2 phút) với cấu trúc tệp phức tạp.

Dù sao, đây là phương pháp tôi sử dụng với thư viện của mình:

/// <summary> 
/// <para>This will get the size for a directory</para> 
/// <para>Can be lengthy to complete on complex folder structures</para> 
/// </summary> 
/// <param name="pathToDirectory">The path to the remote directory</param> 
public ulong GetDirectorySize(string pathToDirectory) 
{ 
    try 
    { 
     var client = Settings.Variables.FtpClient; 
     ulong size = 0; 

     if (!IsConnected) 
      return 0; 

     var dirList = client.GetDirectoryList(pathToDirectory); 
     foreach (var item in dirList) 
     { 
      if (item.IsDirectory) 
       size += GetDirectorySize(string.Format("{0}/{1}", pathToDirectory, item.Name)); 
      else 
       size += item.Size; 
     } 
     return size; 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
    return 0; 
} 
13

Bạn có thể sử dụng lệnh du trong lftp cho mục đích này, như thế này:

echo "du -hs ." | lftp example.com 2>&1 

này sẽ in dòng điện kích thước đĩa của thư mục bao gồm. tất cả các thư mục con, ở định dạng có thể đọc được (-h) và bỏ qua các dòng đầu ra cho các thư mục con (-s). đầu ra stderr được định tuyến lại để stdout với 2>&1 để nó được bao gồm trong đầu ra.

Tuy nhiên, lftp là một phần mềm chỉ dành cho Linux, do đó, để sử dụng phần mềm này từ C#, bạn cần phải sử dụng phần mềm này trong phạm vi Cygwin.

Tài liệu lệnh lftp du bị thiếu từ its manpage, nhưng có sẵn trong trình bao lftp bằng lệnh help du. Để tham khảo, tôi sao chép kết quả của nó tại đây:

lftp :~> help du 
Usage: du [options] <dirs> 
Summarize disk usage. 
-a, --all    write counts for all files, not just directories 
    --block-size=SIZ use SIZ-byte blocks 
-b, --bytes   print size in bytes 
-c, --total   produce a grand total 
-d, --max-depth=N  print the total for a directory (or file, with --all) 
         only if it is N or fewer levels below the command 
         line argument; --max-depth=0 is the same as 
         --summarize 
-F, --files   print number of files instead of sizes 
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) 
-H, --si    likewise, but use powers of 1000 not 1024 
-k, --kilobytes  like --block-size=1024 
-m, --megabytes  like --block-size=1048576 
-S, --separate-dirs do not include size of subdirectories 
-s, --summarize  display only a total for each argument 
    --exclude=PAT  exclude files that match PAT 
+2

Để xác thực echo "du -hs." | lftp -u user, password hostname 2> & 1 –

+0

thay thế cho cygwin là msys2 –

+1

Bây giờ bạn có thể sử dụng Windows Subsystem cho Linux (WSL) nếu bạn đang sử dụng Windows 10 –

0

Cách đơn giản và hiệu quả để nhận kích thước thư mục FTP với tất cả nội dung đệ quy.

var size = FTP.GetFtpDirectorySize ("ftpURL", "userName", "password");

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Net; 
using System.Threading; 
using System.Threading.Tasks; 

public static class FTP 
{ 
    public static long GetFtpDirectorySize(Uri requestUri, NetworkCredential networkCredential, bool recursive = true) 
    { 
     //Get files/directories contained in CURRENT directory. 
     var directoryContents = GetFtpDirectoryContents(requestUri, networkCredential); 

     long ftpDirectorySize = default(long); //Set initial value of the size to default: 0 
     var subDirectoriesList = new List<Uri>(); //Create empty list to fill it later with new founded directories. 

     //Loop on every file/directory founded in CURRENT directory. 
     foreach (var item in directoryContents) 
     { 
      //Combine item path with CURRENT directory path. 
      var itemUri = new Uri(Path.Combine(requestUri.AbsoluteUri + "\\", item)); 
      var fileSize = GetFtpFileSize(itemUri, networkCredential); //Get item file size. 

      if (fileSize == default(long)) //This means it has no size so it's a directory and NOT a file. 
       subDirectoriesList.Add(itemUri); //Add this item Uri to subDirectories to get it's size later. 
      else //This means it has size so it's a file. 
       Interlocked.Add(ref ftpDirectorySize, fileSize); //Add file size to overall directory size. 
     } 

     if (recursive) //If recursive true: it'll get size of subDirectories files. 
      Parallel.ForEach(subDirectoriesList, (subDirectory) => //Loop on every directory 
      //Get size of selected directory and add it to overall directory size. 
     Interlocked.Add(ref ftpDirectorySize, GetFtpDirectorySize(subDirectory, networkCredential, recursive))); 

     return ftpDirectorySize; //returns overall directory size. 
    } 
    public static long GetFtpDirectorySize(string requestUriString, string userName, string password, bool recursive = true) 
    { 
     //Initialize Uri/NetworkCredential objects and call the other method to centralize the code 
     return GetFtpDirectorySize(new Uri(requestUriString), GetNetworkCredential(userName, password), recursive); 
    } 

    public static long GetFtpFileSize(Uri requestUri, NetworkCredential networkCredential) 
    { 
     //Create ftpWebRequest object with given options to get the File Size. 
     var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods.Ftp.GetFileSize); 

     try { return ((FtpWebResponse)ftpWebRequest.GetResponse()).ContentLength; } //Incase of success it'll return the File Size. 
     catch (Exception) { return default(long); } //Incase of fail it'll return default value to check it later. 
    } 
    public static List<string> GetFtpDirectoryContents(Uri requestUri, NetworkCredential networkCredential) 
    { 
     var directoryContents = new List<string>(); //Create empty list to fill it later. 
                //Create ftpWebRequest object with given options to get the Directory Contents. 
     var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods.Ftp.ListDirectory); 
     try 
     { 
      using (var ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse()) //Excute the ftpWebRequest and Get It's Response. 
      using (var streamReader = new StreamReader(ftpWebResponse.GetResponseStream())) //Get list of the Directory Contentss as Stream. 
      { 
       var line = string.Empty; //Initial default value for line. 
       do 
       { 
        line = streamReader.ReadLine(); //Read current line of Stream. 
        directoryContents.Add(line); //Add current line to Directory Contentss List. 
       } while (!string.IsNullOrEmpty(line)); //Keep reading while the line has value. 
      } 
     } 
     catch (Exception) { } //Do nothing incase of Exception occurred. 
     return directoryContents; //Return all list of Directory Contentss: Files/Sub Directories. 
    } 
    public static FtpWebRequest GetFtpWebRequest(Uri requestUri, NetworkCredential networkCredential, string method = null) 
    { 
     var ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri); //Create FtpWebRequest with given Request Uri. 
     ftpWebRequest.Credentials = networkCredential; //Set the Credentials of current FtpWebRequest. 

     if (!string.IsNullOrEmpty(method)) 
      ftpWebRequest.Method = method; //Set the Method of FtpWebRequest incase it has a value. 
     return ftpWebRequest; //Return the configured FtpWebRequest. 
    } 
    public static NetworkCredential GetNetworkCredential(string userName, string password) 
    { 
     //Create and Return NetworkCredential object with given UserName and Password. 
     return new NetworkCredential(userName, password); 
    } 
} 
Các vấn đề liên quan