Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > มีวิธี นับจำนวน file folder ใน ftp ไหมครับ ลองใช้ system.io เเล้วใช้ไมได้ครับ



 

มีวิธี นับจำนวน file folder ใน ftp ไหมครับ ลองใช้ system.io เเล้วใช้ไมได้ครับ

 



Topic : 048859

Guest




มีวิธี นับจำนวน file และ folder ใน ftp ไหมครับ
พอดีผมใช้ System.IO.DirectoryInfo
แล้วมันได้แค่ในเครื่องครับ
ถ้าใช้กับ ftp มันบอกว่า ไม่ support เลยไม่รู้ว่า ต้องใช้ function ไหน หรือวิธีไหนในการนับจำนวน file ดับ folder ครับ



Tag : .NET, MySQL, Win (Windows App), C#







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-09-16 12:18:54 By : - - View : 3698 Reply : 6
 

 

No. 1



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ใช้ 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
 


 

No. 2



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


รูปแบบ ไฟล์คำสั่ง ที่ต้องเจ็น

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
 

 

No. 3

Guest


ขอบคุณครับ คุณ 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 : - -
 


 

No. 4



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ไม่มีหรอก ใช้ .net library ยาวกว่าอีก

1

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;
        }
    }
}


ตอนรัน

2


ประวัติการแก้ไข
2010-09-16 20:37:34
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-09-16 20:28:57 By : tungman
 


 

No. 5

Guest


ขอบคุณ คุณ tungman มากครับ จะเอาไปศึกษาดูครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2010-09-17 10:52:22 By : - -
 


 

No. 6



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Go to : ถามเรื่องการอัพไฟล์ขึ้น ftpโดยตรง อัพขขึ้นโดยตรง โดยไม่ต้องผ่าน path ค่ะ จะต้องเปลี่ยน เงื่อนไขยังไง
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-05-26 06:28:44 By : webmaster
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : มีวิธี นับจำนวน file folder ใน ftp ไหมครับ ลองใช้ system.io เเล้วใช้ไมได้ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่