Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get list of files from ftp

Get list of files from ftp

Scheduled Pinned Locked Moved C#
regexquestioncsssysadmintutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    toink toink
    wrote on last edited by
    #1

    I found an ftp client class from this site and I used the List Function which is as follows: public ArrayList List() { Byte[] bytes = new Byte[512]; string file_list = ""; long bytesgot = 0; int msecs_passed = 0; ArrayList list = new ArrayList(); Connect(); OpenDataSocket(); SendCommand("LIST"); ReadResponse(); //FILIPE MADUREIRA. //Added response 125 switch(response) { case 125: case 150: break; default: CloseDataSocket(); throw new Exception(responseStr); } ConnectDataSocket(); // ####################################### while(data_sock.Available < 1) { System.Threading.Thread.Sleep(50); msecs_passed += 50; // this code is just a fail safe option // so the code doesn't hang if there is // no data comming. if (msecs_passed > (timeout / 10)) { //CloseDataSocket(); //throw new Exception("Timed out waiting on server to respond."); //FILIPE MADUREIRA. //If there are no files to list it gives timeout. //So I wait less time and if no data is received, means that there are no files break;//Maybe there are no files } } while(data_sock.Available > 0) { bytesgot = data_sock.Receive(bytes, bytes.Length, 0); file_list += Encoding.ASCII.GetString(bytes, 0, (int)bytesgot); System.Threading.Thread.Sleep(50); // *shrug*, sometimes there is data comming but it isn't there yet. } CloseDataSocket(); ReadResponse(); if (response != 226) throw new Exception(responseStr); foreach(string f in file_list.Split('\n')) { if (f.Length > 0 && !Regex.Match(f, "^total").Success) list.Add(f.Substring(0, f.Length - 1)); } return list; } But everytime I get files from ftp using this function it includes a date plus other characters plus the name of the file itself. For example: I want to get all the files inside a certain folder in ftp. The filenames of which are Text1.txt, Text2.txt and Text3.txt. Yet when i tried to retrieve its files using this function, it returns 08-29-07 06:42PM 0 Test1.txt 08-29-07 06:42PM 0 Test2.txt 08-29-07 06:42PM 0 Test2.txt My question is, how can i retrieve files from ftp returning only the original filenames which are Text1.txt, Text2.txt and Text3.txt. Thanks

    V 1 Reply Last reply
    0
    • T toink toink

      I found an ftp client class from this site and I used the List Function which is as follows: public ArrayList List() { Byte[] bytes = new Byte[512]; string file_list = ""; long bytesgot = 0; int msecs_passed = 0; ArrayList list = new ArrayList(); Connect(); OpenDataSocket(); SendCommand("LIST"); ReadResponse(); //FILIPE MADUREIRA. //Added response 125 switch(response) { case 125: case 150: break; default: CloseDataSocket(); throw new Exception(responseStr); } ConnectDataSocket(); // ####################################### while(data_sock.Available < 1) { System.Threading.Thread.Sleep(50); msecs_passed += 50; // this code is just a fail safe option // so the code doesn't hang if there is // no data comming. if (msecs_passed > (timeout / 10)) { //CloseDataSocket(); //throw new Exception("Timed out waiting on server to respond."); //FILIPE MADUREIRA. //If there are no files to list it gives timeout. //So I wait less time and if no data is received, means that there are no files break;//Maybe there are no files } } while(data_sock.Available > 0) { bytesgot = data_sock.Receive(bytes, bytes.Length, 0); file_list += Encoding.ASCII.GetString(bytes, 0, (int)bytesgot); System.Threading.Thread.Sleep(50); // *shrug*, sometimes there is data comming but it isn't there yet. } CloseDataSocket(); ReadResponse(); if (response != 226) throw new Exception(responseStr); foreach(string f in file_list.Split('\n')) { if (f.Length > 0 && !Regex.Match(f, "^total").Success) list.Add(f.Substring(0, f.Length - 1)); } return list; } But everytime I get files from ftp using this function it includes a date plus other characters plus the name of the file itself. For example: I want to get all the files inside a certain folder in ftp. The filenames of which are Text1.txt, Text2.txt and Text3.txt. Yet when i tried to retrieve its files using this function, it returns 08-29-07 06:42PM 0 Test1.txt 08-29-07 06:42PM 0 Test2.txt 08-29-07 06:42PM 0 Test2.txt My question is, how can i retrieve files from ftp returning only the original filenames which are Text1.txt, Text2.txt and Text3.txt. Thanks

      V Offline
      V Offline
      Vasudevan Deepak Kumar
      wrote on last edited by
      #2

      toink toink wrote:

      08-29-07 06:42PM 0 Test1.txt 08-29-07 06:42PM 0 Test2.txt 08-29-07 06:42PM 0 Test2.txt

      You may need to parse the output of the FTP process.

      Vasudevan Deepak Kumar Personal Homepage Tech Gossips

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups