如何计算FTP上目录的大小?
你可以发送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);
}
}
非常酷smartftp功能,感谢 – 2014-03-16 20:38:52
的WinSCP做到这一点,以及 – aaiezza 2015-10-21 19:23:26