Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi there,

I have created a program where I connect to a sftp server and retrieve 2 files every 30mins.

each time I connect the 2 new files I download overwrite the 2 existing files.

I would like keep the existing 2 files and create 2 new ones instead of overwriting them.

Here is the code that retrieves both files:

C#
using (FileStream fs = new FileStream(Path.Combine(Properties.Settings.Default.XmlLocation,"file1.xml"), FileMode.Create))
                {                    
                    //Program.processLog.Write("Retrieving File 1" + "\n");
                    client.DownloadFile("dogs.xml", fs);
                             
                    fs.Close();
                }                

 
                client.Disconnect();


please kindly advise how I may save the files I download for 30days instead of overwriting existing files.

thanks
Posted

Stop calling the file "file1.xml" for starters, and give it a sensible name.
A good idea is to use the date/ time you downloaded it:
yyyyMMddHHmmss Dogs.xml
This or a similar name means that they are easy for the user to identify and sort by date, and the file names don't clash.
It's also easy to do:
C#
string fileName = @"D:\MyFolder\" + DateTime.Now.ToString("yyyyMMddHHmmss") + " Dogs.xml";

You can then check when you download a file if any of them are out of date:
C#
DateTime tooOld = DateTime.Now.AddDays(-30);
var toRemove = Directory.GetFiles(@"D:\MyFolder", "* Dogs.xml")
                .Select(s => new FileInfo(s))
                .Where(fi => fi.CreationTime < tooOld)
                .Select(fi => fi.FullName);

You can then delete all the files in the collection.
 
Share this answer
 
XML
DateTime tooOld = DateTime.Now.AddDays(-30);
var toRemove = Directory.GetFiles(@"D:\MyFolder", "* Dogs.xml")
                .Select(s => new FileInfo(s))
                .Where(fi => fi.CreationTime < tooOld)
                .Select(fi => fi.FullName);



this does not delete the file...

I changed it slighty for testing purposes to read:

XML
<pre lang="xml">DateTime tooOld = DateTime.Now.AddMinutes(-30);
var toRemove = Directory.GetFiles(@&quot;D:\MyFolder&quot;, &quot;* Dogs.xml&quot;)
                .Select(s =&gt; new FileInfo(s))
                .Where(fi =&gt; fi.CreationTime &lt; tooOld)
                .Select(fi =&gt; fi.FullName);</pre>


But it will not delete files...

Or am I doing something wrong?
 
Share this answer
 
v8
Comments
George Jonsson 8-Jul-14 6:57am    
You need to update your question with your new code.
There is no way I can see you can get this error using the solution from OriginalGriff.
xirokx 8-Jul-14 7:02am    
I know OriginalGriff solution works...

But now I have a different prob, see above...see solution 3 I edited it
CHill60 8-Jul-14 9:39am    
All you have done is get a list of files to delete - you actually have to issue and appropriate command to delete them as well!! e.g. System.IO.File.Delete(fileNameToDelete);
xirokx 8-Jul-14 10:55am    
thanks I got it in the end...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900