|
|
|
มีวิธี นับจำนวน file folder ใน ftp ไหมครับ ลองใช้ system.io เเล้วใช้ไมได้ครับ |
|
|
|
|
|
|
|
ใช้ c# generate batch file
ใน batch file มีคำสั่ง ftp ตั้งแต่ user, password, และ ls -l (ใช้แสดง list ของ file ทั้งหมด)
ใช้ -l เพื่อจะได้รู้ attribute ว่าอันไหนเป็น directory
รัน batch file
เขียน output ที่ได้ออกมาเป็น text file
เอามาอ่านแล้วนับเฉพาะที่เป็น directory ออกมา
|
|
|
|
|
Date :
2010-09-16 13:56:00 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
รูปแบบ ไฟล์คำสั่ง ที่ต้องเจ็น
ftpcmd.txt
YourUserName
YourPassword
ls -l
bye
รูปแบบ batch file ที่จะสั่งรัน
ftpbatch.bat
ftp -s:ftpcmd.txt 127.0.0.1 > output.txt
type NUL > ftpcmd.txt
del ftpcmd.txt
ftpcmd.txt = ชื่อไฟล์คำสั่ง
127.0.0.1 = ip address ของ ftp server
output.txt = output ที่จะไรด์ออกมา
type NUL > ftpcmd.txt = ลบ user password ในไฟล์
del ftpcmd.txt = ลบไฟล์
แล้วก็สั่งรัน
System.Diagnostics.Process.Start(@"c:\ftpbatch.bat");
จากนั้นก็เอา output.txt ไปอ่าน ตามสะดวก
|
ประวัติการแก้ไข 2010-09-16 14:21:04 2010-09-16 14:22:54
|
|
|
|
Date :
2010-09-16 14:20:40 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ คุณ tungman
แต่ผมรู้สึกว่ามันยาก จัง
คือ สื่งที่ผมต้องการคือ สมมติว่าผมมีอยู่ 1 folder เเล้วผมต้อง upload ไฟล์เข้าไปใน folder นี้เรื่อยๆ เลยคิดว่าวันนึงมันคงจะใส่ไม่ได้ (คิดว่า folder มันน่าจะมีขีดจำกัดในการใส่ folder อยู่ อาจจะซัก 10000 ไฟล์ ต่อ1 folder อันนี้เดาเอานะครับ)
ผมเลยอยาก check จำนวน file ใน folder สมมุติถ้าใน folder นี้มี 1000 file เเล้วให้ สร้าง folderใหม่ครับ มันจะมีวิธีทำไหมครับ
|
|
|
|
|
Date :
2010-09-16 18:29:25 |
By :
- - |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่มีหรอก ใช้ .net library ยาวกว่าอีก
Form1.Designer.cs
namespace FtpClient
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Host:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(81, 6);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(214, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "ftp://";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 35);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(63, 13);
this.label2.TabIndex = 2;
this.label2.Text = "User Name:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(81, 32);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(214, 20);
this.textBox2.TabIndex = 2;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(13, 61);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 13);
this.label3.TabIndex = 3;
this.label3.Text = "Password:";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(81, 58);
this.textBox3.Name = "textBox3";
this.textBox3.PasswordChar = '*';
this.textBox3.Size = new System.Drawing.Size(214, 20);
this.textBox3.TabIndex = 3;
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 84);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "Connect";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.listView1.Location = new System.Drawing.Point(16, 113);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(279, 137);
this.listView1.TabIndex = 5;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "Name";
this.columnHeader1.Width = 155;
//
// columnHeader2
//
this.columnHeader2.Text = "Type";
//
// columnHeader3
//
this.columnHeader3.Text = "Size";
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(16, 257);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 6;
this.button2.Text = "Upload";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Enabled = false;
this.button3.Location = new System.Drawing.Point(220, 257);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 7;
this.button3.Text = "Delete";
this.button3.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(307, 289);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Ftp Client";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FtpClient
{
public partial class Form1 : Form
{
private bool Connected;
public Form1()
{
InitializeComponent();
Connected = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (Connected)
{
listView1.Items.Clear();
button1.Text = "Connect";
button2.Enabled = false;
button3.Enabled = false;
textBox1.ReadOnly = false;
textBox2.ReadOnly = false;
textBox3.ReadOnly = false;
Connected = false;
}
else
{
try
{
// ถ้าเอาแค่จำนวนก็ใช้ Dt.Rows.Count ก็จะได้จำนวน file + folder แต่ถ้าจะเอาเฉพาะ file ก็ต้องตรวจเงื่อนไขในลูป
DataTable Dt = FtpClientManager.DisplayFtpFile(new Uri(textBox1.Text), textBox2.Text, textBox3.Text);
foreach (DataRow Dr in Dt.Rows)
{
ListViewItem ListViewItem1 = new ListViewItem();
ListViewItem1.Text = Dr["FileName"].ToString();
ListViewItem1.SubItems.Add(Dr["FileType"].ToString());
ListViewItem1.SubItems.Add(Dr["FileSize"].ToString());
listView1.Items.Add(ListViewItem1);
button1.Text = "Disconnect";
button2.Enabled = true;
button3.Enabled = true;
textBox1.ReadOnly = true;
textBox2.ReadOnly = true;
textBox3.ReadOnly = true;
}
Connected = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
FtpClientManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
namespace FtpClient
{
class FtpClientManager
{
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
}
public static DataTable DisplayFtpFile(Uri serverUri, string userName, string password)
{
DataTable Dt = new DataTable();
Dt.Columns.Add(new DataColumn("FileName", typeof(string)));
Dt.Columns.Add(new DataColumn("FileType", typeof(string)));
Dt.Columns.Add(new DataColumn("FileSize", typeof(int)));
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
DataRow Dr = Dt.NewRow();
Dr["FileName"] = "Invalid Uri.";
Dr["FileType"] = string.Empty;
Dr["FileSize"] = 0;
Dt.Rows.Add(Dr);
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(userName, password);
request.Proxy = null;
FtpWebResponse response = request.GetResponse() as FtpWebResponse;
try
{
StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string Datastring = sr.ReadToEnd();
response.Close();
FileStruct[] list = (new FtpClientManager()).GetList(Datastring);
foreach (FileStruct thisstruct in list)
{
DataRow Dr = Dt.NewRow();
Dr["FileName"] = thisstruct.Name;
Dr["FileType"] = (thisstruct.IsDirectory) ? "Folder" : "File";
Dr["FileSize"] = 0;
Dt.Rows.Add(Dr);
}
}
catch (WebException ex)
{
DataRow Dr = Dt.NewRow();
Dr["FileName"] = ex.Message;
Dr["FileType"] = string.Empty;
Dr["FileSize"] = 0;
Dt.Rows.Add(Dr);
}
return Dt;
}
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(); ;
}
private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
{
///Assuming the record style as
/// 02-03-04 07:46PM <DIR> Append
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();
f.CreateTime = DateTime.Parse(dateStr + " " + timeStr);
if (processstr.Substring(0, 5) == "<DIR>")
{
f.IsDirectory = true;
processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
}
else
{
string[] strs = processstr.Split(new char[] { ' ' });
processstr = strs[1].Trim();
f.IsDirectory = false;
}
f.Name = processstr; //Rest is name
return f;
}
public 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;
}
private FileStruct ParseFileStructFromUnixStyleRecord(string Record)
{
///Assuming record style as
/// dr-xr-xr-x 1 owner group 0 Nov 25 2002 bussys
FileStruct f = new FileStruct();
string processstr = Record.Trim();
f.Flags = processstr.Substring(0, 9);
f.IsDirectory = (f.Flags[0] == 'd');
processstr = (processstr.Substring(11)).Trim();
_cutSubstringFromStringWithTrim(ref processstr, ' ', 0); //skip one part
f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
_cutSubstringFromStringWithTrim(ref processstr, ' ', 0); //skip one part
f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8));
f.Name = processstr; //Rest of the part is name
return f;
}
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;
}
}
}
ตอนรัน
|
ประวัติการแก้ไข 2010-09-16 20:37:34
|
|
|
|
Date :
2010-09-16 20:28:57 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณ คุณ tungman มากครับ จะเอาไปศึกษาดูครับ
|
|
|
|
|
Date :
2010-09-17 10:52:22 |
By :
- - |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|