Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Using ListDirectoryDetails
Message
De
17/04/2010 12:33:15
 
 
À
17/04/2010 12:19:22
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01460672
Message ID:
01460744
Vues:
44
>>>Use GetDateTimeStamp instead.
>>
>>Thanks, it is a shame that we are forced to do that to get the full date time stamp as the ListDirectoryDetails() method does not support. This means that in order to compare the time stamp of a local file against the server file, we would have to send one request per file to the server to get that additional information. So, I have several directories having 1000 files, about 20 of them, so, instead of sending 20 commands to get the full directory listing, I would have to send 20000 requests against the server.
>>
>>There has to be a better way.
>
>I thought the same way and looked it a little bit today. Though it sounded really weird to me here is what I have found. You use ListDirectoryDetails and parse the datetime part. But if it is in current year, directory listing has hour:minute instead of a year. If it is in another year then it lists the year. Here is a listing w/o parsin further:
>
>
>void Main()
>{
>   Uri uri = new Uri("ftp://ftp.prolib.de/Public");
>   FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uri); 
>	 
>   req.Credentials = new NetworkCredential ("anonymous", "password");
>   req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
>   string details;
>   using(FtpWebResponse resp  = (FtpWebResponse)req.GetResponse())
>   using(Stream stream = resp.GetResponseStream())
>   using(StreamReader reader = new StreamReader(stream))
>	 {
>	  details = reader.ReadToEnd();
>	 }
>	 Console.WriteLine (details);
>}
>
And here is the list I got:
>
drw-rw-rw-   1 user     group           0 Apr 11 13:23 .
>drw-rw-rw-   1 user     group           0 Apr 11 13:23 ..
>-rw-rw-rw-   1 user     group     1823921 Apr 11 13:23 FoxHelp_Win26_GER.zip
>drw-rw-rw-   1 user     group           0 Mar 30  2007 FoxPro
>-rw-rw-rw-   1 user     group        4408 Oct 20 04:30 ReadMe_Runtime.txt
>drw-rw-rw-   1 user     group           0 Dec  8  2008 VFP
>-rw-rw-rw-   1 user     group     7685314 Oct 12  1999 VFP3SP1RT.EXE
>-r--r--r--   1 user     group     1454080 Dec 14  1997 VFP5SP1RT.EXE
>-r--r--r--   1 user     group     1454080 Dec 14  1997 VFP5SP1RT.W02
>-r--r--r--   1 user     group       34177 Dec 14  1997 VFP5SP1RT.W03
>-rw-rw-rw-   1 user     group     6177190 Oct 20 04:22 VFP6SP5RT.EXE
>-rw-rw-rw-   1 user     group     6300577 Sep 21  2001 VFP7SP0RT.EXE
>-rw-rw-rw-   1 user     group    10089422 Oct 20 02:48 VFP7SP1RT.exe
>-rw-rw-rw-   1 user     group     9441214 Jun 27  2003 VFP8SP0RT.EXE
>-rw-rw-rw-   1 user     group    11042282 Oct 20 02:49 VFP8SP1RT.exe
>-rw-rw-rw-   1 user     group    12989444 Oct 20 02:50 VFP9SP0RT.exe
>-rw-rw-rw-   1 user     group    12722668 Oct 20 02:51 VFP9SP1RT.exe
>-rw-rw-rw-   1 user     group    12837733 Oct 20 04:24 VFP9SP2RT.exe
>drw-rw-rw-   1 user     group           0 Mar 30  2007 Why_VFP
>
Strangely you can see entries in future.
>Cetin


The GetDirectoryListing returns a unix ls -l

I was writing this assuming that lines without a year were dates in the current year - until - I saw your post
So, I have adjusted my code, added a test is date > today, subtract a year


This is the output I get from ftp://ftp.prolib.de/Public
True 11/04/2010 13:23:00 0 .
True 11/04/2010 13:23:00 0 ..
False 11/04/2010 13:23:00 1823921 FoxHelp_Win26_GER.zip
True 30/03/2007 00:00:00 0 FoxPro
False 20/10/2009 04:30:00 4408 ReadMe_Runtime.txt
True 08/12/2008 00:00:00 0 VFP
False 12/10/1999 00:00:00 7685314 VFP3SP1RT.EXE
False 14/12/1997 00:00:00 1454080 VFP5SP1RT.EXE
False 14/12/1997 00:00:00 1454080 VFP5SP1RT.W02
False 14/12/1997 00:00:00 34177 VFP5SP1RT.W03
False 20/10/2009 04:22:00 6177190 VFP6SP5RT.EXE
False 21/09/2001 00:00:00 6300577 VFP7SP0RT.EXE
False 20/10/2009 02:48:00 10089422 VFP7SP1RT.exe
False 27/06/2003 00:00:00 9441214 VFP8SP0RT.EXE
False 20/10/2009 02:49:00 11042282 VFP8SP1RT.exe
False 20/10/2009 02:50:00 12989444 VFP9SP0RT.exe
False 20/10/2009 02:51:00 12722668 VFP9SP1RT.exe
False 20/10/2009 04:24:00 12837733 VFP9SP2RT.exe
True 30/03/2007 00:00:00 0 Why_VFP
namespace BaseTest
{
	class Program
	{
		static string Site = @"ftp://ftp.prolib.de/Public";
		static string UserId = "anonymous";
		static string Passwd = "password";

		public static void Main()
		{
	

			var ftp = new FtpTest(Site, UserId, Passwd);

			var files = ftp.GetDirectoryListing("");

			foreach (var f in files)
			{
				Console.WriteLine(
						"{0} {1} {2} {3}",
						f.IsDirectory,
						f.FileTime,
						f.FileSize,
						f.FileName);
			}

			Console.ReadLine();
		}

	}
	public class FtpTest
	{
		string Site;
		NetworkCredential Credentials ;

		static string LinePattern = 
		@"^(?<IsFolder>\S)(?<Permissions>\S{9})\s+(?<Links>\d+)\s+(?<User>\S+)\s+(?<Group>\S+)\s+(?<FileSize>\d+)\s+((?<DateTimeCurrentYear>(?<Month>\S{3})\s+(?<Day>\d{1,2})\s+(?<Time>\d\d:\d\d))|(?<DateTimeYear>(?<Month>\S{3})\s+(?<Day>\d{1,2})\s+(?<Year>\d{4})))\s+(?<FileName>\S+)$";
 
		public FtpTest(string site, string userId, string passwd)
		{
			Site = site;
			Credentials = new NetworkCredential(userId, passwd);

		}
		public List<FtpFileInfo> GetDirectoryListing(string path )
		{
			var request = (FtpWebRequest)WebRequest.Create(Site + "/" + path );
			request.Credentials = Credentials;

			request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

			var response = (FtpWebResponse)request.GetResponse();

			var list = new List<FtpFileInfo>();

			using (var stream = new StreamReader(response.GetResponseStream()))
			{
				while (!stream.EndOfStream)
				{

					string line = stream.ReadLine();
					Match match = Regex.Match(line, LinePattern);

					if (match.Success)
					{
						bool isFolder = match.Groups["FileName"].Value == "d";

						string fileName = match.Groups["FileName"].Value;
						ulong fileSize = Convert.ToUInt64(match.Groups["FileSize"].Value);
						DateTime dateTime;

						if (String.IsNullOrEmpty(match.Groups["DateTimeCurrentYear"].Value))
						{
							// Month, Day, Year
							dateTime = Convert.ToDateTime(match.Groups["DateTimeYear"].Value);
						}
						else
						{
							// Month, Day, Time
	
							dateTime = Convert.ToDateTime(
								String.Format(
									"{0} {1} {2:D4} {3}",
									match.Groups["Month"].Value,
									match.Groups["Day"].Value,
									DateTime.Now.Year,
									match.Groups["Time"].Value
									)
								);
							if (dateTime > DateTime.Now)
								dateTime = dateTime.AddYears(-1);
						}

						list.Add(new FtpFileInfo(isFolder, fileName, dateTime, fileSize));
					}
					else
					{
						Debug.Assert(false, "cannot parse", line);
					}
					
				}
			}

			return list;
		}
	}

	public class FtpFileInfo
	{
		public readonly bool IsDirectory;
		public readonly string FileName;
		public readonly DateTime FileTime;
		public readonly ulong FileSize;

		public FtpFileInfo(bool isDirectory, string fileName, DateTime fileTime, ulong fileSize)
		{
			IsDirectory = isDirectory;
			FileName = fileName;
			FileTime = fileTime;
			FileSize = fileSize;
		}
	}
}
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform