Solved

Downloaded text file from FTP is one long string

Posted on 2014-01-30
4
394 Views
Last Modified: 2014-01-31
Hello,

I have code that is working correctly to download a text file from an FTP server, however that text file comes across as one big long run-on string.  The text file I'm downloading is a listing of access codes delimited by a carriage return, which i eventually need to modify and re-upload, so the file needs to stay in-tact in regards to the formatting of it.  Is there any way to download the file as-is and not have it return as a big pile of characters?  This is my first attempt at pulling down a file from FTP so I'm hoping there is just something easy I am missing.

The code I am using currently to get the file and save it is:

string serverpath = "ftp://ftpsite.com/codefile.txt";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverpath);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("username", "password");

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

StreamWriter writer = new StreamWriter("Y:\\downloadedCodeFile.txt");
writer.Write(reader.ReadToEnd());

writer.Close();
reader.Close();
response.Close();

Thanks,
Joe
0
Comment
Question by:sglew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Expert Comment

by:AlexPace
ID: 39822692
Try transferring the file in ASCII mode.  That will cause the server to convert the line endings to CRLF so they will appear "normal" in Windows.  Do it when you re-upload too so the server can translate the line endings back into its native format.
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 39823633
Hmm - how deep have you inspected the resulting file ?

Does it still contain the original CR characters (which might be suppressed in display by your editor) or did those CRs stripped ?

If the characters are still in it, I would try to read / write the data not as a big blob but line by line, which might need possibly with some kind of end-of-line detection setting.

If the data file is not that big, I would try another attempt:
Transfer the data in binary mode into a big String object,

try to detect the EOL mode (CR / CR-LF) by searching for CR-CF in it,
then use String.Split(...) to make an array on Strings as Lines from it and finally
use File.WriteAll() to put the data into a disk file
Not elegant, but handy.
0
 
LVL 16

Accepted Solution

by:
AlexPace earned 400 total points
ID: 39823645
Don't forget that he has to re-upload the changed files so all that would have to be done in reverse after doing whatever modifications he does.

The FTP specification says that servers are responsible for changing the line endings to CRLF for transfers done in ASCII mode and almost all servers are compliant in that regard...  Transferring in binary and manually re-inflating the line endings is certainly an option but it seems like a waste of time when you could just do the transfer in ASCII in the first place by doing something like this:
request.UseBinary = false;

... anyway, while on the topic of "not elegant, but handy" .... you could download copies of the unix2dos and dos2unix utilities... save the string as a file... run unix2dos on it to get your line endings back... do your modifications... run dos2unix to change the line endings back to a format that the remote site will understand... then upload it in binary.  heh,
0
 

Author Closing Comment

by:sglew
ID: 39824021
Bingo, that was it!  I figured it was probably something easy that I was missing.  Setting the UseBinary attribute to false allowed the file to save as expected.

Thanks AlexPace.

-Joe
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question