Get list of files from ftp
-
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
-
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
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