如何計算FTP文件夾的大小?你知道C#中的任何工具或編程方式嗎?如何計算FTP上目錄的大小?
回答
如果您只需要完成所做的工作,那麼SmartFTP可能會幫助您,it also has a PHP and ASP script通過遞歸遍歷所有文件來獲取總文件夾大小。
你可以發送LIST
命令,它應該給你一個目錄中的文件列表和一些關於他們的信息(相當肯定的大小是包含的),然後你可以解析出來並加起來。
取決於您如何連接到服務器,但如果您使用WebRequest.Ftp
類,則有ListDirectoryDetails
方法可以執行此操作。有關示例代碼,請參閱here以及here。要知道,如果你想要包含所有子目錄的總大小,我想你必須進入每個子目錄並遞歸調用它,因此它可能很慢。如果可能的話,通常我會建議讓服務器上的腳本計算大小並以某種方式返回結果(可能將其存儲在可以下載和閱讀的文件中)。
編輯:或者,如果您只是表示您會對使用它的工具感到滿意,我認爲FlashFXP可以完成這項工作,並且可能還會使用其他高級FTP客戶端。或者,如果它是一個unix服務器,我有一個模糊的內存,你可以登錄並輸入ls -laR
或其他東西來獲得遞歸目錄列表。
只要使用FTP「大小」命令
如果你有FileZilla中,你可以使用這一招:
- 點擊您要計算 其大小的文件夾(或多個)
- 點擊
Add files to queue
這將掃描所有文件夾和文件並將它們添加到隊列中。然後查看隊列窗格並在其下面(狀態欄上),您應該看到一條指示隊列大小的消息。
很好的解決。這樣,您無需下載任何文件即可獲取文件總數和總大小。如果有人對此感興趣,Filezilla使用的[FTP命令](http://en.wikipedia.org/wiki/List_of_FTP_commands)是MLSD(遞歸)。 – 2014-04-24 09:04:09
請注意,您需要在本地目錄樹窗格中選擇一個有效的目標(否則_Add files to queue_將變灰)。 – jbaums 2015-03-15 23:33:12
我使用Alex Pilotti的FTPS library和C#在少數生產環境中執行一些FTP命令。該庫運行良好,但您必須遞歸獲取目錄中的文件列表並將它們的大小相加以獲得結果。在我們的一些較大的服務器(有時1-2分鐘)中,這對於複雜的文件結構可能會耗費一些時間。
無論如何,這是我與他的圖書館使用方法:
/// <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;
}
可以在lftp
使用du
命令用於此目的,像這樣:
echo "du -hs ." | lftp example.com 2>&1
這將打印當前目錄的磁盤大小所有子目錄都以可讀的格式(-h
)並省略子目錄的輸出行(-s
)。 stderr輸出被重新路由到stdout與2>&1
,以便它包含在輸出中。
但是,lftp
是僅限Linux的軟件,因此要在C#中使用它,您需要在Cygwin之內使用它。
its manpage中缺少lftp du
命令文檔,但在lftp shell中使用help du
命令可用。作爲參考,我在這裏複製其輸出:
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
用於身份驗證 echo「du -hs。」 | lftp -u user,password hostname 2>&1 – 2015-08-31 16:59:52
替代cygwin的是msys2 – 2017-01-29 15:40:49
如果您使用的是Windows 10,現在可以使用Windows子系統Linux版(WSL) – 2017-03-03 23:06:35
最簡單,獲取FTP目錄大小的有效方式與它的所有內容遞歸。
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);
}
}
- 1. 計算目錄大小
- 2. 使用Python計算目錄的大小?
- 3. Laravel - 計算目錄的總大小?
- 4. 如何計算記錄的大小
- 5. delphi - 計算目錄大小API?
- 6. MySQL表的數據目錄大小大小從information_schema.tables計算
- 7. 計算目錄的在R上的大小
- 8. 上大數目計算
- 9. 如何在遠程計算機上管理eclipse項目; ssh,ftp?
- 10. 如何計算可用內存上的最大列表大小?
- 11. 如何計算上傳大文件的最佳塊大小
- 12. 在計算目錄大小的同時獲取UnauthorizedAccessException
- 13. 使用CMD計算目錄中文檔的大小
- 14. `find'如何計算文件的大小?
- 15. 如何計算此表單的大小?
- 16. 如何計算精靈的大小?
- 17. 如何計算NSString的大小?
- 18. 如何計算查詢bigquery的大小?
- 19. 如何計算GIF圖像的大小?
- 20. 如何計算DB2中表的大小
- 21. MongoDB如何計算文檔的大小?
- 22. 如何計算UITextView的邊距大小?
- 23. Qt如何計算它的大小?
- 24. 在java中,如何計算最大數目減去較小數字的計算?
- 25. 如何在C中計算您的計算機的字大小?
- 26. Appwidget大小計算
- 27. C++計算大小
- 28. 如何計算保留的堆大小和淺堆大小?
- 29. 如何計算目錄和取得的最大數值
- 30. 如何計算目錄中的文件
非常酷smartftp功能,感謝 – 2014-03-16 20:38:52
的WinSCP做到這一點,以及 – aaiezza 2015-10-21 19:23:26